SIF2025_demo / app.py
Kyudan's picture
Update app.py
99909af verified
import gradio as gr
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from openai import OpenAI
from translate_utils import translate_ko_to_en
YOUR_OPENAI_API_KEY = "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"
# ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ ๋กœ๋“œ
embedding_model_name = "snunlp/KR-SBERT-V40K-klueNLI-augSTS"
embedding_model = HuggingFaceEmbeddings(
model_name=embedding_model_name,
model_kwargs={"device": "cpu"}, # GPU ์‚ฌ์šฉ ์‹œ "cuda"๋กœ ๋ณ€๊ฒฝ
encode_kwargs={"normalize_embeddings": True},
)
# vector DB ๋กœ๋“œ
save_path = "./version-2024-12-22"
vectorstore = FAISS.load_local(save_path, embedding_model, allow_dangerous_deserialization=True)
def chatbot(input_question, eng_trans=True, num_ref=3):
"""์ฑ—๋ด‡ ํ•จ์ˆ˜"""
retriever = vectorstore.as_retriever(search_kwargs={"k": num_ref})
# ํ•œ๊ตญ์–ด๋กœ ์งˆ์˜
if not eng_trans:
basic_docs = retriever.invoke(input_question)
# ์˜์–ด๋กœ ๋ฒˆ์—ญ ํ›„ ์งˆ์˜(์˜์–ด, ํ•œ๊ธ€ ๋‘˜ ๋‹ค ๊ฒ€์ƒ‰)
else:
eng = translate_ko_to_en(input_question)
basic_docs = retriever.invoke(input_question)
eng_docs = retriever.invoke(eng)
basic_docs = basic_docs + eng_docs
context = "\n".join([doc.page_content for doc in basic_docs])
client = OpenAI(api_key=YOUR_OPENAI_API_KEY)
# GPT-4 or GPT-4o-mini ๋“ฑ ๋ชจ๋ธ ์ง€์ •
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": f"""๋‹น์‹ ์€ ๋ฐ˜๋„์ฒด์™€ ์ฐจ์„ธ๋Œ€๋ฐ˜๋„์ฒดํ•™๊ณผ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๋Š” Assistant์ž…๋‹ˆ๋‹ค.
์ฐจ์„ธ๋Œ€๋ฐ˜๋„์ฒดํ•™๊ณผ๋Š” ํ•œ๊ตญ ์„œ์šธ์˜ ์ค‘์•™๋Œ€ํ•™๊ต์˜ ์ฐฝ์˜ICT๊ณต๊ณผ๋Œ€ํ•™์— ์„ค๋ฆฝ๋œ ํ•™๊ณผ์ž…๋‹ˆ๋‹ค. ์ด ํ•™๊ณผ๋Š” ์„œ์šธ๋Œ€ํ•™๊ต, ์ค‘์•™๋Œ€ํ•™๊ต, ํฌํ•ญ๊ณต๊ณผ๋Œ€ํ•™๊ต, ์ˆญ์‹ค๋Œ€ํ•™๊ต, ๊ฐ•์›๋Œ€ํ•™๊ต, ๋Œ€๊ตฌ๋Œ€ํ•™๊ต, ์กฐ์„ ์ด๊ณต๋Œ€ํ•™๊ต๊ฐ€ ํ•™์ ๊ต๋ฅ˜๋ฅผ ํ†ตํ•ด ์ˆ˜๊ฐ•ํ•  ์ˆ˜ ์žˆ๋Š” ํ•™๊ณผ์ž…๋‹ˆ๋‹ค.
๋‹ค์Œ ๋งฅ๋ฝ์— ๋งž๊ฒŒ ์งˆ๋ฌธ์— ํ•œ๊ธ€๋กœ ๋‹ตํ•˜์„ธ์š”. ๋Œ€์‹ , ๋ฐ˜๋„์ฒด ์ „๋ฌธ์šฉ์–ด๋Š” ์˜์–ด๋กœ ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.
๋งฅ๋ฝ: {context}
์งˆ๋ฌธ: {input_question}
"""
}
]
)
return completion.choices[0].message.content
# Gradio Blocks ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๊พธ๋ฏผ ๋ฒ„์ „
with gr.Blocks() as demo:
# ์ƒ๋‹จ ์ด๋ฏธ์ง€
gr.Image(
value="head.png", # ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ ๋˜๋Š” URL
elem_id="top-image",
label=None
)
# ์•ˆ๋‚ด ๋ฌธ๊ตฌ
gr.Markdown(
"""
# ์ฐจ์„ธ๋Œ€๋ฐ˜๋„์ฒดํ•™๊ณผ ํŠนํ™” ์ฑ—๋ด‡
- ์ด ์ฑ—๋ด‡์€ ํ•œ๊ตญ์–ด ์งˆ์˜์— ๋Œ€ํ•ด ๋ฐ˜๋„์ฒด ๊ด€๋ จ ์ •๋ณด, ์ฐจ์„ธ๋Œ€๋ฐ˜๋„์ฒดํ•™๊ณผ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์นœ์ ˆํ•˜๊ฒŒ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.<br>
- ๋ฐ˜๋„์ฒด ์ „๋ฌธ์šฉ์–ด๋Š” ์ผ๋ถ€ ์˜์–ด๋กœ ๋‹ต๋ณ€๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.<br>
### ์˜์–ด ๋ฒˆ์—ญ ์‚ฌ์šฉ ์—ฌ๋ถ€ ๊ธฐ๋Šฅ
- ์˜์–ด ๋ฒˆ์—ญ ์‚ฌ์šฉ ์—ฌ๋ถ€๋ฅผ ํ‚ค๋ฉด, ์ •ํ™•๋„๊ฐ€ ์ƒ์Šน๋  ์ˆ˜ ์žˆ์œผ๋‚˜, ์ถ”๋ก  ์‹œ๊ฐ„์ด ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <br>
### ๊ฒ€์ƒ‰ ๋ฌธ์„œ ๊ฐœ์ˆ˜ ๋ณ€๊ฒฝ ๊ธฐ๋Šฅ
๊ฒ€์ƒ‰ํ•  ๋ฌธ์„œ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๋ฉด ์ •ํ™•๋„๊ฐ€ ์ƒ์Šน๋  ์ˆ˜ ์žˆ์œผ๋‚˜, ์ถ”๋ก  ์‹œ๊ฐ„์ด ๊ธธ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <br>
""",
elem_id="description"
)
# ๋ฉ”์ธ UI
with gr.Group():
with gr.Row():
# ์ž…๋ ฅ ํŒŒํŠธ
with gr.Column():
input_question = gr.Textbox(
label="์งˆ๋ฌธ ์ž…๋ ฅ",
placeholder="๋ฐ˜๋„์ฒด์™€ ์ฐจ์„ธ๋Œ€๋ฐ˜๋„์ฒดํ•™๊ณผ์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•œ ์ ์„ ์ž…๋ ฅํ•˜์„ธ์š”."
)
eng_trans = gr.Checkbox(
label="์˜์–ด ๋ฒˆ์—ญ ์‚ฌ์šฉ ์—ฌ๋ถ€",
value=True
)
num_ref = gr.Slider(
minimum=1,
maximum=5,
value=3,
step=1,
label="๊ฒ€์ƒ‰ํ•  ๋ฌธ์„œ ๊ฐœ์ˆ˜"
)
submit_btn = gr.Button("์งˆ๋ฌธํ•˜๊ธฐ")
# ์ถœ๋ ฅ ํŒŒํŠธ
with gr.Column():
output_answer = gr.Textbox(
label="๋‹ต๋ณ€",
placeholder="๋‹ต๋ณ€์ด ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค...",
lines=10
)
# ๋ฒ„ํŠผ๊ณผ ํ•จ์ˆ˜ ์—ฐ๊ฒฐ
submit_btn.click(
fn=chatbot,
inputs=[input_question, eng_trans, num_ref],
outputs=output_answer
)
demo.launch()