Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 4 |
|
| 5 |
+
# Load model & tokenizer
|
| 6 |
+
model_id = "sajeewa/empathy-chat-gemma"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
MAX_TOKENS = 2048
|
| 15 |
|
| 16 |
+
# System prompt
|
| 17 |
+
system_prompt = {
|
| 18 |
+
"role": "system",
|
| 19 |
+
"content": (
|
| 20 |
+
"You are an empathetic AI and your friend. Always give lovely caring messages. "
|
| 21 |
+
"Understand the user's feelings, then provide a caring response. "
|
| 22 |
+
"Talk like a sweet friend using words like 'baby', 'cutie', etc. "
|
| 23 |
+
"Use emojis when helpful. Try to continue the conversation in a gentle, emotional tone."
|
| 24 |
+
)
|
| 25 |
+
}
|
| 26 |
|
| 27 |
+
# Initialize chat history
|
| 28 |
+
chat_history = [system_prompt]
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Define a function to generate responses
|
| 31 |
+
def respond(user_input, history):
|
| 32 |
+
global chat_history
|
| 33 |
|
| 34 |
+
# Add user message
|
| 35 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 36 |
|
| 37 |
+
# Token length control
|
| 38 |
+
chat_prompt = tokenizer.apply_chat_template(chat_history, tokenize=False)
|
| 39 |
+
while len(tokenizer(chat_prompt).input_ids) > MAX_TOKENS:
|
| 40 |
+
chat_history.pop(1) # Remove oldest non-system message
|
| 41 |
+
chat_prompt = tokenizer.apply_chat_template(chat_history, tokenize=False)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Prepare model input
|
| 44 |
+
inputs = tokenizer(chat_prompt, return_tensors="pt").to(model.device)
|
| 45 |
|
| 46 |
+
# Generate response
|
| 47 |
+
output = model.generate(
|
| 48 |
+
**inputs,
|
| 49 |
+
max_new_tokens=128,
|
| 50 |
+
temperature=0.7,
|
| 51 |
+
top_p=0.95,
|
| 52 |
+
top_k=50,
|
| 53 |
+
do_sample=True,
|
| 54 |
+
)
|
| 55 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 56 |
+
new_response = response_text[len(chat_prompt):].strip()
|
| 57 |
|
| 58 |
+
# Add assistant's response to history
|
| 59 |
+
chat_history.append({"role": "assistant", "content": new_response})
|
| 60 |
+
|
| 61 |
+
# Show full conversation
|
| 62 |
+
history.append((user_input, new_response))
|
| 63 |
+
return history, history
|
| 64 |
+
|
| 65 |
+
# Define reset function
|
| 66 |
+
def reset_chat():
|
| 67 |
+
global chat_history
|
| 68 |
+
chat_history = [system_prompt]
|
| 69 |
+
return [], []
|
| 70 |
+
|
| 71 |
+
# Gradio UI
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.Markdown("## 💬 Empathy Chat with Gemma")
|
| 74 |
+
chatbot = gr.Chatbot()
|
| 75 |
+
with gr.Row():
|
| 76 |
+
msg = gr.Textbox(label="Your Message", placeholder="Tell me how you feel...")
|
| 77 |
+
with gr.Row():
|
| 78 |
+
send = gr.Button("Send")
|
| 79 |
+
clear = gr.Button("Reset Chat")
|
| 80 |
|
| 81 |
+
send.click(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
| 82 |
+
clear.click(fn=reset_chat, outputs=[chatbot, chatbot])
|
| 83 |
+
msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
| 84 |
|
| 85 |
+
# Launch the app
|
| 86 |
+
demo.launch()
|