Spaces:
Build error
Build error
Deploy chatbot update
Browse files
app.py
CHANGED
|
@@ -1,31 +1,27 @@
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
import pdfplumber
|
| 4 |
-
|
| 5 |
-
from huggingface_hub import hf_hub_download, login
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain.chains import RetrievalQA
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ:
|
| 13 |
-
raise ValueError("โ HUGGINGFACEHUB_API_TOKEN
|
| 14 |
-
login(token=os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
repo_id = "meta-llama/Llama-3.2-3B-Instruct"
|
| 18 |
llm = HuggingFaceEndpoint(
|
| 19 |
-
repo_id=
|
| 20 |
-
huggingfacehub_api_token=os.
|
| 21 |
temperature=0.7,
|
| 22 |
task="text-generation"
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# 4. ์ฒ๋ฆฌํ PDF ํ์ผ ๋ฆฌ์คํธ
|
| 29 |
pdf_files = [
|
| 30 |
"(20220324) L2 Switch ์ด์ฉ ๋งค๋ด์ผ_Innovation TF_Ver3.1_OCR.pdf",
|
| 31 |
"(20230504) 23๋
๊ธฐ์ ๊ต์ก ๊ต์ฌ 1 (LTE)_๊ฐ์นํ์ ํ_OCR.pdf",
|
|
@@ -68,59 +64,62 @@ pdf_files = [
|
|
| 68 |
"์ฐจ๋จ๊ธฐ ์ข
๋ฅ ๋ฐ ์ฉ๋_OCR.pdf"
|
| 69 |
]
|
| 70 |
|
| 71 |
-
# 5. PDF ํ
์คํธ ์ถ์ถ
|
| 72 |
docs = []
|
|
|
|
| 73 |
for fname in pdf_files:
|
| 74 |
try:
|
| 75 |
-
pdf_path = hf_hub_download(repo_id=
|
| 76 |
texts = []
|
| 77 |
with pdfplumber.open(pdf_path) as pdf:
|
| 78 |
-
for page in pdf.pages:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
else:
|
| 86 |
print(f"โ ๏ธ ํ
์คํธ ์์: {fname}")
|
| 87 |
except Exception as e:
|
| 88 |
print(f"๐จ ์ค๋ฅ ๋ฐ์: {fname} - {str(e)}")
|
| 89 |
|
|
|
|
| 90 |
if not docs:
|
| 91 |
raise ValueError("โ PDF์์ ์ถ์ถ๋ ํ
์คํธ๊ฐ ์์ต๋๋ค. (docs ๋ฆฌ์คํธ ๋น์ด์์)")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# 6. ํ
์คํธ ๋ถํ
|
| 96 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 97 |
texts = splitter.split_documents(docs)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1")
|
| 101 |
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
qa_chain = RetrievalQA.from_chain_type(
|
| 105 |
llm=llm,
|
| 106 |
chain_type="stuff",
|
| 107 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 108 |
)
|
| 109 |
|
| 110 |
-
#
|
| 111 |
def chatbot(query: str):
|
| 112 |
try:
|
| 113 |
return qa_chain.run(query)
|
| 114 |
except Exception as e:
|
| 115 |
-
return f"โ
|
| 116 |
|
| 117 |
-
#
|
| 118 |
with gr.Blocks(title="Ericsson ์ฅ๋น ๋ถ์ ์ฑ๋ด") as demo:
|
| 119 |
-
gr.Markdown("
|
| 120 |
-
gr.Markdown("Hugging Face
|
| 121 |
query = gr.Textbox(label="์ง๋ฌธ ์
๋ ฅ (ํ๊ตญ์ด/์์ด)", placeholder="์: Spurious Emission ์์ธ์?")
|
| 122 |
output = gr.Textbox(label="์๋ต", lines=10)
|
| 123 |
-
btn = gr.Button("๋ถ์
|
| 124 |
btn.click(chatbot, inputs=query, outputs=output)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
|
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
import pdfplumber
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
|
|
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
+
import gradio as gr
|
| 10 |
|
| 11 |
+
# โ
ํ๊ฒฝ ๋ณ์ (Secrets์์ ์๋ ์ฃผ์
)
|
| 12 |
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ:
|
| 13 |
+
raise ValueError("โ HUGGINGFACEHUB_API_TOKEN ์ด ์ค์ ๋์ง ์์์ต๋๋ค. Spaces โ Settings โ Secrets์์ ์ถ๊ฐํ์ธ์.")
|
|
|
|
| 14 |
|
| 15 |
+
# โ
LLM ๋ชจ๋ธ ์ค์
|
|
|
|
| 16 |
llm = HuggingFaceEndpoint(
|
| 17 |
+
repo_id="meta-llama/Llama-3.2-3B-Instruct",
|
| 18 |
+
huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
|
| 19 |
temperature=0.7,
|
| 20 |
task="text-generation"
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# โ
Hugging Face Datasets โ PDF ๋ค์ด๋ก๋
|
| 24 |
+
repo_id = "dgmos/ericsson-manuals"
|
|
|
|
|
|
|
| 25 |
pdf_files = [
|
| 26 |
"(20220324) L2 Switch ์ด์ฉ ๋งค๋ด์ผ_Innovation TF_Ver3.1_OCR.pdf",
|
| 27 |
"(20230504) 23๋
๊ธฐ์ ๊ต์ก ๊ต์ฌ 1 (LTE)_๊ฐ์นํ์ ํ_OCR.pdf",
|
|
|
|
| 64 |
"์ฐจ๋จ๊ธฐ ์ข
๋ฅ ๋ฐ ์ฉ๋_OCR.pdf"
|
| 65 |
]
|
| 66 |
|
|
|
|
| 67 |
docs = []
|
| 68 |
+
|
| 69 |
for fname in pdf_files:
|
| 70 |
try:
|
| 71 |
+
pdf_path = hf_hub_download(repo_id=repo_id, filename=fname, repo_type="dataset")
|
| 72 |
texts = []
|
| 73 |
with pdfplumber.open(pdf_path) as pdf:
|
| 74 |
+
for page_num, page in enumerate(pdf.pages, start=1):
|
| 75 |
+
try:
|
| 76 |
+
content = page.extract_text()
|
| 77 |
+
if content:
|
| 78 |
+
texts.append(content)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"โ ๏ธ PDF ํ์ฑ ์ค๋ฅ (๋ฌด์): {fname} p.{page_num} - {str(e)}")
|
| 81 |
+
if texts:
|
| 82 |
+
docs.append({"page_content": "
|
| 83 |
+
".join(texts)})
|
| 84 |
+
print(f"โ
ํ
์คํธ ์ถ์ถ ์ฑ๊ณต: {fname}")
|
| 85 |
else:
|
| 86 |
print(f"โ ๏ธ ํ
์คํธ ์์: {fname}")
|
| 87 |
except Exception as e:
|
| 88 |
print(f"๐จ ์ค๋ฅ ๋ฐ์: {fname} - {str(e)}")
|
| 89 |
|
| 90 |
+
# โ
๋ฌธ์ ๊ฒ์ฆ
|
| 91 |
if not docs:
|
| 92 |
raise ValueError("โ PDF์์ ์ถ์ถ๋ ํ
์คํธ๊ฐ ์์ต๋๋ค. (docs ๋ฆฌ์คํธ ๋น์ด์์)")
|
| 93 |
|
| 94 |
+
# โ
ํ
์คํธ ๋ถํ
|
|
|
|
|
|
|
| 95 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 96 |
texts = splitter.split_documents(docs)
|
| 97 |
|
| 98 |
+
# โ
๋ฒกํฐ DB ๊ตฌ์ถ
|
| 99 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1")
|
| 100 |
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 101 |
|
| 102 |
+
# โ
RAG ์ฒด์ธ
|
| 103 |
qa_chain = RetrievalQA.from_chain_type(
|
| 104 |
llm=llm,
|
| 105 |
chain_type="stuff",
|
| 106 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 107 |
)
|
| 108 |
|
| 109 |
+
# โ
์ฑ๋ด ํจ์
|
| 110 |
def chatbot(query: str):
|
| 111 |
try:
|
| 112 |
return qa_chain.run(query)
|
| 113 |
except Exception as e:
|
| 114 |
+
return f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 115 |
|
| 116 |
+
# โ
Gradio UI
|
| 117 |
with gr.Blocks(title="Ericsson ์ฅ๋น ๋ถ์ ์ฑ๋ด") as demo:
|
| 118 |
+
gr.Markdown("## ๐ 3G/LTE/5G ์ฅ๋น ๋ถ๋/๋ถ์ํ ๋ถ์ ์ฑ๋ด")
|
| 119 |
+
gr.Markdown("Hugging Face Dataset์์ OCR PDF ๊ธฐ๋ฐ ์ง๋ฌธ ์๋ต ์ ๊ณต")
|
| 120 |
query = gr.Textbox(label="์ง๋ฌธ ์
๋ ฅ (ํ๊ตญ์ด/์์ด)", placeholder="์: Spurious Emission ์์ธ์?")
|
| 121 |
output = gr.Textbox(label="์๋ต", lines=10)
|
| 122 |
+
btn = gr.Button("๋ถ์ ์์!")
|
| 123 |
btn.click(chatbot, inputs=query, outputs=output)
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|