Spaces:
Running
Running
update app tab structure and live test
Browse files- app.py +20 -27
- flags.json +0 -11
- modules/case_loader.py +23 -0
- modules/ui_components.py +50 -0
- test_cases.json +100 -0
- utils_prompt.py β webtest_prompt.py +13 -6
app.py
CHANGED
|
@@ -1,29 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from inference import run_inference
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
# UI
|
| 6 |
def gradio_infer(npc_id, npc_location, player_utt):
|
| 7 |
prompt = build_webtest_prompt(npc_id, npc_location, player_utt)
|
| 8 |
result = run_inference(prompt)
|
| 9 |
return result["npc_output_text"], result["deltas"], result["flags_prob"]
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
# λͺ¨λΈ μ¬λ‘λ©μ© ν¨μ
|
| 24 |
-
def ping_reload():
|
| 25 |
-
reload_model(branch="latest") # latest λΈλμΉμμ μ¬λ€μ΄λ‘λ & λ‘λ
|
| 26 |
-
return {"status": "reloaded"}
|
| 27 |
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
gr.Markdown("## NPC Main Model Inference")
|
|
@@ -37,21 +31,20 @@ with gr.Blocks() as demo:
|
|
| 37 |
flags = gr.JSON(label="Flags Probabilities")
|
| 38 |
btn = gr.Button("Run Inference")
|
| 39 |
|
| 40 |
-
#
|
| 41 |
btn.click(
|
| 42 |
fn=gradio_infer,
|
| 43 |
inputs=[npc_id, npc_loc, player_utt],
|
| 44 |
-
outputs=[npc_resp, deltas, flags]
|
| 45 |
-
api_name="predict_main" # /api/predict_main μλν¬μΈνΈ μμ±
|
| 46 |
)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
gr.Button("
|
| 50 |
-
fn=
|
| 51 |
inputs=[],
|
| 52 |
outputs=[],
|
| 53 |
-
api_name="
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from inference import run_inference
|
| 3 |
+
from webtest_prompt import build_webtest_prompt
|
| 4 |
|
| 5 |
+
# Web Test UI νΈμΆ ν¨μ
|
| 6 |
def gradio_infer(npc_id, npc_location, player_utt):
|
| 7 |
prompt = build_webtest_prompt(npc_id, npc_location, player_utt)
|
| 8 |
result = run_inference(prompt)
|
| 9 |
return result["npc_output_text"], result["deltas"], result["flags_prob"]
|
| 10 |
|
| 11 |
+
# ping: μν νμΈ λ° κΉ¨μ°κΈ°
|
| 12 |
+
def ping():
|
| 13 |
+
# λͺ¨λΈμ΄ λ‘λλμ΄ μλμ§ νμΈ, μμΌλ©΄ λ‘λ
|
| 14 |
+
global wrapper, tokenizer, model, flags_order
|
| 15 |
+
if 'model' not in globals() or model is None:
|
| 16 |
+
from model_loader import ModelWrapper
|
| 17 |
+
wrapper = ModelWrapper()
|
| 18 |
+
tokenizer, model, flags_order = wrapper.get()
|
| 19 |
+
return {"status": "awake"}
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
gr.Markdown("## NPC Main Model Inference")
|
|
|
|
| 31 |
flags = gr.JSON(label="Flags Probabilities")
|
| 32 |
btn = gr.Button("Run Inference")
|
| 33 |
|
| 34 |
+
# Web Test μ μ© (api_name μ κ±°)
|
| 35 |
btn.click(
|
| 36 |
fn=gradio_infer,
|
| 37 |
inputs=[npc_id, npc_loc, player_utt],
|
| 38 |
+
outputs=[npc_resp, deltas, flags]
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# ping μλν¬μΈνΈ (μν νμΈ/κΉ¨μ°κΈ°)
|
| 42 |
+
gr.Button("Ping Server").click(
|
| 43 |
+
fn=ping,
|
| 44 |
inputs=[],
|
| 45 |
outputs=[],
|
| 46 |
+
api_name="ping"
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
flags.json
DELETED
|
@@ -1,11 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"ALL_FLAGS": [
|
| 3 |
-
"give_item",
|
| 4 |
-
"end_npc_main_story",
|
| 5 |
-
"quest_stage_change",
|
| 6 |
-
"change_game_state",
|
| 7 |
-
"change_player_state",
|
| 8 |
-
"npc_action",
|
| 9 |
-
"unlock_hidden_path"
|
| 10 |
-
]
|
| 11 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modules/case_loader.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json
|
| 2 |
+
from webtest_prompt import build_webtest_prompt
|
| 3 |
+
from inference import run_inference
|
| 4 |
+
|
| 5 |
+
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # modules/ μμ ν΄λ
|
| 6 |
+
TEST_CASES_PATH = os.path.join(BASE_DIR, "test_cases.json")
|
| 7 |
+
|
| 8 |
+
with open(TEST_CASES_PATH, "r", encoding="utf-8") as f:
|
| 9 |
+
TEST_CASES = json.load(f)
|
| 10 |
+
|
| 11 |
+
def get_case_names():
|
| 12 |
+
return [f"{i+1}. {c['description']}" for i, c in enumerate(TEST_CASES)]
|
| 13 |
+
|
| 14 |
+
def load_case(idx):
|
| 15 |
+
case = TEST_CASES[idx]
|
| 16 |
+
return json.dumps(case, ensure_ascii=False, indent=2), case["player_utterance"]
|
| 17 |
+
|
| 18 |
+
def run_case(idx, player_utt):
|
| 19 |
+
case = TEST_CASES[idx].copy()
|
| 20 |
+
case["player_utterance"] = player_utt
|
| 21 |
+
prompt = build_webtest_prompt(case["npc_id"], case["npc_location"], player_utt)
|
| 22 |
+
result = run_inference(prompt)
|
| 23 |
+
return result["npc_output_text"], result["deltas"], result["flags_prob"]
|
modules/ui_components.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from case_loader import get_case_names, load_case, run_case
|
| 3 |
+
|
| 4 |
+
def build_ui():
|
| 5 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple")) as demo:
|
| 6 |
+
# μλ¨ μκ°
|
| 7 |
+
gr.Markdown("""
|
| 8 |
+
# πΎ PersonaChatEngine HF-Serve
|
| 9 |
+
**κ²μ λ΄ NPC λ©μΈ λͺ¨λΈ μΆλ‘ μλ²**
|
| 10 |
+
Qwen 3B κΈ°λ° LoRA νμΈνλ λͺ¨λΈμ μ¬μ©νμ¬ NPC λμ¬λ₯Ό μμ±ν©λλ€.
|
| 11 |
+
""")
|
| 12 |
+
|
| 13 |
+
with gr.Row():
|
| 14 |
+
gr.Button("π μμΈ λ¬Έμ 보기",
|
| 15 |
+
link="https://huggingface.co/spaces/m97j/PersonaChatEngine_HF-serve/blob/main/README.md")
|
| 16 |
+
gr.Button("π» Colab ν
μ€νΈ μ΄κΈ°",
|
| 17 |
+
link="https://colab.research.google.com/drive/1_-qH8kdoU2Jj58TdaSnswHex-BFefInq?usp=sharing#scrollTo=cFJGv8BJ8oPD")
|
| 18 |
+
|
| 19 |
+
gr.Markdown("### π― ν
μ€νΈ μΌμ΄μ€ κΈ°λ° κ°λ¨ μ€ν")
|
| 20 |
+
|
| 21 |
+
with gr.Row():
|
| 22 |
+
case_dropdown = gr.Dropdown(choices=get_case_names(), label="ν
μ€νΈ μΌμ΄μ€ μ ν", value=get_case_names()[0])
|
| 23 |
+
load_btn = gr.Button("μΌμ΄μ€ λΆλ¬μ€κΈ°")
|
| 24 |
+
|
| 25 |
+
case_info = gr.Textbox(label="μΌμ΄μ€ μ 보", lines=10)
|
| 26 |
+
player_input = gr.Textbox(label="Player Utterance μμ ", lines=2)
|
| 27 |
+
|
| 28 |
+
run_btn = gr.Button("π Run Inference", variant="primary")
|
| 29 |
+
npc_resp = gr.Textbox(label="NPC Response")
|
| 30 |
+
deltas = gr.JSON(label="Deltas")
|
| 31 |
+
flags = gr.JSON(label="Flags Probabilities")
|
| 32 |
+
|
| 33 |
+
load_btn.click(
|
| 34 |
+
fn=lambda name: load_case(get_case_names().index(name)),
|
| 35 |
+
inputs=[case_dropdown],
|
| 36 |
+
outputs=[case_info, player_input]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
run_btn.click(
|
| 40 |
+
fn=lambda name, utt: run_case(get_case_names().index(name), utt),
|
| 41 |
+
inputs=[case_dropdown, player_input],
|
| 42 |
+
outputs=[npc_resp, deltas, flags]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
gr.Markdown("""
|
| 46 |
+
---
|
| 47 |
+
β οΈ **μ€μ κ²μ νμ΄νλΌμΈ ν
μ€νΈ**λ [ai-server Swagger](https://huggingface.co/spaces/m97j/PersonaChatEngine_ai_server)μμ μ§ννμΈμ.
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
return demo
|
test_cases.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"id": "case1",
|
| 4 |
+
"npc_id": "mother_abandoned_factory",
|
| 5 |
+
"npc_location": "map1",
|
| 6 |
+
"description": "ν곡μ₯μμ NPCμ λννλ μ₯λ©΄",
|
| 7 |
+
"player_utterance": "μ! 머리κ°!!! κ°μκΈ° κΈ°μ΅μ΄ λ μ¬λμ΄μ...",
|
| 8 |
+
"tags": {
|
| 9 |
+
"quest_stage": "in_progress",
|
| 10 |
+
"relationship": 0.35,
|
| 11 |
+
"trust": 0.35,
|
| 12 |
+
"npc_mood": "grief",
|
| 13 |
+
"player_reputation": "helpful",
|
| 14 |
+
"style": "emotional"
|
| 15 |
+
},
|
| 16 |
+
"lore": "μ΄ κ³΅μ₯μ μμ λ
μ νμ¬λ‘ νμλμλ€.",
|
| 17 |
+
"context": [
|
| 18 |
+
{"role": "player", "text": "μ¬μ€ μ΄ κ³΅μ₯μ λμλ€λλ©΄μ..."},
|
| 19 |
+
{"role": "npc", "text": "νΉμ κ·Έ νν°μ Jasonλ μμλμ..."}
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"id": "case2",
|
| 24 |
+
"npc_id": "blacksmith_village_center",
|
| 25 |
+
"npc_location": "village_square",
|
| 26 |
+
"description": "λ§μ λμ₯μ₯μ΄μ 무기 μ리μ λν΄ λννλ μ₯λ©΄",
|
| 27 |
+
"player_utterance": "μ΄ κ²μ λ€μ μΈ μ μκ² κ³ μ³μ€ μ μλμ?",
|
| 28 |
+
"tags": {
|
| 29 |
+
"quest_stage": "not_started",
|
| 30 |
+
"relationship": 0.2,
|
| 31 |
+
"trust": 0.4,
|
| 32 |
+
"npc_mood": "neutral",
|
| 33 |
+
"player_reputation": "unknown",
|
| 34 |
+
"style": "direct"
|
| 35 |
+
},
|
| 36 |
+
"lore": "λ§μμ λμ₯μ₯μ΄λ μΈλλ₯Ό μ΄μ΄ 무기λ₯Ό μ μν΄μλ€.",
|
| 37 |
+
"context": [
|
| 38 |
+
{"role": "npc", "text": "μ€, μ¬νμκ΅°. λ¬΄μ¨ μΌλ‘ μλ?"}
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"id": "case3",
|
| 43 |
+
"npc_id": "forest_hermit",
|
| 44 |
+
"npc_location": "deep_forest",
|
| 45 |
+
"description": "μ²μ μλμμ ν¬κ· μ½μ΄μ λν΄ λννλ μ₯λ©΄",
|
| 46 |
+
"player_utterance": "νΉμ μ΄ κ·Όμ²μμ νΈλ₯ΈλΉ μ½μ΄λ₯Ό λ³Έ μ μλμ?",
|
| 47 |
+
"tags": {
|
| 48 |
+
"quest_stage": "in_progress",
|
| 49 |
+
"relationship": 0.5,
|
| 50 |
+
"trust": 0.6,
|
| 51 |
+
"npc_mood": "curious",
|
| 52 |
+
"player_reputation": "friendly",
|
| 53 |
+
"style": "polite"
|
| 54 |
+
},
|
| 55 |
+
"lore": "μλμλ μ²μ κΉμ κ³³μμ μ½μ΄μ λ²μ―μ μ°κ΅¬νλ€.",
|
| 56 |
+
"context": [
|
| 57 |
+
{"role": "player", "text": "μλ
νμΈμ, νΉμ μ μ μ΄μΌκΈ° λλ μ μμκΉμ?"},
|
| 58 |
+
{"role": "npc", "text": "μ¬κΈ°κΉμ§ μ€λ μ¬λμ λλ¬Όμ§μ."}
|
| 59 |
+
]
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"id": "case4",
|
| 63 |
+
"npc_id": "captain_port_authority",
|
| 64 |
+
"npc_location": "harbor",
|
| 65 |
+
"description": "νꡬ κ΄λ¦¬κ΄κ³Ό μΆν νκ°μ λν΄ λννλ μ₯λ©΄",
|
| 66 |
+
"player_utterance": "μ΄ λ°°λ₯Ό μ€λ μμ μΆνμμΌμΌ ν©λλ€. νκ°λ₯Ό λΆνλ립λλ€.",
|
| 67 |
+
"tags": {
|
| 68 |
+
"quest_stage": "urgent",
|
| 69 |
+
"relationship": 0.45,
|
| 70 |
+
"trust": 0.3,
|
| 71 |
+
"npc_mood": "suspicious",
|
| 72 |
+
"player_reputation": "neutral",
|
| 73 |
+
"style": "persuasive"
|
| 74 |
+
},
|
| 75 |
+
"lore": "νꡬ κ΄λ¦¬κ΄μ λͺ¨λ μ λ°μ μΆνμ μ격ν κ΄λ¦¬νλ€.",
|
| 76 |
+
"context": [
|
| 77 |
+
{"role": "npc", "text": "μλ₯λ λ€ μ€λΉλλ?"}
|
| 78 |
+
]
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"id": "case5",
|
| 82 |
+
"npc_id": "young_apprentice_mage",
|
| 83 |
+
"npc_location": "mage_tower_library",
|
| 84 |
+
"description": "λ§λ²μ¬ 견μ΅μκ³Ό κ³ λ μ£Όλ¬Έμμ λν΄ λννλ μ₯λ©΄",
|
| 85 |
+
"player_utterance": "μ΄ μ£Όλ¬Έμμ μ ν λ¬Έμ₯μ ν΄μν μ μλμ?",
|
| 86 |
+
"tags": {
|
| 87 |
+
"quest_stage": "research",
|
| 88 |
+
"relationship": 0.6,
|
| 89 |
+
"trust": 0.7,
|
| 90 |
+
"npc_mood": "excited",
|
| 91 |
+
"player_reputation": "scholar",
|
| 92 |
+
"style": "inquisitive"
|
| 93 |
+
},
|
| 94 |
+
"lore": "λ§λ²μ¬ νμ λμκ΄μλ μλ°± λ
λ κ³ μλ€μ΄ 보κ΄λμ΄ μλ€.",
|
| 95 |
+
"context": [
|
| 96 |
+
{"role": "player", "text": "μ΄ μ±
μ κ΅μ₯ν μ€λλ κ² κ°μμ."},
|
| 97 |
+
{"role": "npc", "text": "λ§μμ! μ΄λ° 건 μ λ§ λλ¬Όμ£ ."}
|
| 98 |
+
]
|
| 99 |
+
}
|
| 100 |
+
]
|
utils_prompt.py β webtest_prompt.py
RENAMED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
|
| 3 |
def build_webtest_prompt(npc_id: str, npc_location: str, player_utt: str) -> str:
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
pre = {
|
| 6 |
"tags": {
|
| 7 |
"npc_id": npc_id,
|
|
@@ -14,15 +18,18 @@ def build_webtest_prompt(npc_id: str, npc_location: str, player_utt: str) -> str
|
|
| 14 |
"style": ""
|
| 15 |
},
|
| 16 |
"player_state": {},
|
| 17 |
-
"rag_main_docs": [],
|
| 18 |
-
"context": [],
|
| 19 |
"player_utterance": player_utt
|
| 20 |
}
|
| 21 |
-
|
| 22 |
-
return build_main_prompt(pre, session_id="", npc_id=npc_id)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
def build_main_prompt(pre: Dict[str, Any], session_id: str, npc_id: str) -> str:
|
| 26 |
tags = pre.get("tags", {})
|
| 27 |
ps = pre.get("player_state", {})
|
| 28 |
rag_docs = pre.get("rag_main_docs", [])
|
|
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
|
| 3 |
def build_webtest_prompt(npc_id: str, npc_location: str, player_utt: str) -> str:
|
| 4 |
+
"""
|
| 5 |
+
Web Test μ μ©: μ΅μ μ
λ ₯κ°(NPC ID, Location, Player λ°ν)μΌλ‘
|
| 6 |
+
λͺ¨λΈ νμ΅ ν¬λ§·μ λ§λ prompt λ¬Έμμ΄μ μμ±.
|
| 7 |
+
μ€μ API/κ²μ μλΉμ€ κ²½λ‘μμλ μ¬μ©νμ§ μμ.
|
| 8 |
+
"""
|
| 9 |
pre = {
|
| 10 |
"tags": {
|
| 11 |
"npc_id": npc_id,
|
|
|
|
| 18 |
"style": ""
|
| 19 |
},
|
| 20 |
"player_state": {},
|
| 21 |
+
"rag_main_docs": [],
|
| 22 |
+
"context": [],
|
| 23 |
"player_utterance": player_utt
|
| 24 |
}
|
| 25 |
+
return _assemble_prompt_for_model(pre)
|
|
|
|
| 26 |
|
| 27 |
+
def _assemble_prompt_for_model(pre: Dict[str, Any]) -> str:
|
| 28 |
+
"""
|
| 29 |
+
Web Test μ μ© λ΄λΆ ν¨μ:
|
| 30 |
+
pre dict β λͺ¨λΈ μ
λ ₯ ν¬λ§· λ¬Έμμ΄(<SYS>~<NPC>)
|
| 31 |
+
"""
|
| 32 |
|
|
|
|
| 33 |
tags = pre.get("tags", {})
|
| 34 |
ps = pre.get("player_state", {})
|
| 35 |
rag_docs = pre.get("rag_main_docs", [])
|