Spaces:
Running
Running
| import gradio as gr | |
| from .case_loader import load_case, run_case | |
| # μμ°μ© μΌμ΄μ€ μ΄λ¦ νλμ½λ© | |
| CASE_NAMES = [ | |
| "ν곡μ₯μμ NPCμ λννλ μ₯λ©΄", | |
| "λ§μ λμ₯μ₯μ΄μ 무기 μ리μ λν΄ λννλ μ₯λ©΄", | |
| "μ²μ μλμμ ν¬κ· μ½μ΄μ λν΄ λννλ μ₯λ©΄", | |
| "νꡬ κ΄λ¦¬κ΄κ³Ό μΆν νκ°μ λν΄ λννλ μ₯λ©΄", | |
| "λ§λ²μ¬ 견μ΅μκ³Ό κ³ λ μ£Όλ¬Έμμ λν΄ λννλ μ₯λ©΄" | |
| ] | |
| def format_case_info(case: dict) -> dict: | |
| """μΌμ΄μ€ μ 보λ₯Ό 보기 μ’κ² μ 리ν΄μ λ°ν""" | |
| inp = case["input"] | |
| tags = inp.get("tags", {}) | |
| context_lines = [f"{h['role'].upper()}: {h['text']}" for h in inp.get("context", [])] | |
| return { | |
| "npc_id": inp.get("npc_id", ""), | |
| "npc_location": inp.get("npc_location", ""), | |
| "quest_stage": tags.get("quest_stage", ""), | |
| "relationship": tags.get("relationship", ""), | |
| "trust": tags.get("trust", ""), | |
| "npc_mood": tags.get("npc_mood", ""), | |
| "player_reputation": tags.get("player_reputation", ""), | |
| "style": tags.get("style", ""), | |
| "lore": inp.get("lore", ""), | |
| "description": inp.get("description", ""), | |
| "player_state": inp.get("player_state", {}), | |
| "context": "\n".join(context_lines), | |
| "player_utterance": inp.get("player_utterance", "") | |
| } | |
| def build_ui(): | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple")) as demo: | |
| gr.Markdown(""" | |
| # πΎ PersonaChatEngine HF-Serve | |
| **κ²μ λ΄ NPC λ©μΈ λͺ¨λΈ μΆλ‘ μλ²** | |
| Qwen 3B κΈ°λ° LoRA νμΈνλ λͺ¨λΈμ μ¬μ©νμ¬ NPC λμ¬λ₯Ό μμ±ν©λλ€. | |
| """) | |
| with gr.Row(): | |
| gr.Button("π μμΈ λ¬Έμ 보기", | |
| link="https://huggingface.co/spaces/m97j/PersonaChatEngine_HF-serve/blob/main/README.md") | |
| gr.Button("π» Colab λ ΈνΈλΆ μ΄κΈ°", | |
| link="https://colab.research.google.com/drive/1_-qH8kdoU2Jj58TdaSnswHex-BFefInq?usp=sharing#scrollTo=cFJGv8BJ8oPD") | |
| gr.Markdown("### π― ν μ€νΈ μΌμ΄μ€ κΈ°λ° κ°λ¨ μ€ν") | |
| gr.Markdown("β οΈ μΆλ‘ μλ μ μ΄ ~ μ΅λ 1λΆ μ λ μμλ μ μμ΅λλ€. μ μλ§ κΈ°λ€λ €μ£ΌμΈμ.") | |
| with gr.Row(): | |
| case_dropdown = gr.Dropdown(choices=CASE_NAMES, label="ν μ€νΈ μΌμ΄μ€ μ ν", value=CASE_NAMES[0]) | |
| load_btn = gr.Button("μΌμ΄μ€ λΆλ¬μ€κΈ°") | |
| # μΌμ΄μ€ μ 보 νμ μμ | |
| with gr.Row(): | |
| with gr.Column(): | |
| npc_id = gr.Textbox(label="NPC ID", interactive=False) | |
| npc_loc = gr.Textbox(label="NPC Location", interactive=False) | |
| quest_stage = gr.Textbox(label="Quest Stage", interactive=False) | |
| relationship = gr.Textbox(label="Relationship", interactive=False) | |
| trust = gr.Textbox(label="Trust", interactive=False) | |
| npc_mood = gr.Textbox(label="NPC Mood", interactive=False) | |
| player_rep = gr.Textbox(label="Player Reputation", interactive=False) | |
| style = gr.Textbox(label="Style", interactive=False) | |
| with gr.Column(): | |
| lore = gr.Textbox(label="Lore", lines=3, interactive=False) | |
| desc = gr.Textbox(label="Description", lines=3, interactive=False) | |
| player_state = gr.JSON(label="Player State") | |
| context = gr.Textbox(label="Context", lines=6, interactive=False) | |
| # Player Utteranceλ λ³λ μ λ ₯μ°½ | |
| player_input = gr.Textbox(label="Player Utterance", lines=2) | |
| run_btn = gr.Button("π Run Inference", variant="primary") | |
| npc_resp = gr.Textbox(label="NPC Response") | |
| deltas = gr.JSON(label="Deltas") | |
| flags = gr.JSON(label="Flags Probabilities") | |
| # μΌμ΄μ€ λΆλ¬μ€κΈ° λμ | |
| def on_load_case(name): | |
| idx = CASE_NAMES.index(name) | |
| case = load_case(idx) # TEST_CASES μ§μ μ κ·Ό λμ load_case μ¬μ© | |
| info = format_case_info(case) | |
| return ( | |
| info["npc_id"], info["npc_location"], info["quest_stage"], | |
| info["relationship"], info["trust"], info["npc_mood"], | |
| info["player_reputation"], info["style"], info["lore"], | |
| info["description"], info["player_state"], info["context"], | |
| info["player_utterance"] | |
| ) | |
| load_btn.click( | |
| fn=on_load_case, | |
| inputs=[case_dropdown], | |
| outputs=[ | |
| npc_id, npc_loc, quest_stage, relationship, trust, | |
| npc_mood, player_rep, style, lore, desc, player_state, context, | |
| player_input | |
| ] | |
| ) | |
| # μΆλ‘ μ€ν | |
| run_btn.click( | |
| fn=lambda name, utt: run_case(CASE_NAMES.index(name), utt), | |
| inputs=[case_dropdown, player_input], | |
| outputs=[npc_resp, deltas, flags] | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| β οΈ **μ€μ κ²μ νμ΄νλΌμΈ ν μ€νΈ**λ [ai-server Swagger](https://huggingface.co/spaces/m97j/PersonaChatEngine_ai_server)μμ μ§ννμΈμ. | |
| """) | |
| return demo | |