Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# Load the pre-trained model (
|
5 |
def load_model():
|
|
|
6 |
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
7 |
|
|
|
8 |
sentiment_model = load_model()
|
9 |
|
10 |
-
#
|
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 |
-
#
|
16 |
if sentiment == 'negative':
|
17 |
-
return
|
|
|
|
|
|
|
18 |
elif sentiment == 'neutral':
|
19 |
-
return
|
|
|
|
|
|
|
20 |
elif sentiment == 'positive':
|
21 |
-
return
|
|
|
|
|
|
|
22 |
else:
|
23 |
-
return
|
|
|
|
|
|
|
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
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load the pre-trained model (using the trained model you provided)
|
5 |
def load_model():
|
6 |
+
# Use your trained model here; if it's hosted on Hugging Face, provide the path or URL to the model
|
7 |
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
8 |
|
9 |
+
# Initialize the model
|
10 |
sentiment_model = load_model()
|
11 |
|
12 |
+
# Function to analyze sentiment and provide motivational feedback
|
13 |
def analyze_sentiment(user_input):
|
14 |
result = sentiment_model(user_input)[0]
|
15 |
sentiment = result['label'].lower() # Convert to lowercase for easier comparison
|
16 |
+
|
17 |
+
# Analyze the mood and provide motivational messages accordingly
|
18 |
if sentiment == 'negative':
|
19 |
+
return (
|
20 |
+
"Mood Detected: Negative π\n\n"
|
21 |
+
"Stay positive! π Remember, tough times don't last, but tough people do!"
|
22 |
+
)
|
23 |
elif sentiment == 'neutral':
|
24 |
+
return (
|
25 |
+
"Mood Detected: Neutral π\n\n"
|
26 |
+
"It's good to reflect on steady days. Keep your goals in mind, and stay motivated!"
|
27 |
+
)
|
28 |
elif sentiment == 'positive':
|
29 |
+
return (
|
30 |
+
"Mood Detected: Positive π\n\n"
|
31 |
+
"You're on the right track! Keep shining! π"
|
32 |
+
)
|
33 |
else:
|
34 |
+
return (
|
35 |
+
"Mood Detected: Unknown π€\n\n"
|
36 |
+
"Keep going, you're doing great!"
|
37 |
+
)
|
38 |
|
39 |
# Gradio UI
|
40 |
def chatbot_ui():
|
41 |
+
# Define the Gradio interface
|
42 |
interface = gr.Interface(
|
43 |
fn=analyze_sentiment,
|
44 |
+
inputs=gr.Textbox(label="Enter your text here:", placeholder="Type your feelings or thoughts..."),
|
45 |
outputs=gr.Textbox(label="Motivational Message"),
|
46 |
title="Student Sentiment Analysis Chatbot",
|
47 |
+
description="This chatbot detects your mood and provides positive or motivational messages based on sentiment analysis."
|
48 |
)
|
49 |
|
50 |
return interface
|