|
import gradio as gr |
|
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
chatbot_model = "microsoft/DialoGPT-medium" |
|
tokenizer = AutoTokenizer.from_pretrained(chatbot_model) |
|
model = AutoModelForCausalLM.from_pretrained(chatbot_model) |
|
|
|
|
|
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion") |
|
|
|
def generate_response(user_input): |
|
|
|
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt") |
|
output = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id) |
|
response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True) |
|
|
|
|
|
emotion_result = emotion_pipeline(user_input) |
|
emotion = emotion_result[0]["label"] |
|
|
|
return response, emotion |
|
|
|
iface = gr.Interface( |
|
fn=generate_response, |
|
inputs=gr.Textbox(label="Enter your message"), |
|
outputs=[gr.Textbox(label="Chatbot Response"), gr.Textbox(label="Emotion Detected")], |
|
live=True |
|
) |
|
|
|
iface.launch() |
|
|