Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,62 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
# Suggestion Database
|
5 |
-
suggestion_database = {
|
6 |
-
"sadness": {
|
7 |
-
"suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
|
8 |
-
"articles": [
|
9 |
-
{"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
|
10 |
-
{"title": "Understanding Depression", "url": "https://example.com/sadness2"},
|
11 |
-
],
|
12 |
-
"videos": [
|
13 |
-
{"title": "Mindfulness for Sadness", "url": "https://www.youtube.com/watch?v=sadnessvideo1"},
|
14 |
-
{"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"},
|
15 |
-
],
|
16 |
-
},
|
17 |
-
"joy": {
|
18 |
-
"suggestions": ["Practice gratitude", "Engage in a hobby", "Spend time with loved ones"],
|
19 |
-
"articles": [
|
20 |
-
{"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
|
21 |
-
{"title": "Maintaining Positive Emotions", "url": "https://example.com/joy2"},
|
22 |
-
],
|
23 |
-
"videos": [
|
24 |
-
{"title": "Boosting Your Happiness", "url": "https://www.youtube.com/watch?v=joyvideo1"},
|
25 |
-
{"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
|
26 |
-
],
|
27 |
-
},
|
28 |
-
"neutral": {
|
29 |
-
"suggestions": ["Take a break", "Engage in a relaxing activity", "Spend time in nature"],
|
30 |
-
"articles": [
|
31 |
-
{"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
|
32 |
-
{"title": "Stress Management Techniques", "url": "https://example.com/stress1"},
|
33 |
-
],
|
34 |
-
"videos": [
|
35 |
-
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
|
36 |
-
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"},
|
37 |
-
],
|
38 |
-
},
|
39 |
-
}
|
40 |
-
|
41 |
-
# Function to fetch relevant resources
|
42 |
-
def get_relevant_resources(emotion):
|
43 |
-
return suggestion_database.get(emotion, {"suggestions": [], "articles": [], "videos": []})
|
44 |
-
|
45 |
-
# Debugging Model Initialization
|
46 |
-
def load_emotion_model(model_path="path/to/model"):
|
47 |
-
if not os.path.exists(model_path):
|
48 |
-
st.error(f"Model file not found at {model_path}")
|
49 |
-
return None
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
else:
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
st.write(f"- [{article['title']}]({article['url']})")
|
103 |
-
|
104 |
-
st.subheader("Videos to Watch:")
|
105 |
-
for video in resources["videos"]:
|
106 |
-
st.write(f"- [{video['title']}]({video['url']})")
|
107 |
-
|
108 |
-
# Run the App
|
109 |
-
if __name__ == "__main__":
|
110 |
-
main()
|
|
|
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 |
+
# Streamlit App UI
|
37 |
+
st.title("Personalized Self-Care Strategy App")
|
38 |
+
st.markdown("### Answer the following questions to get personalized self-care suggestions.")
|
39 |
+
|
40 |
+
# List of questions for user to answer
|
41 |
+
questions = [
|
42 |
+
"1. How do you feel about your overall health today?",
|
43 |
+
"2. How have you been sleeping recently?",
|
44 |
+
"3. Do you feel overwhelmed with tasks or emotions?",
|
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 |
+
responses.append(st.text_input(question, placeholder="Type your response here..."))
|
53 |
+
|
54 |
+
# Button to analyze and provide self-care suggestions
|
55 |
+
if st.button("Get Self-Care Suggestions"):
|
56 |
+
if all(responses): # Ensure all questions are answered
|
57 |
+
suggestions = analyze_and_suggest(responses)
|
58 |
+
st.markdown("### **Your Personalized Suggestions**")
|
59 |
+
for i, suggestion in enumerate(suggestions, 1):
|
60 |
+
st.write(f"**{i}.** {suggestion}")
|
61 |
+
else:
|
62 |
+
st.error("Please answer all the questions before proceeding.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|