Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,13 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
|
|
|
| 6 |
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 7 |
|
| 8 |
-
print("Loading Mochi to CPU...")
|
| 9 |
-
|
|
|
|
| 10 |
pipe = pipeline(
|
| 11 |
"text-generation",
|
| 12 |
model=model_id,
|
|
@@ -16,50 +18,56 @@ pipe = pipeline(
|
|
| 16 |
|
| 17 |
def chat_fn(message, history):
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
history: List of [user_message, assistant_message] pairs
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
- Keep answers short and snappy."""
|
| 29 |
|
| 30 |
-
#
|
|
|
|
| 31 |
messages = [{"role": "system", "content": character_card}]
|
| 32 |
|
|
|
|
| 33 |
for user_msg, assistant_msg in history:
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Add the
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="slate")) as demo:
|
| 55 |
gr.Markdown("# 🐾 Mochi AI")
|
| 56 |
-
gr.Markdown("*Your chill, unlimited best friend. No
|
| 57 |
|
|
|
|
| 58 |
chat_interface = gr.ChatInterface(
|
| 59 |
fn=chat_fn,
|
| 60 |
-
examples=["
|
| 61 |
-
cache_examples=False
|
| 62 |
)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# 1. SETUP: Qwen 0.5B - The "King" of Free CPU models
|
| 6 |
+
# This model is small enough to be fast but smart enough to have a personality.
|
| 7 |
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 8 |
|
| 9 |
+
print("Loading Mochi to CPU... Stay chill.")
|
| 10 |
+
|
| 11 |
+
# Using float32 because CPUs handle it more stably than float16
|
| 12 |
pipe = pipeline(
|
| 13 |
"text-generation",
|
| 14 |
model=model_id,
|
|
|
|
| 18 |
|
| 19 |
def chat_fn(message, history):
|
| 20 |
"""
|
| 21 |
+
Fixed chat function to prevent 'Role' errors on 2nd+ messages.
|
|
|
|
| 22 |
"""
|
| 23 |
+
# THE SOUL: Define Mochi's personality
|
| 24 |
+
character_card = (
|
| 25 |
+
"You are Mochi, a chill, slightly sarcastic, but deeply loyal best friend. "
|
| 26 |
+
"Talk like a real person in their 20s: use lowercase, occasional slang, and short sentences. "
|
| 27 |
+
"Be supportive but roast the user occasionally. Keep answers short and snappy."
|
| 28 |
+
)
|
|
|
|
| 29 |
|
| 30 |
+
# THE MEMORY: Properly format history for the model
|
| 31 |
+
# Format: [ {'role': 'system', 'content': '...'}, {'role': 'user', 'content': '...'}, {'role': 'assistant', 'content': '...'} ]
|
| 32 |
messages = [{"role": "system", "content": character_card}]
|
| 33 |
|
| 34 |
+
# history comes from Gradio as a list of [user_message, assistant_message]
|
| 35 |
for user_msg, assistant_msg in history:
|
| 36 |
+
if user_msg:
|
| 37 |
+
messages.append({"role": "user", "content": user_msg})
|
| 38 |
+
if assistant_msg:
|
| 39 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 40 |
|
| 41 |
+
# Add the newest message from the user
|
| 42 |
messages.append({"role": "user", "content": message})
|
| 43 |
|
| 44 |
+
# THE GENERATION: Optimized for speed and personality
|
| 45 |
+
try:
|
| 46 |
+
generation = pipe(
|
| 47 |
+
messages,
|
| 48 |
+
max_new_tokens=128,
|
| 49 |
+
do_sample=True,
|
| 50 |
+
temperature=0.85,
|
| 51 |
+
top_p=0.9,
|
| 52 |
+
truncation=True # Prevents RAM overflow as chat gets longer
|
| 53 |
+
)
|
| 54 |
+
# Safely extract the content
|
| 55 |
+
return generation[0]['generated_text'][-1]['content']
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error: {e}")
|
| 58 |
+
return "my bad, my brain just glitched. can you say that again? lol"
|
| 59 |
|
| 60 |
+
# THE UI: Soft Pink Theme for a 'Buddy' feel
|
| 61 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="slate")) as demo:
|
| 62 |
gr.Markdown("# 🐾 Mochi AI")
|
| 63 |
+
gr.Markdown("*Your chill, unlimited best friend. No tokens, no limits.*")
|
| 64 |
|
| 65 |
+
# ChatInterface automatically handles the 'history' list for us
|
| 66 |
chat_interface = gr.ChatInterface(
|
| 67 |
fn=chat_fn,
|
| 68 |
+
examples=["yo mochi, what's up?", "roast my life real quick", "idk what to eat today"],
|
| 69 |
+
cache_examples=False
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Launch the app (This also enables the API automatically)
|
| 73 |
demo.launch()
|