Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
# Set Page Configuration
|
| 5 |
-
st.set_page_config(
|
| 6 |
-
page_title="Emotion Prediction App",
|
| 7 |
-
page_icon="🌟",
|
| 8 |
-
layout="wide",
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
# Custom CSS for Background and Styling
|
| 12 |
-
def add_custom_styles():
|
| 13 |
-
st.markdown("""
|
| 14 |
-
<style>
|
| 15 |
-
/* Background Styling */
|
| 16 |
-
.stApp {
|
| 17 |
-
background: linear-gradient(to right, #f2f7ff, #d9faff);
|
| 18 |
-
color: #333333;
|
| 19 |
-
font-family: Arial, sans-serif;
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
/* Header Styling */
|
| 23 |
-
.main-title {
|
| 24 |
-
color: #1e88e5;
|
| 25 |
-
text-align: center;
|
| 26 |
-
font-size: 3rem;
|
| 27 |
-
margin-top: -20px;
|
| 28 |
-
margin-bottom: 30px;
|
| 29 |
-
font-weight: bold;
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
/* Section Headers */
|
| 33 |
-
.section-header {
|
| 34 |
-
color: #0d47a1;
|
| 35 |
-
font-size: 1.8rem;
|
| 36 |
-
margin-top: 20px;
|
| 37 |
-
margin-bottom: 10px;
|
| 38 |
-
border-bottom: 2px solid #1e88e5;
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
/* Suggestions Styling */
|
| 42 |
-
.suggestions {
|
| 43 |
-
font-size: 1.1rem;
|
| 44 |
-
line-height: 1.6;
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
/* Footer Styling */
|
| 48 |
-
.footer {
|
| 49 |
-
text-align: center;
|
| 50 |
-
margin-top: 50px;
|
| 51 |
-
font-size: 0.9rem;
|
| 52 |
-
color: #555;
|
| 53 |
-
}
|
| 54 |
-
</style>
|
| 55 |
-
""", unsafe_allow_html=True)
|
| 56 |
-
|
| 57 |
-
add_custom_styles()
|
| 58 |
-
|
| 59 |
-
# Debugging Logger
|
| 60 |
-
def debug_log(message):
|
| 61 |
-
st.sidebar.text(f"DEBUG: {message}")
|
| 62 |
-
|
| 63 |
# Suggestion Database
|
| 64 |
suggestion_database = {
|
| 65 |
"sadness": {
|
| 66 |
-
"suggestions": ["Try guided meditation", "Take a walk in nature", "Connect with a friend"],
|
| 67 |
"articles": [
|
| 68 |
{"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
|
| 69 |
{"title": "Understanding Depression", "url": "https://example.com/sadness2"},
|
|
@@ -97,62 +38,73 @@ suggestion_database = {
|
|
| 97 |
},
|
| 98 |
}
|
| 99 |
|
| 100 |
-
# Function to
|
| 101 |
def get_relevant_resources(emotion):
|
| 102 |
-
return suggestion_database.get(emotion,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
# Placeholder for Model Loading
|
| 105 |
-
def load_emotion_model(model_path):
|
| 106 |
try:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
return
|
| 111 |
except Exception as e:
|
| 112 |
-
|
| 113 |
return None
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
#
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
#
|
| 141 |
-
st.
|
| 142 |
-
st.
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
st.
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 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"},
|
|
|
|
| 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 |
try:
|
| 52 |
+
# Replace with actual model loading logic
|
| 53 |
+
model = "Dummy model loaded"
|
| 54 |
+
st.success("Model loaded successfully!")
|
| 55 |
+
return model
|
| 56 |
except Exception as e:
|
| 57 |
+
st.error(f"Error loading model: {e}")
|
| 58 |
return None
|
| 59 |
|
| 60 |
+
# Analyze User Input and Provide Suggestions
|
| 61 |
+
def analyze_emotion(user_input, model):
|
| 62 |
+
if model is None:
|
| 63 |
+
return "neutral" # Default to neutral if model failed to load
|
| 64 |
+
try:
|
| 65 |
+
# Dummy emotion analysis (replace with model prediction logic)
|
| 66 |
+
if "sad" in user_input.lower():
|
| 67 |
+
return "sadness"
|
| 68 |
+
elif "happy" in user_input.lower() or "joy" in user_input.lower():
|
| 69 |
+
return "joy"
|
| 70 |
+
else:
|
| 71 |
+
return "neutral"
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"Error during emotion analysis: {e}")
|
| 74 |
+
return "neutral"
|
| 75 |
+
|
| 76 |
+
# Streamlit Interface
|
| 77 |
+
def main():
|
| 78 |
+
st.title("Emotion-Based Suggestions")
|
| 79 |
+
|
| 80 |
+
# Load Model
|
| 81 |
+
st.sidebar.title("Model Loader")
|
| 82 |
+
model_path = st.sidebar.text_input("Model Path", "path/to/model")
|
| 83 |
+
model = load_emotion_model(model_path)
|
| 84 |
+
|
| 85 |
+
# User Input
|
| 86 |
+
st.header("How are you feeling today?")
|
| 87 |
+
user_input = st.text_input("Describe your mood in a few words:")
|
| 88 |
+
|
| 89 |
+
if user_input:
|
| 90 |
+
# Analyze Emotion
|
| 91 |
+
emotion = analyze_emotion(user_input, model)
|
| 92 |
+
st.subheader(f"Detected Emotion: {emotion.capitalize()}")
|
| 93 |
+
|
| 94 |
+
# Fetch and Display Suggestions
|
| 95 |
+
resources = get_relevant_resources(emotion)
|
| 96 |
+
st.subheader("Suggestions for You:")
|
| 97 |
+
for suggestion in resources["suggestions"]:
|
| 98 |
+
st.write(f"- {suggestion}")
|
| 99 |
+
|
| 100 |
+
st.subheader("Articles to Explore:")
|
| 101 |
+
for article in resources["articles"]:
|
| 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()
|