File size: 1,091 Bytes
01c3c58 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import gradio as gr
from transformers import pipeline
# Load the pre-trained model (cached for performance)
def load_model():
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
sentiment_model = load_model()
# Define the function to analyze sentiment
def analyze_sentiment(user_input):
result = sentiment_model(user_input)[0]
sentiment = result['label']
if sentiment in ['NEGATIVE', 'NEUTRAL']:
return "Stay positive! π You can handle anything that comes your way."
return "You're on the right track! Keep shining! π"
# Gradio UI
def chatbot_ui():
# Define the interface
interface = gr.Interface(
fn=analyze_sentiment,
inputs=gr.Textbox(label="Enter your text here:"),
outputs=gr.Textbox(label="Motivational Message"),
title="Student Sentiment Analysis Chatbot",
description="This chatbot detects your mood and provides positive or motivational messages."
)
return interface
# Launch the interface
if __name__ == "__main__":
chatbot_ui().launch()
|