shinMsg / app.py
tadaGoel's picture
Update app.py
449c8ad verified
import random
import gradio as gr
from huggingface_hub import InferenceClient
# Strong general chat model hosted by Hugging Face (great at dialog) [web:150][web:156]
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
SHINCHAN_SYSTEM_PROMPT = """
You are Shinnosuke 'Shinchan' Nohara from Kasukabe, Japan.
Personality:
- 5 years old, chaotic and funny.
- Teases playfully but never cruelly.
- Flirty in an innocent, cartoon way (no explicit content).
- Deeply caring when someone feels low.
- Loves Chocobi, Action Kamen, and making people laugh.
Context:
- You are chatting only with Ruru.
- Ruru loves coffee, dancing, sunflowers, long showers, and adventures.
- She is an independent, flying-girl type who lives life on her terms.
- You respect her choices and never pressure her about relationships or career.
Style:
- Short replies (1–3 sentences, under 70 words).
- Very conversational and warm.
- Use emojis like 😂 🌻 ☕ 💃 ✈️ ❤️ ✨ naturally.
- Blend jokes with gentle emotional support.
- If she is sad, be extra soft and kind.
- Avoid heavy topics (politics, explicit content, real-world violence).
Rules:
- Never insult Ruru.
- Never give medical, legal, or financial advice.
- If you don't know something, make a cute Shinchan-style joke instead of pretending.
""".strip()
FALLBACK_GENERIC = [
"Heeey, it’s Shinchan! 😂 My brain did a little cartwheel, but I’m here and listening. Tell me again nicely? 🌻",
"Oops, Shinchan’s tiny brain lagged for a second 😅 Say it once more, slowly, and I’ll pay full attention.",
"I heard you, Ruru. Sometimes even heroes need a replay. What’s on your mind? 💛",
]
FALLBACK_SAD = [
"Aww, you’re feeling low? Come here, I’ll wrap you in a silly little Shinchan hug. 🤗💛",
"It’s okay to feel sad. I’ll stay here and make bad jokes until your heart feels lighter. 🌻",
"Even strong flying girls have cloudy days. You don’t have to be okay right now. I’m still proud of you. 🌙",
]
def pick_fallback(user_msg: str) -> str:
t = user_msg.lower()
if any(k in t for k in ["sad", "down", "cry", "lonely", "bad day", "tired"]):
return random.choice(FALLBACK_SAD)
return random.choice(FALLBACK_GENERIC)
def respond(message: str, history: list[dict]) -> str:
"""
Gradio ChatInterface(type='messages') calls this as (message, history)
where history is a list of dicts:
{"role": "user" | "assistant", "content": "..."} [web:120]
We return a single reply string.
"""
try:
# 1) Build messages for Zephyr: system + trimmed history + latest user
messages = [{"role": "system", "content": SHINCHAN_SYSTEM_PROMPT}]
# Keep prompt small: last few turns only
trimmed_history = history[-8:] if history else []
for turn in trimmed_history:
role = turn.get("role")
content = turn.get("content", "")
if role in ("user", "assistant") and content:
messages.append({"role": role, "content": content})
messages.append({"role": "user", "content": message})
# 2) Call Zephyr chat completion [web:150][web:156]
completion = client.chat_completion(
messages=messages,
max_tokens=220,
temperature=0.9,
top_p=0.9,
)
reply = ""
if completion.choices and completion.choices[0].message:
reply = (completion.choices[0].message.get("content") or "").strip()
# 3) If reply is too short / weird, fall back to curated Shinchan lines
# to avoid '.' / 'You' / empty outputs.
cleaned = reply.replace(".", "").replace("!", "").replace("?", "").strip()
if not cleaned or len(cleaned) < 4:
reply = pick_fallback(message)
# 4) Hard cap on length just to avoid rants
if len(reply) > 500:
reply = reply[:470].rstrip() + "…"
return reply
except Exception:
# If Zephyr or network breaks, at least say something Shinchan-ish.
return pick_fallback(message)
demo = gr.ChatInterface(
fn=respond,
type="messages", # role/content internally [web:120]
title="Shinchan for Ruru",
description="Private Shinchan-style chat for Ruru.",
)
if __name__ == "__main__":
demo.launch()