Spaces:
Running
Running
File size: 5,258 Bytes
2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 1e828d1 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 bbb241c 2249ab6 |
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 |
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
|