Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -40,7 +40,7 @@ suggestion_database = {
|
|
40 |
}
|
41 |
}
|
42 |
|
43 |
-
# Function to fetch relevant resources
|
44 |
def get_relevant_resources(emotion):
|
45 |
resources = suggestion_database.get(emotion, {})
|
46 |
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
@@ -50,8 +50,9 @@ def get_relevant_resources(emotion):
|
|
50 |
def load_model():
|
51 |
try:
|
52 |
st.write("Attempting to load the emotion analysis model...")
|
53 |
-
#
|
54 |
-
|
|
|
55 |
st.write("Model loaded successfully!")
|
56 |
return emotion_analyzer
|
57 |
except Exception as e:
|
@@ -63,7 +64,7 @@ def predict_emotion_single(response, emotion_analyzer):
|
|
63 |
if emotion_analyzer is None:
|
64 |
st.error("Model not loaded. Please try reloading the app.")
|
65 |
return {"Error": "Emotion analyzer model not initialized. Please check model loading."}
|
66 |
-
|
67 |
try:
|
68 |
result = emotion_analyzer([response])
|
69 |
return {res["label"]: round(res["score"], 4) for res in result}
|
@@ -72,8 +73,12 @@ def predict_emotion_single(response, emotion_analyzer):
|
|
72 |
return {"Error": f"Prediction failed: {e}"}
|
73 |
|
74 |
# Streamlit App Layout
|
75 |
-
st.title("
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# Define questions for the user
|
79 |
questions = [
|
@@ -109,25 +114,25 @@ for i, question in enumerate(questions, start=1):
|
|
109 |
responses[question] = (user_response, analysis)
|
110 |
st.write(f"**Your Response**: {user_response}")
|
111 |
st.write(f"**Emotion Analysis**: {analysis}")
|
112 |
-
|
113 |
# Based on the emotion, suggest activities, articles, and videos
|
114 |
max_emotion = max(analysis, key=analysis.get) if analysis else "neutral"
|
115 |
suggestions, articles, videos = get_relevant_resources(max_emotion)
|
116 |
-
|
117 |
if suggestions:
|
118 |
st.write(f"### π§ Suggested Activity: {suggestions[0]}")
|
119 |
else:
|
120 |
st.write("### π§ No suggestions available at the moment.")
|
121 |
-
|
122 |
if articles:
|
123 |
-
st.write(f"### π Suggested Articles:")
|
124 |
for article in articles:
|
125 |
st.write(f"[{article['title']}]({article['url']})")
|
126 |
else:
|
127 |
st.write("### π No articles available at the moment.")
|
128 |
-
|
129 |
if videos:
|
130 |
-
st.write(f"### π₯ Suggested Videos:")
|
131 |
for video in videos:
|
132 |
st.write(f"[{video['title']}]({video['url']})")
|
133 |
else:
|
|
|
40 |
}
|
41 |
}
|
42 |
|
43 |
+
# Function to fetch relevant resources
|
44 |
def get_relevant_resources(emotion):
|
45 |
resources = suggestion_database.get(emotion, {})
|
46 |
return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
|
|
|
50 |
def load_model():
|
51 |
try:
|
52 |
st.write("Attempting to load the emotion analysis model...")
|
53 |
+
# Check if CUDA (GPU) is available, if not use CPU
|
54 |
+
device = 0 if torch.cuda.is_available() else -1
|
55 |
+
emotion_analyzer = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta", device=device)
|
56 |
st.write("Model loaded successfully!")
|
57 |
return emotion_analyzer
|
58 |
except Exception as e:
|
|
|
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}
|
|
|
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 = [
|
|
|
114 |
responses[question] = (user_response, analysis)
|
115 |
st.write(f"**Your Response**: {user_response}")
|
116 |
st.write(f"**Emotion Analysis**: {analysis}")
|
117 |
+
|
118 |
# Based on the emotion, suggest activities, articles, and videos
|
119 |
max_emotion = max(analysis, key=analysis.get) if analysis else "neutral"
|
120 |
suggestions, articles, videos = get_relevant_resources(max_emotion)
|
121 |
+
|
122 |
if suggestions:
|
123 |
st.write(f"### π§ Suggested Activity: {suggestions[0]}")
|
124 |
else:
|
125 |
st.write("### π§ No suggestions available at the moment.")
|
126 |
+
|
127 |
if articles:
|
128 |
+
st.write(f"### π Suggested Articles: ")
|
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 |
if videos:
|
135 |
+
st.write(f"### π₯ Suggested Videos: ")
|
136 |
for video in videos:
|
137 |
st.write(f"[{video['title']}]({video['url']})")
|
138 |
else:
|