Spaces:
Sleeping
Sleeping
| # 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) | |