Madiharehan's picture
Update app.py
025ebfe verified
raw
history blame
1.97 kB
import gradio as gr
from transformers import pipeline
# Load the pre-trained model (using the trained model you provided)
def load_model():
# Use your trained model here; if it's hosted on Hugging Face, provide the path or URL to the model
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
# Initialize the model
sentiment_model = load_model()
# Function to analyze sentiment and provide motivational feedback
def analyze_sentiment(user_input):
result = sentiment_model(user_input)[0]
sentiment = result['label'].lower() # Convert to lowercase for easier comparison
# Analyze the mood and provide motivational messages accordingly
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!"
)
# Gradio UI
def chatbot_ui():
# Define the Gradio interface
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
# Launch the interface
if __name__ == "__main__":
chatbot_ui().launch()