Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,146 @@
|
|
| 1 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 6 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
do_sample=True,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
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(
|
| 18 |
+
"text-generation",
|
| 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 |
+
|
| 72 |
+
def respond(message, history):
|
| 73 |
+
# Manage conversation flow
|
| 74 |
+
messages = format_history(history)
|
| 75 |
+
messages.append({"role": "user", "content": message})
|
| 76 |
+
|
| 77 |
+
# Generate response with strict limits
|
| 78 |
+
prompt = tokenizer.apply_chat_template(
|
| 79 |
+
messages,
|
| 80 |
+
tokenize=False,
|
| 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
|
| 97 |
+
full_text = outputs[0]['generated_text']
|
| 98 |
+
response = full_text.split("assistant\n")[-1].split("###")[0].strip()
|
| 99 |
+
|
| 100 |
+
# Apply emotional intelligence
|
| 101 |
+
response = add_emotional_intelligence(response, message)
|
| 102 |
+
|
| 103 |
+
# Ensure natural ending
|
| 104 |
+
if response and response[-1] not in {".", "!", "?", "..."}:
|
| 105 |
+
response += "..." if len(response) < 35 else "."
|
| 106 |
+
|
| 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,
|
| 115 |
+
bubble_full_width=False,
|
| 116 |
+
show_copy_button=True,
|
| 117 |
+
avatar_images=(
|
| 118 |
+
"https://i.ibb.co/0nN3Pjz/user.png",
|
| 119 |
+
"https://i.ibb.co/7y0d1K5/bot.png"
|
| 120 |
+
)
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
msg = gr.Textbox(
|
| 124 |
+
placeholder="What's on your mind?",
|
| 125 |
+
container=False,
|
| 126 |
+
scale=7,
|
| 127 |
+
autofocus=True
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
clear = gr.Button("New Vibe", size="sm")
|
| 131 |
+
|
| 132 |
+
def user(user_message, history):
|
| 133 |
+
return "", history + [[user_message, None]]
|
| 134 |
+
|
| 135 |
+
def bot(history):
|
| 136 |
+
message = history[-1][0]
|
| 137 |
+
response = respond(message, history[:-1])
|
| 138 |
+
history[-1][1] = response
|
| 139 |
+
return history
|
| 140 |
+
|
| 141 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 142 |
+
bot, chatbot, chatbot
|
| 143 |
+
)
|
| 144 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 145 |
|
| 146 |
+
demo.queue(concurrency_count=1).launch()
|