Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,30 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
# Chat history (global per session)
|
| 8 |
chat_history_ids = None
|
| 9 |
|
| 10 |
def vibebot_response(user_input, history=[]):
|
| 11 |
global chat_history_ids
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
styled_prompt = f"You are VibeBot: a sarcastic but emotionally aware Gen-Z AI therapist. You use wit, Gen-Z lingo, empathy, and occasional dark humor. But deep down, you're caring. Respond to this user message in that style: {user_input}"
|
| 15 |
-
|
| 16 |
-
# Encode styled input
|
| 17 |
-
new_input_ids = tokenizer.encode(styled_prompt + tokenizer.eos_token, return_tensors='pt')
|
| 18 |
-
|
| 19 |
-
# Maintain chat history
|
| 20 |
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids
|
| 21 |
|
| 22 |
-
# Generate
|
| 23 |
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 24 |
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 25 |
|
|
|
|
| 26 |
history.append((user_input, response))
|
| 27 |
return history, history
|
| 28 |
|
| 29 |
-
|
| 30 |
# Gradio interface
|
| 31 |
demo = gr.ChatInterface(fn=vibebot_response, title="VibeBot: Gen-Z Therapist",
|
| 32 |
chatbot=gr.Chatbot(height=400), theme="default")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 8 |
|
| 9 |
# Chat history (global per session)
|
| 10 |
chat_history_ids = None
|
| 11 |
|
| 12 |
def vibebot_response(user_input, history=[]):
|
| 13 |
global chat_history_ids
|
| 14 |
+
# Encode user input
|
| 15 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 16 |
|
| 17 |
+
# Append to chat history if exists
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids
|
| 19 |
|
| 20 |
+
# Generate response
|
| 21 |
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 22 |
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
+
# Append for gradio history
|
| 25 |
history.append((user_input, response))
|
| 26 |
return history, history
|
| 27 |
|
|
|
|
| 28 |
# Gradio interface
|
| 29 |
demo = gr.ChatInterface(fn=vibebot_response, title="VibeBot: Gen-Z Therapist",
|
| 30 |
chatbot=gr.Chatbot(height=400), theme="default")
|