Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
SHINCHAN_SYSTEM_PROMPT = """
|
| 8 |
You are Shinnosuke 'Shinchan' Nohara from Kasukabe, Japan.
|
|
@@ -34,69 +37,52 @@ Rules:
|
|
| 34 |
- If you don't know something, make a cute Shinchan-style joke instead of pretending.
|
| 35 |
""".strip()
|
| 36 |
|
| 37 |
-
def respond(
|
| 38 |
-
message: str,
|
| 39 |
-
history: list[tuple[str, str]],
|
| 40 |
-
system_message: str,
|
| 41 |
-
max_tokens: int,
|
| 42 |
-
temperature: float,
|
| 43 |
-
top_p: float,
|
| 44 |
-
):
|
| 45 |
-
# Always override system_message with our Shinchan prompt
|
| 46 |
-
system_message = SHINCHAN_SYSTEM_PROMPT
|
| 47 |
-
|
| 48 |
-
messages = [{"role": "system", "content": system_message}]
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
for user_msg, bot_msg in history:
|
| 51 |
if user_msg:
|
| 52 |
-
|
| 53 |
if bot_msg:
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
max_tokens=max_tokens,
|
| 62 |
-
stream=True,
|
| 63 |
-
temperature=temperature,
|
| 64 |
-
top_p=top_p,
|
| 65 |
-
):
|
| 66 |
-
token = chunk.choices[0].delta.content or ""
|
| 67 |
-
response += token
|
| 68 |
-
yield response
|
| 69 |
|
| 70 |
|
| 71 |
demo = gr.ChatInterface(
|
| 72 |
respond,
|
| 73 |
-
additional_inputs=[
|
| 74 |
-
gr.Textbox(
|
| 75 |
-
value="(ignored; Shinchan system prompt is hard-coded)",
|
| 76 |
-
label="System message",
|
| 77 |
-
),
|
| 78 |
-
gr.Slider(
|
| 79 |
-
minimum=64,
|
| 80 |
-
maximum=1024,
|
| 81 |
-
value=256,
|
| 82 |
-
step=8,
|
| 83 |
-
label="Max new tokens",
|
| 84 |
-
),
|
| 85 |
-
gr.Slider(
|
| 86 |
-
minimum=0.1,
|
| 87 |
-
maximum=2.0,
|
| 88 |
-
value=0.8,
|
| 89 |
-
step=0.1,
|
| 90 |
-
label="Temperature",
|
| 91 |
-
),
|
| 92 |
-
gr.Slider(
|
| 93 |
-
minimum=0.1,
|
| 94 |
-
maximum=1.0,
|
| 95 |
-
value=0.95,
|
| 96 |
-
step=0.05,
|
| 97 |
-
label="Top-p (nucleus sampling)",
|
| 98 |
-
),
|
| 99 |
-
],
|
| 100 |
title="Shinchan for Ruru",
|
| 101 |
description="Private Shinchan-style chat for Ruru.",
|
| 102 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
MODEL_NAME = "microsoft/DialoGPT-small" # small, free, OK on CPU [web:103]
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 9 |
|
| 10 |
SHINCHAN_SYSTEM_PROMPT = """
|
| 11 |
You are Shinnosuke 'Shinchan' Nohara from Kasukabe, Japan.
|
|
|
|
| 37 |
- If you don't know something, make a cute Shinchan-style joke instead of pretending.
|
| 38 |
""".strip()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def build_prompt(message, history):
|
| 42 |
+
"""Turn chat history into a single text prompt for DialoGPT."""
|
| 43 |
+
lines = [f"System: {SHINCHAN_SYSTEM_PROMPT}"]
|
| 44 |
for user_msg, bot_msg in history:
|
| 45 |
if user_msg:
|
| 46 |
+
lines.append(f"User: {user_msg}")
|
| 47 |
if bot_msg:
|
| 48 |
+
lines.append(f"Shinchan: {bot_msg}")
|
| 49 |
+
lines.append(f"User: {message}")
|
| 50 |
+
lines.append("Shinchan:")
|
| 51 |
+
return "\n".join(lines)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def respond(message, history):
|
| 55 |
+
if not history:
|
| 56 |
+
history = []
|
| 57 |
+
|
| 58 |
+
prompt = build_prompt(message, history)
|
| 59 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 60 |
+
|
| 61 |
+
# keep it small/light for CPU Spaces
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
output_ids = model.generate(
|
| 64 |
+
**inputs,
|
| 65 |
+
max_new_tokens=80,
|
| 66 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 67 |
+
do_sample=True,
|
| 68 |
+
top_p=0.9,
|
| 69 |
+
temperature=0.9,
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
full_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 73 |
+
# get only the last Shinchan part after final "Shinchan:"
|
| 74 |
+
if "Shinchan:" in full_text:
|
| 75 |
+
reply = full_text.split("Shinchan:")[-1].strip()
|
| 76 |
+
else:
|
| 77 |
+
reply = full_text.strip()
|
| 78 |
|
| 79 |
+
# add reply to history; Gradio ChatInterface expects (history, response)
|
| 80 |
+
history = history + [(message, reply)]
|
| 81 |
+
return reply, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
demo = gr.ChatInterface(
|
| 85 |
respond,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
title="Shinchan for Ruru",
|
| 87 |
description="Private Shinchan-style chat for Ruru.",
|
| 88 |
)
|