Shahid0812 commited on
Commit
49c770a
·
verified ·
1 Parent(s): b560b2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -2,10 +2,12 @@ import gradio as gr
2
  from transformers import pipeline
3
  import torch
4
 
5
- # 1. SETUP
6
  model_id = "Qwen/Qwen2.5-0.5B-Instruct"
7
- print("Loading Mochi to CPU...")
8
 
 
 
 
9
  pipe = pipeline(
10
  "text-generation",
11
  model=model_id,
@@ -14,47 +16,60 @@ pipe = pipeline(
14
  )
15
 
16
  def chat_fn(message, history):
17
- # THE SOUL
18
  character_card = (
19
  "You are Mochi, a chill, slightly sarcastic, but deeply loyal best friend. "
20
  "Talk like a real person in their 20s: use lowercase, occasional slang, and short sentences. "
21
  "Be supportive but roast the user occasionally. Keep answers short and snappy."
22
  )
23
 
 
24
  messages = [{"role": "system", "content": character_card}]
25
 
26
- # THE MEMORY (Auto-detecting format to prevent 'unpack' errors)
 
27
  for entry in history:
28
- if isinstance(entry, dict):
29
- # If history is [{'role': 'user', 'content': '...'}, ...]
30
- messages.append({"role": entry["role"], "content": entry["content"]})
31
- elif isinstance(entry, (list, tuple)):
32
- # If history is [[user_msg, bot_msg], ...]
33
- messages.append({"role": "user", "content": str(entry[0])})
34
- messages.append({"role": "assistant", "content": str(entry[1])})
35
-
36
- # Add the current message
 
 
 
 
 
 
 
37
  messages.append({"role": "user", "content": message})
38
 
39
- # THE GENERATION
40
  try:
 
41
  generation = pipe(
42
  messages,
43
  max_new_tokens=128,
44
  do_sample=True,
45
- temperature=0.85,
46
  top_p=0.9,
47
  truncation=True
48
  )
 
49
  return generation[0]['generated_text'][-1]['content']
50
  except Exception as e:
51
- print(f"Error: {e}")
52
  return "my bad, my brain just glitched. lol try again?"
53
 
54
- # 4. THE UI (Minimalist to avoid version conflicts)
55
  with gr.Blocks() as demo:
56
  gr.Markdown("# 🐾 Mochi AI")
57
- gr.Markdown("*Unlimited chill vibes.*")
 
 
58
  gr.ChatInterface(fn=chat_fn)
59
 
60
  # 5. THE LAUNCH
 
2
  from transformers import pipeline
3
  import torch
4
 
5
+ # 1. SETUP: Qwen 0.5B - Optimized for Free CPU
6
  model_id = "Qwen/Qwen2.5-0.5B-Instruct"
 
7
 
8
+ print("Loading Mochi to CPU... Stay chill.")
9
+
10
+ # Initialize the pipeline
11
  pipe = pipeline(
12
  "text-generation",
13
  model=model_id,
 
16
  )
17
 
18
  def chat_fn(message, history):
19
+ # THE SOUL: Define Mochi's personality
20
  character_card = (
21
  "You are Mochi, a chill, slightly sarcastic, but deeply loyal best friend. "
22
  "Talk like a real person in their 20s: use lowercase, occasional slang, and short sentences. "
23
  "Be supportive but roast the user occasionally. Keep answers short and snappy."
24
  )
25
 
26
+ # Start with the personality
27
  messages = [{"role": "system", "content": character_card}]
28
 
29
+ # 2. THE CLEANER: This part is the most important.
30
+ # It converts any version of Gradio history into a clean format for the AI.
31
  for entry in history:
32
+ # Check if entry is the new Gradio 6.0 dict format
33
+ if isinstance(entry, dict) and "role" in entry and "content" in entry:
34
+ content = entry["content"]
35
+ # If content is a list (Gradio 6.0 style), extract the text
36
+ if isinstance(content, list):
37
+ text = next((item["text"] for item in content if item.get("type") == "text"), "")
38
+ else:
39
+ text = str(content)
40
+ messages.append({"role": entry["role"], "content": text})
41
+
42
+ # Check if entry is the old Gradio 5.0 tuple/list format
43
+ elif isinstance(entry, (list, tuple)) and len(entry) == 2:
44
+ if entry[0]: messages.append({"role": "user", "content": str(entry[0])})
45
+ if entry[1]: messages.append({"role": "assistant", "content": str(entry[1])})
46
+
47
+ # Add the current message from the user
48
  messages.append({"role": "user", "content": message})
49
 
50
+ # 3. GENERATION
51
  try:
52
+ # We limit max_new_tokens to 128 to keep it fast on CPU
53
  generation = pipe(
54
  messages,
55
  max_new_tokens=128,
56
  do_sample=True,
57
+ temperature=0.8,
58
  top_p=0.9,
59
  truncation=True
60
  )
61
+ # Extract response
62
  return generation[0]['generated_text'][-1]['content']
63
  except Exception as e:
64
+ print(f"Error during generation: {e}")
65
  return "my bad, my brain just glitched. lol try again?"
66
 
67
+ # 4. THE UI
68
  with gr.Blocks() as demo:
69
  gr.Markdown("# 🐾 Mochi AI")
70
+ gr.Markdown("*Your chill, unlimited best friend.*")
71
+
72
+ # We remove 'type' and let the 'Cleaner' above handle whatever Gradio sends
73
  gr.ChatInterface(fn=chat_fn)
74
 
75
  # 5. THE LAUNCH