Update app.py
Browse files
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 |
-
#
|
| 14 |
-
model_id = "HuggingFaceH4/zephyr-7b-
|
| 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
|
| 29 |
PERSONA = """
|
| 30 |
-
You are π΄ ππ πππ β a fun, smooth, emotionally intelligent AI. You speak like a real person
|
| 31 |
-
|
| 32 |
|
| 33 |
Key principles:
|
| 34 |
1. NEVER say "as an AI" or sound robotic
|
| 35 |
-
2. Mirror the user's emotion level
|
| 36 |
-
3.
|
| 37 |
-
4. Keep responses under
|
| 38 |
-
5. Use natural speech: contractions
|
| 39 |
-
6. Add
|
| 40 |
|
| 41 |
-
Now respond
|
| 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:]: #
|
| 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 |
-
#
|
| 55 |
-
if "
|
| 56 |
-
response
|
| 57 |
-
elif
|
| 58 |
-
response += "
|
| 59 |
|
| 60 |
# Add conversational hooks
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if len(response) < 60: # Only add if space allows
|
| 64 |
response += " What about you?"
|
| 65 |
|
| 66 |
# Make more human-like
|
| 67 |
-
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,
|
| 88 |
temperature=0.85,
|
| 89 |
top_k=30,
|
| 90 |
do_sample=True,
|
| 91 |
-
num_beams=1,
|
| 92 |
repetition_penalty=1.1,
|
| 93 |
-
|
|
|
|
| 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("# π΄
|
| 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,
|