seoulalpha / app.py
SyngyeonTak
changes to adapt to hugging face
1a554ac
# app.py
import gradio as gr
from langdetect import detect
from deep_translator import GoogleTranslator
# λͺ¨λ“ˆ import
from cluster_predictor import get_user_cluster
from region_extractor import extract_region_from_query
from rag_retriever import get_rag_recommendation
# μ–Έμ–΄ μ½”λ“œ λ§€ν•‘ (deep_translator ν˜Έν™˜)
LANG_CODE_MAP = {
"zh-cn": "zh-CN",
"zh-tw": "zh-TW",
"iw": "he",
}
def normalize_lang_code(code: str) -> str:
return LANG_CODE_MAP.get(code.lower(), code)
# --- Gradio용 λŒ€ν™” ν•¨μˆ˜ ---
def chatbot_interface(user_input, history, state):
if user_input.lower() in ["μ’…λ£Œ", "exit", "quit"]:
return history + [[user_input, "ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€."]], state
conversation_context = state.get("conversation_context", {})
full_conversation = state.get("full_conversation", [])
# --- Step1: μž…λ ₯ μ–Έμ–΄ 감지 & ν•œκ΅­μ–΄ λ²ˆμ—­ ---
try:
detected = detect(user_input) # 'en', 'ja', 'fr', 'zh-cn' ...
input_lang = normalize_lang_code(detected)
except Exception as e:
return history + [[user_input, f"❌ μ–Έμ–΄ 감지 였λ₯˜: {e}"]], state
if input_lang != "ko":
try:
current_query = GoogleTranslator(source=input_lang, target="ko").translate(user_input)
except Exception as e:
return history + [[user_input, f"❌ λ²ˆμ—­ 였λ₯˜: {e}"]], state
else:
current_query = user_input
cluster_info = None
max_turns = 3
# ν΄λŸ¬μŠ€ν„° ν™•μ • 루프
for turn in range(max_turns):
full_conversation.append(current_query)
status, data = get_user_cluster(current_query, conversation_context)
if status == "SUCCESS":
cluster_info = data
break
elif status == "RETRY_WITH_QUESTION":
question_to_user, updated_context = data
conversation_context = updated_context
# μ§ˆλ¬Έλ„ μž…λ ₯ μ–Έμ–΄λ‘œ λ²ˆμ—­ν•΄μ„œ μ‚¬μš©μžμ—κ²Œ λ³΄μ—¬μ€Œ
if input_lang != "ko":
try:
question_to_user = GoogleTranslator(source="ko", target=input_lang).translate(question_to_user)
except:
pass
# state μ—…λ°μ΄νŠΈ
state["conversation_context"] = conversation_context
state["full_conversation"] = full_conversation
return history + [[user_input, question_to_user]], state
elif status == "FAIL":
fail_msg = "μ΅œμ’… ν΄λŸ¬μŠ€ν„° 뢄석에 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."
if input_lang != "ko":
try:
fail_msg = GoogleTranslator(source="ko", target=input_lang).translate(fail_msg)
except:
pass
return history + [[user_input, fail_msg]], state
# RAG μ‹€ν–‰
if cluster_info:
cluster_id, cluster_profile = cluster_info
final_query_for_rag = " ".join(full_conversation)
region_keywords = extract_region_from_query(final_query_for_rag)
rag_query = f"{cluster_profile} νŠΉμ§•μ„ κ°€μ§„ 여행객이 '{final_query_for_rag}'와 같은 여행을 ν•  λ•Œ κ°€κΈ° 쒋은 κ³³"
final_answer_ko = get_rag_recommendation(rag_query, region_keywords)
# μ΅œμ’… 닡변도 μž…λ ₯ μ–Έμ–΄λ‘œ λ‹€μ‹œ λ²ˆμ—­
final_answer = final_answer_ko
if input_lang != "ko":
try:
final_answer = GoogleTranslator(source="ko", target=input_lang).translate(final_answer_ko)
except:
final_answer = f"❌ κ²°κ³Ό λ²ˆμ—­ 였λ₯˜: {final_answer_ko}"
# state μ—…λ°μ΄νŠΈ
state["conversation_context"] = conversation_context
state["full_conversation"] = full_conversation
return history + [[user_input, final_answer]], state
else:
fail_msg = "μΆ”μ²œμ„ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€."
if input_lang != "ko":
try:
fail_msg = GoogleTranslator(source="ko", target=input_lang).translate(fail_msg)
except:
pass
return history + [[user_input, fail_msg]], state
# --- Gradio UI μ •μ˜ ---
with gr.Blocks() as demo:
gr.Markdown("## ✈️ μ—¬ν–‰ μΆ”μ²œ 챗봇")
chatbot = gr.Chatbot(height=500)
msg = gr.Textbox(label="μ‚¬μš©μž μž…λ ₯")
state = gr.State({"conversation_context": {}, "full_conversation": []})
def respond(message, chat_history, state):
response, new_state = chatbot_interface(message, chat_history, state)
return "", response, new_state
msg.submit(respond, [msg, chatbot, state], [msg, chatbot, state])
if __name__ == "__main__":
demo.launch(show_api=False, debug=True)