Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, pipeline
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 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(
|
| 18 |
"text-generation",
|
| 19 |
model=model_id,
|
| 20 |
tokenizer=tokenizer,
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model_kwargs={
|
| 24 |
-
"quantization_config": quant_config,
|
| 25 |
-
}
|
| 26 |
)
|
| 27 |
|
| 28 |
# Enhanced persona definition
|
|
@@ -43,7 +34,7 @@ Now respond to this:
|
|
| 43 |
|
| 44 |
def format_history(history):
|
| 45 |
messages = [{"role": "system", "content": PERSONA}]
|
| 46 |
-
for user_msg, bot_msg in history[-
|
| 47 |
messages.append({"role": "user", "content": user_msg})
|
| 48 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 49 |
return messages
|
|
@@ -51,19 +42,24 @@ def format_history(history):
|
|
| 51 |
def add_emotional_intelligence(response, message):
|
| 52 |
"""Enhance response with emotional elements"""
|
| 53 |
# Add emoji based on content
|
| 54 |
-
if
|
| 55 |
-
response
|
| 56 |
-
elif
|
| 57 |
-
response
|
| 58 |
|
| 59 |
# Add conversational hooks
|
| 60 |
if "?" in message and not response.endswith("?"):
|
| 61 |
-
if len(response
|
| 62 |
-
response += "
|
| 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 |
|
| 69 |
def respond(message, history):
|
|
@@ -71,51 +67,41 @@ def respond(message, history):
|
|
| 71 |
messages = format_history(history)
|
| 72 |
messages.append({"role": "user", "content": message})
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
prompt =
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
#
|
| 82 |
outputs = pipe(
|
| 83 |
prompt,
|
| 84 |
-
max_new_tokens=48,
|
| 85 |
-
temperature=0.
|
| 86 |
-
top_k=
|
| 87 |
do_sample=True,
|
| 88 |
-
num_beams=1,
|
| 89 |
repetition_penalty=1.1,
|
| 90 |
-
|
| 91 |
-
pad_token_id=tokenizer.eos_token_id
|
| 92 |
)
|
| 93 |
|
| 94 |
# Extract response
|
| 95 |
-
|
| 96 |
-
response = full_text.split("assistant\n")[-1].split("###")[0].strip()
|
| 97 |
|
| 98 |
# Apply emotional intelligence
|
| 99 |
response = add_emotional_intelligence(response, message)
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
response += "..." if len(response) < 35 else "."
|
| 104 |
-
|
| 105 |
-
return response[:96] # Hard character limit
|
| 106 |
|
| 107 |
-
#
|
| 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=
|
| 113 |
-
bubble_full_width=False
|
| 114 |
-
show_copy_button=True,
|
| 115 |
-
avatar_images=(
|
| 116 |
-
"https://i.ibb.co/0nN3Pjz/user.png",
|
| 117 |
-
"https://i.ibb.co/7y0d1K5/bot.png"
|
| 118 |
-
)
|
| 119 |
)
|
| 120 |
|
| 121 |
msg = gr.Textbox(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, pipeline
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Free-tier optimized model (lightweight but capable)
|
| 7 |
+
model_id = "HuggingFaceH4/zephyr-7b-alpha" # Smaller than beta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
|
| 10 |
+
# Free-tier friendly setup (no quantization needed)
|
| 11 |
pipe = pipeline(
|
| 12 |
"text-generation",
|
| 13 |
model=model_id,
|
| 14 |
tokenizer=tokenizer,
|
| 15 |
+
device="cpu", # Force CPU-only
|
| 16 |
+
torch_dtype=torch.float32, # Use float32 for CPU compatibility
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# Enhanced persona definition
|
|
|
|
| 34 |
|
| 35 |
def format_history(history):
|
| 36 |
messages = [{"role": "system", "content": PERSONA}]
|
| 37 |
+
for user_msg, bot_msg in history[-2:]: # Only last 2 exchanges
|
| 38 |
messages.append({"role": "user", "content": user_msg})
|
| 39 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 40 |
return messages
|
|
|
|
| 42 |
def add_emotional_intelligence(response, message):
|
| 43 |
"""Enhance response with emotional elements"""
|
| 44 |
# Add emoji based on content
|
| 45 |
+
if "!" in message:
|
| 46 |
+
response = response.replace(".", "!") + " π"
|
| 47 |
+
elif "?" in message:
|
| 48 |
+
response = response + " π€" if not response.endswith("?") else response
|
| 49 |
|
| 50 |
# Add conversational hooks
|
| 51 |
if "?" in message and not response.endswith("?"):
|
| 52 |
+
if len(response) < 60:
|
| 53 |
+
response += " How about you?"
|
| 54 |
|
| 55 |
# Make more human-like
|
| 56 |
response = response.replace("I am", "I'm").replace("You are", "You're")
|
| 57 |
|
| 58 |
+
# Free-tier: Limit to 15 words max
|
| 59 |
+
words = response.split()
|
| 60 |
+
if len(words) > 15:
|
| 61 |
+
response = " ".join(words[:15]) + "..."
|
| 62 |
+
|
| 63 |
return response.strip()
|
| 64 |
|
| 65 |
def respond(message, history):
|
|
|
|
| 67 |
messages = format_history(history)
|
| 68 |
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
+
# Create prompt manually (lightweight)
|
| 71 |
+
prompt = ""
|
| 72 |
+
for msg in messages:
|
| 73 |
+
role = "User" if msg["role"] == "user" else "Assistant"
|
| 74 |
+
prompt += f"{role}: {msg['content']}\n"
|
| 75 |
+
prompt += "Assistant:"
|
| 76 |
|
| 77 |
+
# Free-tier optimized generation
|
| 78 |
outputs = pipe(
|
| 79 |
prompt,
|
| 80 |
+
max_new_tokens=48, # Short responses
|
| 81 |
+
temperature=0.9,
|
| 82 |
+
top_k=40,
|
| 83 |
do_sample=True,
|
| 84 |
+
num_beams=1, # Fastest decoding
|
| 85 |
repetition_penalty=1.1,
|
| 86 |
+
no_repeat_ngram_size=2
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
# Extract response
|
| 90 |
+
response = outputs[0]['generated_text'].split("Assistant:")[-1].strip()
|
|
|
|
| 91 |
|
| 92 |
# Apply emotional intelligence
|
| 93 |
response = add_emotional_intelligence(response, message)
|
| 94 |
|
| 95 |
+
# Free-tier safety
|
| 96 |
+
return response[:80] # Hard character limit
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Lightweight interface
|
| 99 |
with gr.Blocks(theme=gr.themes.Soft(), title="π΄ ππ πππ") as demo:
|
| 100 |
gr.Markdown("# π΄ ππ πππ \n*Chill β’ Confident β’ Humanlike*")
|
| 101 |
|
| 102 |
chatbot = gr.Chatbot(
|
| 103 |
+
height=350,
|
| 104 |
+
bubble_full_width=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
msg = gr.Textbox(
|