Spaces:
Sleeping
Sleeping
File size: 4,698 Bytes
02f3fef 1a554ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # 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)
|