Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,106 @@
|
|
1 |
-
# Install necessary libraries (if not already installed)
|
2 |
-
# pip install streamlit transformers datasets
|
3 |
-
|
4 |
import streamlit as st
|
5 |
from transformers import pipeline
|
6 |
-
from datasets import load_dataset
|
7 |
-
|
8 |
-
# Load the pre-trained model for sentiment analysis (using a valid model from Hugging Face)
|
9 |
-
emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2")
|
10 |
-
|
11 |
-
# Load a dataset from Hugging Face (Sentiment Analysis - SST-2)
|
12 |
-
dataset = load_dataset("glue", "sst2")
|
13 |
-
|
14 |
-
# Example of how to use a dataset (showing the first few examples)
|
15 |
-
st.write("Dataset Sample (SST-2):")
|
16 |
-
st.write(dataset["train"][0:3]) # Display the first 3 samples
|
17 |
-
|
18 |
-
# Define the function to analyze emotions and suggest strategies
|
19 |
-
def analyze_and_suggest(responses):
|
20 |
-
suggestions = []
|
21 |
-
for response in responses:
|
22 |
-
# Get the sentiment analysis result
|
23 |
-
result = emotion_analyzer(response)[0]
|
24 |
-
label = result['label']
|
25 |
-
|
26 |
-
# Suggest strategies based on sentiment
|
27 |
-
if label == "NEGATIVE":
|
28 |
-
suggestions.append("Try deep breathing exercises or mindfulness activities.")
|
29 |
-
elif label == "POSITIVE":
|
30 |
-
suggestions.append("Great! Keep the positivity going with a walk or some light exercise.")
|
31 |
-
else:
|
32 |
-
suggestions.append("Consider focusing on better sleep or reflecting on your priorities.")
|
33 |
-
|
34 |
-
return suggestions
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
questions = [
|
42 |
-
"
|
43 |
-
"
|
44 |
-
"
|
45 |
-
"4. What are your energy levels like today?",
|
46 |
-
"5. How often do you exercise or engage in physical activity?"
|
47 |
]
|
48 |
|
49 |
-
# Collect user responses
|
50 |
responses = []
|
51 |
for question in questions:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
if
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
else:
|
62 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load the emotion analysis pipeline using an open-access model
|
5 |
+
emotion_analyzer = pipeline(
|
6 |
+
"text-classification",
|
7 |
+
model="j-hartmann/emotion-english-distilroberta-base"
|
8 |
+
)
|
9 |
+
|
10 |
+
# App title and description
|
11 |
+
st.set_page_config(page_title="Hawaii Emotion Wellness", layout="centered", page_icon="🌴")
|
12 |
+
st.markdown(
|
13 |
+
"""
|
14 |
+
<style>
|
15 |
+
body {
|
16 |
+
background-color: #E0F7FA;
|
17 |
+
color: #004D40;
|
18 |
+
}
|
19 |
+
.main-header {
|
20 |
+
font-size: 36px;
|
21 |
+
font-weight: bold;
|
22 |
+
text-align: center;
|
23 |
+
margin-bottom: 10px;
|
24 |
+
}
|
25 |
+
.sub-header {
|
26 |
+
font-size: 18px;
|
27 |
+
text-align: center;
|
28 |
+
margin-bottom: 20px;
|
29 |
+
}
|
30 |
+
.suggestion-card {
|
31 |
+
background-color: #B2EBF2;
|
32 |
+
padding: 15px;
|
33 |
+
border-radius: 8px;
|
34 |
+
margin-bottom: 15px;
|
35 |
+
}
|
36 |
+
</style>
|
37 |
+
""",
|
38 |
+
unsafe_allow_html=True,
|
39 |
+
)
|
40 |
|
41 |
+
st.markdown('<div class="main-header">🌺 Hawaii Emotion Wellness App 🌴</div>', unsafe_allow_html=True)
|
42 |
+
st.markdown('<div class="sub-header">Understand your emotions and find the right balance in paradise.</div>', unsafe_allow_html=True)
|
43 |
+
|
44 |
+
# Step 1: Collect user's responses
|
45 |
+
st.markdown("### Answer these three questions to get started:")
|
46 |
questions = [
|
47 |
+
"How are you feeling right now? (e.g., stressed, happy, sad)",
|
48 |
+
"What is the most pressing issue on your mind currently?",
|
49 |
+
"On a scale of 1-10, how motivated do you feel to take care of yourself today?",
|
|
|
|
|
50 |
]
|
51 |
|
|
|
52 |
responses = []
|
53 |
for question in questions:
|
54 |
+
response = st.text_input(question)
|
55 |
+
responses.append(response)
|
56 |
+
|
57 |
+
# Analyze the emotions if the user has answered all questions
|
58 |
+
if st.button("Analyze Emotions"):
|
59 |
+
if all(responses):
|
60 |
+
# Aggregate responses into a single input for emotion analysis
|
61 |
+
aggregated_response = " ".join(responses)
|
62 |
+
emotion_results = emotion_analyzer(aggregated_response)
|
63 |
+
|
64 |
+
# Get the most likely emotion
|
65 |
+
predicted_emotion = emotion_results[0]["label"]
|
66 |
+
st.markdown(f"### Your Predicted Emotion: **{predicted_emotion}** 🎭")
|
67 |
+
|
68 |
+
# Provide well-being suggestions
|
69 |
+
st.markdown("### Here's what we recommend for you:")
|
70 |
+
suggestions = {
|
71 |
+
"joy": [
|
72 |
+
{"activity": "Go for a walk on the beach", "url": "https://www.hawaiibeachwalks.com"},
|
73 |
+
{"activity": "Try a short surfing session", "url": "https://www.learnsurf.com"},
|
74 |
+
{"activity": "Join a hula dancing class", "url": "https://www.hulahawaii.com"},
|
75 |
+
],
|
76 |
+
"sadness": [
|
77 |
+
{"activity": "Practice deep breathing for 5 minutes", "url": "https://www.breathingexercise.com"},
|
78 |
+
{"activity": "Watch a calming ocean video", "url": "https://www.youtube.com/watch?v=lM02vNMRRB0"},
|
79 |
+
{"activity": "Do a quick yoga session", "url": "https://www.doyogawithme.com"},
|
80 |
+
],
|
81 |
+
# Add more emotions as needed
|
82 |
+
}
|
83 |
+
|
84 |
+
user_suggestions = suggestions.get(predicted_emotion.lower(), [])
|
85 |
+
if user_suggestions:
|
86 |
+
for suggestion in user_suggestions:
|
87 |
+
st.markdown(
|
88 |
+
f"""
|
89 |
+
<div class="suggestion-card">
|
90 |
+
<strong>{suggestion['activity']}</strong>
|
91 |
+
<br>
|
92 |
+
<a href="{suggestion['url']}" target="_blank">Learn More</a>
|
93 |
+
</div>
|
94 |
+
""",
|
95 |
+
unsafe_allow_html=True,
|
96 |
+
)
|
97 |
+
else:
|
98 |
+
st.markdown("Stay positive! Enjoy the aloha spirit and take a deep breath.")
|
99 |
else:
|
100 |
+
st.warning("Please answer all the questions to analyze your emotions.")
|
101 |
+
|
102 |
+
# Footer
|
103 |
+
st.markdown("---")
|
104 |
+
st.markdown(
|
105 |
+
"Built with ❤️ for the Hawaii Hackathon 2024 by [Your Team Name]. Deployed on Hugging Face Spaces."
|
106 |
+
)
|