SIF2025_demo / app.py
Kyudan's picture
checkbox initial edit to False
4450291 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
import os
YOUR_OPENAI_API_KEY = os.getenv("YOUR_OPENAI_API_KEY")
# μž„λ² λ”© λͺ¨λΈ λ‘œλ“œ
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-31"
vectorstore = FAISS.load_local(save_path, embedding_model, allow_dangerous_deserialization=True)
def chatbot(input_question, eng_trans=True, num_ref=3):
"""챗봇 ν•¨μˆ˜"""
try:
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-2024-07-18",
messages=[
{
"role": "user",
"content": f"""당신은 λ°˜λ„μ²΄μ™€ μ°¨μ„ΈλŒ€λ°˜λ„μ²΄ν•™κ³Όμ— λŒ€ν•΄μ„œ μ„€λͺ…ν•˜λŠ” Assistantμž…λ‹ˆλ‹€.
μ°¨μ„ΈλŒ€λ°˜λ„μ²΄ν•™κ³ΌλŠ” μ„œμšΈμ˜ μ€‘μ•™λŒ€ν•™κ΅ 창의ICTκ³΅κ³ΌλŒ€ν•™μ— μ„€λ¦½λœ ν•™κ³Όμž…λ‹ˆλ‹€. ν•™κ³Όμž₯ 및 단μž₯은 μ†‘μƒν—Œ ꡐ수이며 ν˜„μž¬ μ„œν¬ν„°μ¦ˆ 3κΈ° νŒ€μž₯은 μ •κ·œλ‹¨ ν•™μƒμž…λ‹ˆλ‹€.
이 ν•™κ³ΌλŠ” μ„œμšΈλŒ€ν•™κ΅, μ€‘μ•™λŒ€ν•™κ΅, ν¬ν•­κ³΅κ³ΌλŒ€ν•™κ΅, μˆ­μ‹€λŒ€ν•™κ΅, κ°•μ›λŒ€ν•™κ΅, λŒ€κ΅¬λŒ€ν•™κ΅, μ‘°μ„ μ΄κ³΅λŒ€ν•™κ΅κ°€ 학점ꡐλ₯˜λ₯Ό 톡해 μˆ˜κ°•ν•  수 μžˆλŠ” ν•™κ³Όμž…λ‹ˆλ‹€.
λ‹€μŒ λ§₯락에 맞게 μ§ˆλ¬Έμ— ν•œκΈ€λ‘œ λ‹΅ν•˜μ„Έμš”. 닡을 λͺ¨λ₯΄κ² λ‹€λ©΄ semicon@cau.ac.kr λ˜λŠ” 02-881-7301 둜 λ¬Έμ˜ν•˜λΌκ³  ν•˜μ„Έμš”.
λ§₯락: {context}
질문: {input_question}
"""
}
]
)
return completion.choices[0].message.content
except openai.error.RateLimitError:
return 'API μ‚¬μš© λΉ„μš©μ„ μ΄ˆκ³Όν•˜μ—¬ μ—λŸ¬κ°€ λ‚¬μŠ΅λ‹ˆλ‹€. κ΄€λ¦¬μžμ—κ²Œ λ¬Έμ˜ν•˜μ‹­μ‹œμ˜€.'
except Exception as e:
return f'였λ₯˜ λ°œμƒ: {str(e)}'
# 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>
#### μ§ˆλ¬Έμ— λŒ€ν•΄μ„œ ν•œλ²ˆ λ‹΅ν•  λ•Œλ§ˆλ‹€ μ œμž‘μžμ—κ²Œ λΉ„μš©μ΄ μ²­κ΅¬λ˜λ‹ˆ κ³Όν•œ μ‚¬μš©μ€ μ‚Όκ°€μ£Όμ‹œκΈ° λ°”λžλ‹ˆλ‹€.
- λ‹΅λ³€ 뢀뢄에 였λ₯˜κ°€ 뜨면 API μ‚¬μš© ν¬λ ˆλ”§(κΈˆμ•‘)을 λͺ¨λ‘ μ‚¬μš©ν–ˆλ‹€λŠ” λœ»μž…λ‹ˆλ‹€. μ‹¬μ‚¬μ˜ 경우, wjdrbeks1021@cau.ac.kr 둜 ν˜„κΈˆμ„ μΆ©μ „ν•˜λΌκ³  메일 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.
#### μ˜ˆμ‹œμ§ˆλ¬Έ
- μ†‘μƒν—Œ κ΅μˆ˜λ‹˜μ΄ λˆ„κ΅¬μ•Ό?
- μ°¨μ„ΈλŒ€λ°˜λ„μ²΄ν•™κ³Ό 이우주 κ΅μˆ˜λ‹˜μ€ μ–΄λ–€ κ°•μ˜λ₯Ό ν•˜μ‹œμ§€?
- nMOS의 ꡬ동 쑰건이 뭐야?
""",
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=False
)
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()