Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
-
import torch
|
4 |
-
import time
|
5 |
|
6 |
-
#
|
7 |
suggestion_database = {
|
8 |
"sadness": {
|
9 |
"suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
|
@@ -36,118 +33,64 @@ suggestion_database = {
|
|
36 |
"videos": [
|
37 |
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
|
38 |
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"},
|
39 |
-
]
|
40 |
-
}
|
41 |
}
|
42 |
|
43 |
-
# Function to fetch
|
44 |
def get_relevant_resources(emotion):
|
45 |
resources = suggestion_database.get(emotion, {})
|
46 |
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
except Exception as e:
|
59 |
-
st.write(f"Error loading the model: {e}")
|
60 |
-
return None
|
61 |
-
|
62 |
-
# Function to predict emotion for a single response
|
63 |
-
def predict_emotion_single(response, emotion_analyzer):
|
64 |
-
if emotion_analyzer is None:
|
65 |
-
st.error("Model not loaded. Please try reloading the app.")
|
66 |
-
return {"Error": "Emotion analyzer model not initialized. Please check model loading."}
|
67 |
-
|
68 |
-
try:
|
69 |
-
result = emotion_analyzer([response])
|
70 |
-
return {res["label"]: round(res["score"], 4) for res in result}
|
71 |
-
except Exception as e:
|
72 |
-
st.error(f"Prediction failed: {e}")
|
73 |
-
return {"Error": f"Prediction failed: {e}"}
|
74 |
-
|
75 |
-
# Streamlit App Layout
|
76 |
-
st.title("Emotion Prediction App: Your Personal Wellness Assistant")
|
77 |
-
|
78 |
-
st.write("**How it works:**")
|
79 |
-
st.write("- Enter your thoughts or feelings.")
|
80 |
-
st.write("- Our AI analyzes your text to predict your emotional state.")
|
81 |
-
st.write("- Receive personalized suggestions to improve your well-being.")
|
82 |
-
|
83 |
-
# Define questions for the user
|
84 |
-
questions = [
|
85 |
-
"How are you feeling today?",
|
86 |
-
"Describe your mood in a few words.",
|
87 |
-
"What was the most significant emotion you felt this week?"
|
88 |
-
]
|
89 |
-
|
90 |
-
# Initialize a dictionary to store responses
|
91 |
-
responses = {}
|
92 |
-
|
93 |
-
# Initialize the emotion analysis model with retries
|
94 |
-
emotion_analyzer = None
|
95 |
-
max_retries = 3
|
96 |
-
retry_delay = 5 # seconds
|
97 |
-
|
98 |
-
# Try loading the model with retries
|
99 |
-
for attempt in range(max_retries):
|
100 |
-
emotion_analyzer = load_model()
|
101 |
-
if emotion_analyzer:
|
102 |
-
break
|
103 |
-
if attempt < max_retries - 1:
|
104 |
-
st.warning(f"Retrying model load... Attempt {attempt + 2}/{max_retries}")
|
105 |
-
time.sleep(retry_delay)
|
106 |
-
else:
|
107 |
-
st.error("Model failed to load after multiple attempts. Please try again later.")
|
108 |
-
|
109 |
-
# Function to handle responses and emotion analysis
|
110 |
-
for i, question in enumerate(questions, start=1):
|
111 |
-
user_response = st.text_input(f"Question {i}: {question}")
|
112 |
-
if user_response:
|
113 |
-
analysis = predict_emotion_single(user_response, emotion_analyzer)
|
114 |
-
responses[question] = (user_response, analysis)
|
115 |
-
st.write(f"**Your Response**: {user_response}")
|
116 |
-
st.write(f"**Emotion Analysis**: {analysis}")
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
suggestions, articles, videos = get_relevant_resources(max_emotion)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
st.write("### 🧘 No suggestions available at the moment.")
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
for article in articles:
|
130 |
-
st.write(f"[{article['title']}]({article['url']})")
|
131 |
-
else:
|
132 |
-
st.write("### 📚 No articles available at the moment.")
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
for video in videos:
|
137 |
-
st.write(f"[{video['title']}]({video['url']})")
|
138 |
-
else:
|
139 |
-
st.write("### 🎥 No videos available at the moment.")
|
140 |
|
141 |
-
#
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
144 |
|
145 |
-
#
|
146 |
-
if st.button("
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
st.write(f"Emotion Analysis: {analysis}")
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
+
# Suggestion Database
|
4 |
suggestion_database = {
|
5 |
"sadness": {
|
6 |
"suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
|
|
|
33 |
"videos": [
|
34 |
{"title": "Relaxation Techniques", "url": "https://www.youtube.com/watch?v=relaxvideo1"},
|
35 |
{"title": "Mindfulness Exercises", "url": "https://www.youtube.com/watch?v=mindfulnessvideo1"},
|
36 |
+
],
|
37 |
+
},
|
38 |
}
|
39 |
|
40 |
+
# Function to fetch resources
|
41 |
def get_relevant_resources(emotion):
|
42 |
resources = suggestion_database.get(emotion, {})
|
43 |
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
44 |
|
45 |
+
# Suggestion Logic
|
46 |
+
def suggest_activity(emotion_analysis):
|
47 |
+
max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "neutral"
|
48 |
+
suggestions, articles, videos = get_relevant_resources(max_emotion)
|
49 |
+
return {
|
50 |
+
"emotion": max_emotion,
|
51 |
+
"suggestions": suggestions,
|
52 |
+
"articles": articles,
|
53 |
+
"videos": videos,
|
54 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
# Streamlit Interface
|
57 |
+
st.title("Personalized Emotional Wellness Recommendations")
|
|
|
58 |
|
59 |
+
# Questions
|
60 |
+
st.write("### How are you feeling today?")
|
61 |
+
emotion_input = st.text_input("Your response (e.g., happy, sad, neutral):", "").lower()
|
|
|
62 |
|
63 |
+
st.write("### Describe your mood in a few words.")
|
64 |
+
mood_input = st.text_input("Your response (e.g., calm, frustrated, joyful):", "").lower()
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
st.write("### What was the most significant emotion you felt this week?")
|
67 |
+
emotion_week_input = st.text_input("Your response (e.g., sadness, joy, anxiety):", "").lower()
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
# Simulated Emotion Analysis (Here, based on user input; replace with real analysis later)
|
70 |
+
emotion_analysis = {
|
71 |
+
"sadness": emotion_input == "sad" or emotion_week_input == "sadness",
|
72 |
+
"joy": emotion_input == "happy" or emotion_week_input == "joy",
|
73 |
+
"neutral": emotion_input == "neutral" or emotion_week_input == "calm",
|
74 |
+
}
|
75 |
|
76 |
+
# Analyze and Provide Suggestions
|
77 |
+
if st.button("Get Suggestions"):
|
78 |
+
analysis_results = {
|
79 |
+
"sadness": emotion_analysis.get("sadness", 0),
|
80 |
+
"joy": emotion_analysis.get("joy", 0),
|
81 |
+
"neutral": emotion_analysis.get("neutral", 0),
|
82 |
+
}
|
|
|
83 |
|
84 |
+
suggestions = suggest_activity(analysis_results)
|
85 |
+
st.write(f"### Detected Emotion: {suggestions['emotion'].capitalize()}")
|
86 |
+
st.write("### Suggestions for You:")
|
87 |
+
for suggestion in suggestions["suggestions"]:
|
88 |
+
st.write(f"- {suggestion}")
|
89 |
+
|
90 |
+
st.write("### Articles to Explore:")
|
91 |
+
for article in suggestions["articles"]:
|
92 |
+
st.markdown(f"[{article['title']}]({article['url']})")
|
93 |
+
|
94 |
+
st.write("### Videos to Watch:")
|
95 |
+
for video in suggestions["videos"]:
|
96 |
+
st.markdown(f"[{video['title']}]({video['url']})")
|