Spaces:
Build error
Build error
Deploy chatbot update
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
-
from
|
| 6 |
from langchain_community.vectorstores import FAISS
|
| 7 |
-
from langchain_huggingface import HuggingFaceEndpoint
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
import gradio as gr
|
| 10 |
-
import pdfplumber
|
| 11 |
|
| 12 |
# 1. ํ๊ฒฝ ๋ณ์ ํ์ธ
|
| 13 |
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ:
|
| 14 |
-
raise ValueError(
|
| 15 |
-
|
| 16 |
-
"HF Space Settings > Secrets์์ ์ถ๊ฐํ์ธ์."
|
| 17 |
-
)
|
| 18 |
-
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 19 |
|
| 20 |
-
# 2. ๋ชจ๋ธ
|
| 21 |
repo_id = "meta-llama/Llama-3.2-3B-Instruct"
|
| 22 |
llm = HuggingFaceEndpoint(
|
| 23 |
repo_id=repo_id,
|
|
@@ -26,46 +22,45 @@ llm = HuggingFaceEndpoint(
|
|
| 26 |
task="text-generation"
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# 3.
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# 4. PDF โ ํ
์คํธ ์ถ์ถ
|
| 34 |
docs = []
|
| 35 |
-
for
|
| 36 |
-
pdf_path = item.get("file") or item.get("path") or None
|
| 37 |
-
if not pdf_path:
|
| 38 |
-
print(f"โ ๏ธ PDF ๊ฒฝ๋ก ์์: {item}")
|
| 39 |
-
continue
|
| 40 |
-
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
with pdfplumber.open(pdf_path) as pdf:
|
|
|
|
| 44 |
for page in pdf.pages:
|
| 45 |
content = page.extract_text()
|
| 46 |
if content:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
print(f"โ ๏ธ ํ
์คํธ ์ถ์ถ ์คํจ: {pdf_path}")
|
| 56 |
-
|
| 57 |
except Exception as e:
|
| 58 |
-
print(f"๐จ PDF ์ฒ๋ฆฌ ์ค๋ฅ: {
|
| 59 |
-
continue
|
| 60 |
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
# 5. ํ
์คํธ ๋ถํ
|
| 64 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 65 |
texts = splitter.split_documents(docs)
|
| 66 |
|
| 67 |
-
# 6. ์๋ฒ ๋ฉ + ๋ฒกํฐ
|
| 68 |
-
embeddings = HuggingFaceEmbeddings(
|
|
|
|
|
|
|
| 69 |
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 70 |
|
| 71 |
# 7. Retrieval QA ์ฒด์ธ
|
|
@@ -81,19 +76,19 @@ def chatbot(query: str):
|
|
| 81 |
response = qa_chain.run(query)
|
| 82 |
return response
|
| 83 |
except Exception as e:
|
| 84 |
-
return f"โ ์ค๋ฅ
|
| 85 |
|
| 86 |
# 9. Gradio UI
|
| 87 |
with gr.Blocks(title="Ericsson ์ฅ๋น ๋ถ์ ์ฑ๋ด") as demo:
|
| 88 |
-
gr.Markdown("#
|
| 89 |
-
gr.Markdown("Hugging Face
|
| 90 |
|
| 91 |
query = gr.Textbox(
|
| 92 |
label="์ง๋ฌธ ์
๋ ฅ (ํ๊ตญ์ด/์์ด)",
|
| 93 |
-
placeholder="์:
|
| 94 |
)
|
| 95 |
output = gr.Textbox(label="์๋ต", lines=10)
|
| 96 |
-
btn = gr.Button("๋ถ์ ์์
|
| 97 |
|
| 98 |
btn.click(chatbot, inputs=query, outputs=output)
|
| 99 |
|
|
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
+
import pdfplumber
|
| 4 |
+
from huggingface_hub import hf_hub_download, list_repo_files
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
|
| 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 |
+
"HF Space โ Settings โ Secrets์์ ์ถ๊ฐํ์ธ์.")
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# 2. LLM ๋ชจ๋ธ (Hugging Face Inference API)
|
| 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. ๋ฐ์ดํฐ์
์์ PDF ํ์ผ ๋ฆฌ์คํธ ์๋ ์์ง
|
| 26 |
+
dataset_repo = "dgmos/ericsson-manuals"
|
| 27 |
+
all_files = list_repo_files(dataset_repo)
|
| 28 |
+
pdf_files = [f for f in all_files if f.lower().endswith(".pdf")]
|
| 29 |
+
|
| 30 |
+
print(f"๐ ์ด {len(pdf_files)} ๊ฐ PDF ๊ฐ์ง๋จ")
|
| 31 |
|
| 32 |
# 4. PDF โ ํ
์คํธ ์ถ์ถ
|
| 33 |
docs = []
|
| 34 |
+
for filename in pdf_files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
+
pdf_path = hf_hub_download(repo_id=dataset_repo, filename=filename)
|
| 37 |
with pdfplumber.open(pdf_path) as pdf:
|
| 38 |
+
texts = []
|
| 39 |
for page in pdf.pages:
|
| 40 |
content = page.extract_text()
|
| 41 |
if content:
|
| 42 |
+
texts.append(content)
|
| 43 |
+
text = "
|
| 44 |
+
".join(texts).strip()
|
| 45 |
+
if text:
|
| 46 |
+
docs.append({"page_content": text, "metadata": {"source": filename}})
|
| 47 |
+
print(f"โ
{filename} ์ฒ๋ฆฌ ์๋ฃ (๊ธธ์ด: {len(text)}์)")
|
| 48 |
+
else:
|
| 49 |
+
print(f"โ ๏ธ ํ
์คํธ ์์: {filename}")
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
+
print(f"๐จ PDF ์ฒ๋ฆฌ ์ค๋ฅ: {filename} - {e}")
|
|
|
|
| 52 |
|
| 53 |
+
if not docs:
|
| 54 |
+
raise RuntimeError("โ PDF์์ ํ
์คํธ๋ฅผ ์ถ์ถํ์ง ๋ชปํ์ต๋๋ค. ํ์ธ ํ์!")
|
| 55 |
|
| 56 |
# 5. ํ
์คํธ ๋ถํ
|
| 57 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 58 |
texts = splitter.split_documents(docs)
|
| 59 |
|
| 60 |
+
# 6. ์๋ฒ ๋ฉ + ๋ฒกํฐDB
|
| 61 |
+
embeddings = HuggingFaceEmbeddings(
|
| 62 |
+
model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
| 63 |
+
)
|
| 64 |
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 65 |
|
| 66 |
# 7. Retrieval QA ์ฒด์ธ
|
|
|
|
| 76 |
response = qa_chain.run(query)
|
| 77 |
return response
|
| 78 |
except Exception as e:
|
| 79 |
+
return f"โ ์ค๋ฅ: {str(e)}"
|
| 80 |
|
| 81 |
# 9. Gradio UI
|
| 82 |
with gr.Blocks(title="Ericsson ์ฅ๋น ๋ถ์ ์ฑ๋ด") as demo:
|
| 83 |
+
gr.Markdown("# ๐ก Ericsson LTE/5G ์ฅ๋น ๋ถ๋/๋ถ์ํ ๋ถ์ ์ฑ๋ด")
|
| 84 |
+
gr.Markdown("Hugging Face Hub์ ์ ์ฅ๋ **๋ชจ๋ PDF**๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ง๋ฌธ์ ๋ตํฉ๋๋ค.")
|
| 85 |
|
| 86 |
query = gr.Textbox(
|
| 87 |
label="์ง๋ฌธ ์
๋ ฅ (ํ๊ตญ์ด/์์ด)",
|
| 88 |
+
placeholder="์: 5G ์ค๊ณ๊ธฐ ๋ถ์ํ ๋ฐ์ ์์ธ์?",
|
| 89 |
)
|
| 90 |
output = gr.Textbox(label="์๋ต", lines=10)
|
| 91 |
+
btn = gr.Button("๋ถ์ ์์")
|
| 92 |
|
| 93 |
btn.click(chatbot, inputs=query, outputs=output)
|
| 94 |
|