Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from groq import Groq
|
|
| 10 |
os.environ["GROQ_API_KEY"] = "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"
|
| 11 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 12 |
|
| 13 |
-
# ✅ 2. PDF 파일 경로 리스트
|
| 14 |
pdf_paths = [
|
| 15 |
"pdfs/의왕단오축제 _ 의왕문화원.pdf",
|
| 16 |
"pdfs/성인 문화학교 _ 의왕문화원.pdf",
|
|
@@ -20,7 +20,7 @@ pdf_paths = [
|
|
| 20 |
"pdfs/어린이 문화학교 _ 의왕문화원.pdf"
|
| 21 |
]
|
| 22 |
|
| 23 |
-
# ✅ 3.
|
| 24 |
def extract_texts_from_pdfs(pdf_paths):
|
| 25 |
chunks = []
|
| 26 |
for path in pdf_paths:
|
|
@@ -40,7 +40,7 @@ def extract_texts_from_pdfs(pdf_paths):
|
|
| 40 |
|
| 41 |
docs = extract_texts_from_pdfs(pdf_paths)
|
| 42 |
|
| 43 |
-
# ✅ 4.
|
| 44 |
embed_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
|
| 45 |
texts = [doc["text"] for doc in docs]
|
| 46 |
sources = [doc["source"] for doc in docs]
|
|
@@ -49,7 +49,7 @@ embeddings = embed_model.encode(texts, convert_to_numpy=True, show_progress_bar=
|
|
| 49 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 50 |
index.add(np.array(embeddings))
|
| 51 |
|
| 52 |
-
# ✅ 5. 유사 문서 검색
|
| 53 |
def search_similar_docs(query, top_k=3):
|
| 54 |
query_emb = embed_model.encode([query])[0]
|
| 55 |
scores, indices = index.search(np.array([query_emb]), top_k)
|
|
@@ -58,7 +58,7 @@ def search_similar_docs(query, top_k=3):
|
|
| 58 |
results.append(docs[idx]["text"])
|
| 59 |
return "\n\n".join(results)
|
| 60 |
|
| 61 |
-
# ✅ 6. Groq
|
| 62 |
def ask_with_groq(question, context):
|
| 63 |
response = client.chat.completions.create(
|
| 64 |
model="llama3-8b-8192",
|
|
@@ -69,13 +69,13 @@ def ask_with_groq(question, context):
|
|
| 69 |
)
|
| 70 |
return response.choices[0].message.content
|
| 71 |
|
| 72 |
-
# ✅ 7.
|
| 73 |
def chatbot_interface(question):
|
| 74 |
context = search_similar_docs(question)
|
| 75 |
answer = ask_with_groq(question, context)
|
| 76 |
return answer
|
| 77 |
|
| 78 |
-
# ✅ 8.
|
| 79 |
suggested_questions = [
|
| 80 |
"의왕단오축제는 언제 열리나요?",
|
| 81 |
"성인 문화학교에서 제공하는 수업 종류는 무엇인가요?",
|
|
@@ -84,27 +84,31 @@ suggested_questions = [
|
|
| 84 |
"횡성축제의 개최 시기와 일정은 어떻게 되나요?",
|
| 85 |
]
|
| 86 |
|
| 87 |
-
# ✅ 9. Gradio UI
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
gr.Markdown("# 📘 오아시스 문서 기반 문화 프로그램 챗봇 🤖 '뮤지스(Musesis)'")
|
| 90 |
gr.Markdown("질문을 입력하면 유사 문서를 검색해 답변해줍니다.")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
btn.click(fn=chatbot_interface, inputs=gr.Textbox(value=q, visible=False), outputs=answer_box)
|
| 105 |
|
|
|
|
| 106 |
submit_btn.click(fn=chatbot_interface, inputs=question_box, outputs=answer_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
# ✅ 10.
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
demo.launch()
|
|
|
|
| 10 |
os.environ["GROQ_API_KEY"] = "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"
|
| 11 |
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 12 |
|
| 13 |
+
# ✅ 2. PDF 파일 경로 리스트
|
| 14 |
pdf_paths = [
|
| 15 |
"pdfs/의왕단오축제 _ 의왕문화원.pdf",
|
| 16 |
"pdfs/성인 문화학교 _ 의왕문화원.pdf",
|
|
|
|
| 20 |
"pdfs/어린이 문화학교 _ 의왕문화원.pdf"
|
| 21 |
]
|
| 22 |
|
| 23 |
+
# ✅ 3. 텍스트 추출
|
| 24 |
def extract_texts_from_pdfs(pdf_paths):
|
| 25 |
chunks = []
|
| 26 |
for path in pdf_paths:
|
|
|
|
| 40 |
|
| 41 |
docs = extract_texts_from_pdfs(pdf_paths)
|
| 42 |
|
| 43 |
+
# ✅ 4. 임베딩 및 FAISS 색인
|
| 44 |
embed_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
|
| 45 |
texts = [doc["text"] for doc in docs]
|
| 46 |
sources = [doc["source"] for doc in docs]
|
|
|
|
| 49 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 50 |
index.add(np.array(embeddings))
|
| 51 |
|
| 52 |
+
# ✅ 5. 유사 문서 검색
|
| 53 |
def search_similar_docs(query, top_k=3):
|
| 54 |
query_emb = embed_model.encode([query])[0]
|
| 55 |
scores, indices = index.search(np.array([query_emb]), top_k)
|
|
|
|
| 58 |
results.append(docs[idx]["text"])
|
| 59 |
return "\n\n".join(results)
|
| 60 |
|
| 61 |
+
# ✅ 6. Groq 응답
|
| 62 |
def ask_with_groq(question, context):
|
| 63 |
response = client.chat.completions.create(
|
| 64 |
model="llama3-8b-8192",
|
|
|
|
| 69 |
)
|
| 70 |
return response.choices[0].message.content
|
| 71 |
|
| 72 |
+
# ✅ 7. 챗봇 인터페이스
|
| 73 |
def chatbot_interface(question):
|
| 74 |
context = search_similar_docs(question)
|
| 75 |
answer = ask_with_groq(question, context)
|
| 76 |
return answer
|
| 77 |
|
| 78 |
+
# ✅ 8. 추천 질문 리스트
|
| 79 |
suggested_questions = [
|
| 80 |
"의왕단오축제는 언제 열리나요?",
|
| 81 |
"성인 문화학교에서 제공하는 수업 종류는 무엇인가요?",
|
|
|
|
| 84 |
"횡성축제의 개최 시기와 일정은 어떻게 되나요?",
|
| 85 |
]
|
| 86 |
|
| 87 |
+
# ✅ 9. Gradio UI
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
gr.Markdown("# 📘 오아시스 문서 기반 문화 프로그램 챗봇 🤖 '뮤지스(Musesis)'")
|
| 90 |
gr.Markdown("질문을 입력하면 유사 문서를 검색해 답변해줍니다.")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
+
with gr.Column(scale=3):
|
| 94 |
+
question_box = gr.Textbox(placeholder="질문을 입력하세요", label="질문", lines=1)
|
| 95 |
+
with gr.Row():
|
| 96 |
+
submit_btn = gr.Button("질문하기")
|
| 97 |
+
clear_btn = gr.Button("기록 지우기")
|
| 98 |
+
answer_box = gr.Textbox(label="답변", lines=10)
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
with gr.Accordion("💡 추천 질문 보기 (펼치기/접기)", open=False):
|
| 101 |
+
for q in suggested_questions:
|
| 102 |
+
btn = gr.Button(q, scale=1)
|
| 103 |
+
btn.click(fn=chatbot_interface, inputs=gr.Textbox(value=q, visible=False), outputs=answer_box)
|
|
|
|
| 104 |
|
| 105 |
+
# ✅ 버튼 클릭 및 Enter로 질문 실행
|
| 106 |
submit_btn.click(fn=chatbot_interface, inputs=question_box, outputs=answer_box)
|
| 107 |
+
question_box.submit(fn=chatbot_interface, inputs=question_box, outputs=answer_box)
|
| 108 |
+
|
| 109 |
+
# ✅ 기록 지우기
|
| 110 |
+
clear_btn.click(lambda: ("", ""), inputs=[], outputs=[question_box, answer_box])
|
| 111 |
|
| 112 |
+
# ✅ 10. 실행
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|