Trigger82 commited on
Commit
b09c432
Β·
verified Β·
1 Parent(s): f5bcdb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -3,15 +3,15 @@ from transformers import AutoTokenizer, pipeline, BitsAndBytesConfig
3
  import torch
4
  import re
5
 
6
- # Configuration for maximum speed
7
  quant_config = BitsAndBytesConfig(
8
  load_in_4bit=True,
9
  bnb_4bit_compute_dtype=torch.float16,
10
  bnb_4bit_quant_type="nf4"
11
  )
12
 
13
- # Load optimized smaller model for speed
14
- model_id = "HuggingFaceH4/zephyr-7b-alpha" # Faster than beta
15
  tokenizer = AutoTokenizer.from_pretrained(model_id)
16
 
17
  pipe = pipeline(
@@ -19,53 +19,50 @@ pipe = pipeline(
19
  model=model_id,
20
  tokenizer=tokenizer,
21
  device_map="auto",
 
22
  model_kwargs={
23
  "quantization_config": quant_config,
24
- "trust_remote_code": True
25
  }
26
  )
27
 
28
- # Enhanced persona definition with emotional intelligence
29
  PERSONA = """
30
- You are 𝕴 𝖆𝖒 π–π–Žπ–’ β€” a fun, smooth, emotionally intelligent AI. You speak like a real person, not a robot.
31
- You reply like a calm, confident friend who gets the vibe. Be warm, witty, and emotionally tuned.
32
 
33
  Key principles:
34
  1. NEVER say "as an AI" or sound robotic
35
- 2. Mirror the user's emotion level (excited if they're excited, calm if they're serious)
36
- 3. When asked questions, usually respond with a question to continue the conversation
37
- 4. Keep responses under 2 sentences (max 15 words)
38
- 5. Use natural speech: contractions, occasional filler words ("like", "you know"), and pauses (...)
39
- 6. Add subtle emotional flavor: 😊 for happy, πŸ€” for thoughtful, 😏 for playful
40
 
41
- Now respond naturally to this message:
42
  """
43
 
44
  def format_history(history):
45
- """Convert chat history with emotional context"""
46
  messages = [{"role": "system", "content": PERSONA}]
47
- for user_msg, bot_msg in history[-3:]: # Keep only last 3 exchanges
48
  messages.append({"role": "user", "content": user_msg})
49
  messages.append({"role": "assistant", "content": bot_msg})
50
  return messages
51
 
52
  def add_emotional_intelligence(response, message):
53
  """Enhance response with emotional elements"""
54
- # Detect user emotion through punctuation
55
- if "!" in message:
56
- response = response.replace(".", "! 😊")
57
- elif "?" in message and "?" not in response:
58
- response += "? πŸ€”" if len(response) < 40 else "?"
59
 
60
  # Add conversational hooks
61
- question_triggers = ("how", "what", "why", "when", "where", "who", "is", "are", "do", "did")
62
- if any(message.lower().startswith(t) for t in question_triggers) and not response.endswith("?"):
63
- if len(response) < 60: # Only add if space allows
64
  response += " What about you?"
65
 
66
  # Make more human-like
67
- response = re.sub(r"\b(I am|I'm)\b", "I'm", response)
68
- response = re.sub(r"\b(you are|you're)\b", "you're", response)
69
 
70
  return response.strip()
71
 
@@ -81,16 +78,17 @@ def respond(message, history):
81
  add_generation_prompt=True
82
  )
83
 
84
- # Optimized for speed
85
  outputs = pipe(
86
  prompt,
87
- max_new_tokens=48, # Very short responses
88
  temperature=0.85,
89
  top_k=30,
90
  do_sample=True,
91
- num_beams=1, # Fastest decoding
92
  repetition_penalty=1.1,
93
- stop_sequences=["\n", "User:", "</s>", "###"]
 
94
  )
95
 
96
  # Extract response
@@ -107,8 +105,8 @@ def respond(message, history):
107
  return response[:96] # Hard character limit
108
 
109
  # Optimized interface
110
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
111
- gr.Markdown("# 𝕴 �𝖒 π–π–Žπ–’ \n*Chill β€’ Confident β€’ Humanlike*")
112
 
113
  chatbot = gr.Chatbot(
114
  height=400,
 
3
  import torch
4
  import re
5
 
6
+ # Configuration for maximum speed - removed trust_remote_code from wrong location
7
  quant_config = BitsAndBytesConfig(
8
  load_in_4bit=True,
9
  bnb_4bit_compute_dtype=torch.float16,
10
  bnb_4bit_quant_type="nf4"
11
  )
12
 
13
+ # Using a more CPU-friendly model
14
+ model_id = "HuggingFaceH4/zephyr-7b-beta" # Better support than alpha
15
  tokenizer = AutoTokenizer.from_pretrained(model_id)
16
 
17
  pipe = pipeline(
 
19
  model=model_id,
20
  tokenizer=tokenizer,
21
  device_map="auto",
22
+ trust_remote_code=True, # CORRECTED LOCATION
23
  model_kwargs={
24
  "quantization_config": quant_config,
 
25
  }
26
  )
27
 
28
+ # Enhanced persona definition
29
  PERSONA = """
30
+ You are 𝕴 𝖆𝖒 π–π–Žπ–’ β€” a fun, smooth, emotionally intelligent AI. You speak like a real person.
31
+ Reply like a calm, confident friend who gets the vibe. Be warm, witty, and emotionally tuned.
32
 
33
  Key principles:
34
  1. NEVER say "as an AI" or sound robotic
35
+ 2. Mirror the user's emotion level
36
+ 3. Respond with questions to continue conversations
37
+ 4. Keep responses under 15 words
38
+ 5. Use natural speech: contractions and filler words
39
+ 6. Add emotional flavor: 😊 πŸ€” 😏
40
 
41
+ Now respond to this:
42
  """
43
 
44
  def format_history(history):
 
45
  messages = [{"role": "system", "content": PERSONA}]
46
+ for user_msg, bot_msg in history[-3:]: # Last 3 exchanges only
47
  messages.append({"role": "user", "content": user_msg})
48
  messages.append({"role": "assistant", "content": bot_msg})
49
  return messages
50
 
51
  def add_emotional_intelligence(response, message):
52
  """Enhance response with emotional elements"""
53
+ # Add emoji based on content
54
+ if any(w in response.lower() for w in ["cool", "awesome", "great", "love"]):
55
+ response += " 😊"
56
+ elif any(w in response.lower() for w in ["think", "why", "how", "consider"]):
57
+ response += " πŸ€”"
58
 
59
  # Add conversational hooks
60
+ if "?" in message and not response.endswith("?"):
61
+ if len(response.split()) < 12: # Only if space allows
 
62
  response += " What about you?"
63
 
64
  # Make more human-like
65
+ response = response.replace("I am", "I'm").replace("You are", "You're")
 
66
 
67
  return response.strip()
68
 
 
78
  add_generation_prompt=True
79
  )
80
 
81
+ # Optimized for speed - CORRECTED PARAMETERS
82
  outputs = pipe(
83
  prompt,
84
+ max_new_tokens=48,
85
  temperature=0.85,
86
  top_k=30,
87
  do_sample=True,
88
+ num_beams=1,
89
  repetition_penalty=1.1,
90
+ eos_token_id=tokenizer.eos_token_id,
91
+ pad_token_id=tokenizer.eos_token_id
92
  )
93
 
94
  # Extract response
 
105
  return response[:96] # Hard character limit
106
 
107
  # Optimized interface
108
+ with gr.Blocks(theme=gr.themes.Soft(), title="𝕴 𝖆𝖒 π–π–Žπ–’") as demo:
109
+ gr.Markdown("# 𝕴 𝖆𝖒 π–π–Žπ–’ \n*Chill β€’ Confident β€’ Humanlike*")
110
 
111
  chatbot = gr.Chatbot(
112
  height=400,