Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,31 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
-
# Load chatbot model
|
5 |
chatbot_model = "microsoft/DialoGPT-medium"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(chatbot_model)
|
7 |
-
model = AutoModelForCausalLM.from_pretrained(chatbot_model)
|
8 |
|
9 |
-
# Load emotion detection model
|
10 |
-
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion"
|
11 |
|
12 |
-
# Function to generate chatbot response and emotion analysis
|
13 |
def generate_response(user_input):
|
14 |
# Generate chatbot response
|
15 |
-
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
16 |
output = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id)
|
17 |
response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
18 |
|
19 |
# Detect emotion
|
20 |
emotion_result = emotion_pipeline(user_input)
|
21 |
emotion = emotion_result[0]["label"]
|
|
|
|
|
22 |
|
23 |
-
# Return chatbot response and detected emotion
|
24 |
-
return response, f"Emotion Detected: {emotion}"
|
25 |
-
|
26 |
-
# Gradio interface setup
|
27 |
iface = gr.Interface(
|
28 |
fn=generate_response,
|
29 |
-
inputs=gr.Textbox(label="
|
30 |
-
outputs=[gr.Textbox(label="
|
31 |
live=True
|
32 |
)
|
33 |
|
34 |
-
# Launch the app
|
35 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
+
# Load chatbot model
|
5 |
chatbot_model = "microsoft/DialoGPT-medium"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(chatbot_model)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(chatbot_model)
|
8 |
|
9 |
+
# Load emotion detection model
|
10 |
+
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
11 |
|
|
|
12 |
def generate_response(user_input):
|
13 |
# Generate chatbot response
|
14 |
+
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
15 |
output = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id)
|
16 |
response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
17 |
|
18 |
# Detect emotion
|
19 |
emotion_result = emotion_pipeline(user_input)
|
20 |
emotion = emotion_result[0]["label"]
|
21 |
+
|
22 |
+
return response, emotion
|
23 |
|
|
|
|
|
|
|
|
|
24 |
iface = gr.Interface(
|
25 |
fn=generate_response,
|
26 |
+
inputs=gr.Textbox(label="Enter your message"),
|
27 |
+
outputs=[gr.Textbox(label="Chatbot Response"), gr.Textbox(label="Emotion Detected")],
|
28 |
live=True
|
29 |
)
|
30 |
|
|
|
31 |
iface.launch()
|