File size: 2,198 Bytes
01c3c58 e9f4ddd 01c3c58 e9f4ddd 01c3c58 025ebfe 01c3c58 025ebfe 01c3c58 e9f4ddd 025ebfe 5f100b2 025ebfe 5f100b2 025ebfe 5f100b2 025ebfe 5f100b2 025ebfe 01c3c58 025ebfe 01c3c58 025ebfe 01c3c58 025ebfe 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import gradio as gr
from transformers import pipeline
# Load the pre-trained model (using the provided model or custom trained model)
def load_model():
# Use Hugging Face's 'cardiffnlp/twitter-roberta-base-sentiment' or a custom model path
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):
# Get sentiment prediction
result = sentiment_model(user_input)
if not result:
return "Could not determine sentiment. Please try again."
sentiment = result[0]['label'].lower() # Extract sentiment label and convert to lowercase for comparison
print(f"Sentiment Analysis Result: {result}") # Debug: Print model result for review
# 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()
|