|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
def load_model(): |
|
|
|
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment') |
|
|
|
|
|
sentiment_model = load_model() |
|
|
|
|
|
def analyze_sentiment(user_input): |
|
|
|
result = sentiment_model(user_input) |
|
|
|
if not result: |
|
return "Could not determine sentiment. Please try again." |
|
|
|
sentiment = result[0]['label'].lower() |
|
print(f"Sentiment Analysis Result: {result}") |
|
|
|
|
|
if sentiment == 'negative': |
|
return ( |
|
"Mood Detected: Negative π\n\n" |
|
"Stay positive! π Remember, tough times don't last, but tough people do!" |
|
) |
|
elif sentiment == 'neutral': |
|
return ( |
|
"Mood Detected: Neutral π\n\n" |
|
"It's good to reflect on steady days. Keep your goals in mind, and stay motivated!" |
|
) |
|
elif sentiment == 'positive': |
|
return ( |
|
"Mood Detected: Positive π\n\n" |
|
"You're on the right track! Keep shining! π" |
|
) |
|
else: |
|
return ( |
|
"Mood Detected: Unknown π€\n\n" |
|
"Keep going, you're doing great!" |
|
) |
|
|
|
|
|
def chatbot_ui(): |
|
|
|
interface = gr.Interface( |
|
fn=analyze_sentiment, |
|
inputs=gr.Textbox(label="Enter your text here:", placeholder="Type your feelings or thoughts..."), |
|
outputs=gr.Textbox(label="Motivational Message"), |
|
title="Student Sentiment Analysis Chatbot", |
|
description="This chatbot detects your mood and provides positive or motivational messages based on sentiment analysis." |
|
) |
|
|
|
return interface |
|
|
|
|
|
if __name__ == "__main__": |
|
chatbot_ui().launch() |
|
|