Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
MODEL_NAME = "microsoft/DialoGPT-small" # small,
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
|
@@ -39,53 +39,59 @@ Rules:
|
|
| 39 |
|
| 40 |
|
| 41 |
def build_prompt(message, history):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def respond(message, history):
|
| 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 |
-
return reply, history
|
| 82 |
|
| 83 |
|
| 84 |
demo = gr.ChatInterface(
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
MODEL_NAME = "microsoft/DialoGPT-small" # small, CPU-friendly
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def build_prompt(message, history):
|
| 42 |
+
"""
|
| 43 |
+
Gradio ChatInterface (type='messages') passes history as a list of
|
| 44 |
+
dicts like: {'role': 'user'|'assistant', 'content': '...'}.[web:120]
|
| 45 |
+
Turn that into a plain-text prompt for DialoGPT.
|
| 46 |
+
"""
|
| 47 |
+
lines = [f"System: {SHINCHAN_SYSTEM_PROMPT}"]
|
| 48 |
+
for turn in history:
|
| 49 |
+
role = turn.get("role")
|
| 50 |
+
content = turn.get("content", "")
|
| 51 |
+
if role == "user":
|
| 52 |
+
lines.append(f"User: {content}")
|
| 53 |
+
elif role == "assistant":
|
| 54 |
+
lines.append(f"Shinchan: {content}")
|
| 55 |
+
lines.append(f"User: {message}")
|
| 56 |
+
lines.append("Shinchan:")
|
| 57 |
+
return "\n".join(lines)
|
| 58 |
|
| 59 |
|
| 60 |
def respond(message, history):
|
| 61 |
+
"""
|
| 62 |
+
ChatInterface expects: fn(message:str, history:list[dict]) -> str or dict or list.[web:120]
|
| 63 |
+
Do NOT return (reply, history) here – that caused your 'tuple has no attribute get' error.
|
| 64 |
+
"""
|
| 65 |
+
prompt = build_prompt(message, history)
|
| 66 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 67 |
+
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
output_ids = model.generate(
|
| 70 |
+
**inputs,
|
| 71 |
+
max_new_tokens=80,
|
| 72 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 73 |
+
do_sample=True,
|
| 74 |
+
top_p=0.9,
|
| 75 |
+
temperature=0.9,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
full_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 79 |
+
|
| 80 |
+
if "Shinchan:" in full_text:
|
| 81 |
+
reply = full_text.split("Shinchan:")[-1].strip()
|
| 82 |
+
else:
|
| 83 |
+
reply = full_text.strip()
|
| 84 |
+
|
| 85 |
+
# IMPORTANT: return just the reply string
|
| 86 |
+
return reply
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
demo = gr.ChatInterface(
|
| 90 |
+
fn=respond,
|
| 91 |
+
type="messages", # explicit; default since Gradio 5 but makes intent clear[web:120]
|
| 92 |
+
title="Shinchan for Ruru",
|
| 93 |
+
description="Private Shinchan-style chat for Ruru.",
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|