Madiharehan
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# Load the pre-trained model (
|
5 |
def load_model():
|
6 |
-
# Use Hugging Face's 'cardiffnlp/twitter-roberta-base-sentiment' or a custom model path
|
7 |
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
8 |
|
9 |
-
# Initialize the model
|
10 |
sentiment_model = load_model()
|
11 |
|
12 |
-
#
|
13 |
def analyze_sentiment(user_input):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
if not result:
|
18 |
-
return "Could not determine sentiment. Please try again."
|
19 |
|
20 |
-
|
21 |
-
print(f"Sentiment Analysis Result: {result}") # Debug: Print model result for review
|
22 |
-
|
23 |
-
# Analyze the mood and provide motivational messages accordingly
|
24 |
if sentiment == 'negative':
|
25 |
-
return
|
26 |
-
"Mood Detected: Negative π\n\n"
|
27 |
-
"Stay positive! π Remember, tough times don't last, but tough people do!"
|
28 |
-
)
|
29 |
elif sentiment == 'neutral':
|
30 |
-
return
|
31 |
-
"Mood Detected: Neutral π\n\n"
|
32 |
-
"It's good to reflect on steady days. Keep your goals in mind, and stay motivated!"
|
33 |
-
)
|
34 |
elif sentiment == 'positive':
|
35 |
-
return
|
36 |
-
"Mood Detected: Positive π\n\n"
|
37 |
-
"You're on the right track! Keep shining! π"
|
38 |
-
)
|
39 |
else:
|
40 |
-
return
|
41 |
-
"Mood Detected: Unknown π€\n\n"
|
42 |
-
"Keep going, you're doing great!"
|
43 |
-
)
|
44 |
|
45 |
# Gradio UI
|
46 |
def chatbot_ui():
|
47 |
-
# Define the
|
48 |
interface = gr.Interface(
|
49 |
fn=analyze_sentiment,
|
50 |
-
inputs=gr.Textbox(label="Enter your text here:"
|
51 |
outputs=gr.Textbox(label="Motivational Message"),
|
52 |
title="Student Sentiment Analysis Chatbot",
|
53 |
-
description="This chatbot detects your mood and provides positive or motivational messages
|
54 |
)
|
55 |
|
56 |
return interface
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load the pre-trained model (cached for performance)
|
5 |
def load_model():
|
|
|
6 |
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
7 |
|
|
|
8 |
sentiment_model = load_model()
|
9 |
|
10 |
+
# Define the function to analyze sentiment
|
11 |
def analyze_sentiment(user_input):
|
12 |
+
result = sentiment_model(user_input)[0]
|
13 |
+
sentiment = result['label'].lower() # Convert to lowercase for easier comparison
|
|
|
|
|
|
|
14 |
|
15 |
+
# Customize messages based on detected sentiment
|
|
|
|
|
|
|
16 |
if sentiment == 'negative':
|
17 |
+
return "Mood Detected: Negative π\n\nStay positive! π Remember, tough times don't last, but tough people do!"
|
|
|
|
|
|
|
18 |
elif sentiment == 'neutral':
|
19 |
+
return "Mood Detected: Neutral π\n\nIt's good to reflect on steady days. Keep your goals in mind, and stay motivated!"
|
|
|
|
|
|
|
20 |
elif sentiment == 'positive':
|
21 |
+
return "Mood Detected: Positive π\n\nYou're on the right track! Keep shining! π"
|
|
|
|
|
|
|
22 |
else:
|
23 |
+
return "Mood Detected: Unknown π€\n\nKeep going, you're doing great!"
|
|
|
|
|
|
|
24 |
|
25 |
# Gradio UI
|
26 |
def chatbot_ui():
|
27 |
+
# Define the interface
|
28 |
interface = gr.Interface(
|
29 |
fn=analyze_sentiment,
|
30 |
+
inputs=gr.Textbox(label="Enter your text here:"),
|
31 |
outputs=gr.Textbox(label="Motivational Message"),
|
32 |
title="Student Sentiment Analysis Chatbot",
|
33 |
+
description="This chatbot detects your mood and provides positive or motivational messages."
|
34 |
)
|
35 |
|
36 |
return interface
|