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