Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,16 @@
|
|
|
|
|
|
1 |
|
2 |
-
|
3 |
-
|
4 |
-
import
|
5 |
-
|
|
|
6 |
|
7 |
import streamlit as st
|
8 |
from transformers import pipeline
|
9 |
|
10 |
-
# Load the pre-trained model
|
11 |
@st.cache_resource
|
12 |
def load_model():
|
13 |
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
@@ -19,7 +22,7 @@ def analyze_sentiment(user_input):
|
|
19 |
result = sentiment_model(user_input)[0]
|
20 |
sentiment = result['label']
|
21 |
|
22 |
-
if sentiment
|
23 |
return "Stay positive! π You can handle anything that comes your way."
|
24 |
return "You're on the right track! Keep shining! π"
|
25 |
|
@@ -27,7 +30,7 @@ def analyze_sentiment(user_input):
|
|
27 |
st.title("Student Sentiment Analysis Chatbot")
|
28 |
st.write("This chatbot detects your mood and provides positive or motivational messages.")
|
29 |
|
30 |
-
# User input
|
31 |
user_input = st.text_area("Enter your text here:")
|
32 |
|
33 |
# Button to analyze sentiment
|
@@ -37,3 +40,4 @@ if st.button("Analyze Sentiment"):
|
|
37 |
else:
|
38 |
message = analyze_sentiment(user_input)
|
39 |
st.success(message)
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
3 |
|
4 |
+
# Ensure dependencies are installed
|
5 |
+
try:
|
6 |
+
import transformers
|
7 |
+
except ImportError:
|
8 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "transformers", "torch"])
|
9 |
|
10 |
import streamlit as st
|
11 |
from transformers import pipeline
|
12 |
|
13 |
+
# Load the pre-trained model (cached for performance)
|
14 |
@st.cache_resource
|
15 |
def load_model():
|
16 |
return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
|
|
|
22 |
result = sentiment_model(user_input)[0]
|
23 |
sentiment = result['label']
|
24 |
|
25 |
+
if sentiment in ['NEGATIVE', 'NEUTRAL']:
|
26 |
return "Stay positive! π You can handle anything that comes your way."
|
27 |
return "You're on the right track! Keep shining! π"
|
28 |
|
|
|
30 |
st.title("Student Sentiment Analysis Chatbot")
|
31 |
st.write("This chatbot detects your mood and provides positive or motivational messages.")
|
32 |
|
33 |
+
# User input section
|
34 |
user_input = st.text_area("Enter your text here:")
|
35 |
|
36 |
# Button to analyze sentiment
|
|
|
40 |
else:
|
41 |
message = analyze_sentiment(user_input)
|
42 |
st.success(message)
|
43 |
+
|