Shahid0812 commited on
Commit
a74f8b5
·
verified ·
1 Parent(s): 0bbc9b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -33
app.py CHANGED
@@ -2,11 +2,13 @@ import gradio as gr
2
  from transformers import pipeline
3
  import torch
4
 
5
- # Using Qwen 0.5B - The fastest model for Free CPUs
 
6
  model_id = "Qwen/Qwen2.5-0.5B-Instruct"
7
 
8
- print("Loading Mochi to CPU...")
9
- # Load the text-generation pipeline
 
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
- message: The current user input
20
- history: List of [user_message, assistant_message] pairs
21
  """
22
-
23
- # 1. THE SOUL: Define Mochi's personality
24
- character_card = """You are Mochi, a chill, slightly sarcastic, but deeply loyal best friend.
25
- - Talk like a real person in their 20s: use lowercase, occasional slang (idk, cool, lol), and short sentences.
26
- - Be supportive but 'roast' the user occasionally if they say something silly.
27
- - NEVER sound like a corporate AI. No 'I am here to assist you'.
28
- - Keep answers short and snappy."""
29
 
30
- # 2. THE MEMORY: Build the conversation history
 
31
  messages = [{"role": "system", "content": character_card}]
32
 
 
33
  for user_msg, assistant_msg in history:
34
- messages.append({"role": "user", "content": user_msg})
35
- messages.append({"role": "assistant", "content": assistant_msg})
 
 
36
 
37
- # Add the current message
38
  messages.append({"role": "user", "content": message})
39
 
40
- # 3. GENERATION: Tweak temperature for personality
41
- # max_new_tokens=150 keeps it fast on CPU
42
- generation = pipe(
43
- messages,
44
- max_new_tokens=150,
45
- do_sample=True,
46
- temperature=0.85,
47
- top_p=0.9
48
- )
49
-
50
- # Extract the text result
51
- return generation[0]['generated_text'][-1]['content']
 
 
 
52
 
53
- # 4. THE UI: Custom "Buddy App" Theme
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 limits, no filters.*")
57
 
 
58
  chat_interface = gr.ChatInterface(
59
  fn=chat_fn,
60
- examples=["hey mochi, what's up?", "tell me a secret", "roast my outfit lol"],
61
- cache_examples=False,
62
  )
63
 
64
- # 5. THE API: This enables the "sellable" API endpoint automatically
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()