SIF2025_demo / app.py
Kyudan
buttons
2cc938b
raw
history blame
2.35 kB
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 = "./my_faiss_index"
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 eng_trans == False:
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)
# question๊ณผ context๋ฅผ message์— ์‚ฝ์ž…
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
demo = gr.Interface(fn=chatbot,
inputs=[gr.Textbox(label="์งˆ๋ฌธ ์ž…๋ ฅ"), # ํ…์ŠคํŠธ ์ž…๋ ฅ
gr.Checkbox(label="์˜์–ด ๋ฒˆ์—ญ ์‚ฌ์šฉ ์—ฌ๋ถ€", value=True), # ์ฒดํฌ๋ฐ•์Šค ์ถ”๊ฐ€
gr.Slider(1,5, value=3, step=1),
], outputs="text")
demo.launch()