tarrasyed19472007 commited on
Commit
d5c2a19
·
verified ·
1 Parent(s): c94affa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -113
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 Fetch Relevant Suggestions
101
  def get_relevant_resources(emotion):
102
- return suggestion_database.get(emotion, suggestion_database["neutral"])
 
 
 
 
 
 
103
 
104
- # Placeholder for Model Loading
105
- def load_emotion_model(model_path):
106
  try:
107
- if not os.path.exists(model_path):
108
- raise FileNotFoundError(f"Model file not found at {model_path}")
109
- debug_log("Model loaded successfully.")
110
- return "Emotion Model Placeholder"
111
  except Exception as e:
112
- debug_log(str(e))
113
  return None
114
 
115
- # Main Application Header
116
- st.markdown('<div class="main-title">Emotion Prediction & Suggestions 🌈</div>', unsafe_allow_html=True)
117
- st.markdown("""
118
- This app is designed to help you discover activities, articles, and videos tailored to your emotional state.
119
- Simply describe how you're feeling, and let us provide uplifting and insightful suggestions.
120
- """)
121
-
122
- # Sidebar for Model Path Input
123
- st.sidebar.header("Emotion Model Configuration")
124
- model_path = st.sidebar.text_input("Enter the Path to the Emotion Prediction Model", "path/to/model")
125
-
126
- # Load Emotion Prediction Model
127
- emotion_model = load_emotion_model(model_path)
128
- if emotion_model is None:
129
- st.sidebar.error("Model failed to load. Please check the path or contact support.")
130
-
131
- # Emotion Analysis Section
132
- st.markdown('<div class="section-header">How Are You Feeling Today?</div>', unsafe_allow_html=True)
133
- user_response = st.text_input("Describe your current emotion (e.g., happy, sad, neutral):", "neutral")
134
-
135
- # Display Suggestions
136
- if user_response:
137
- resources = get_relevant_resources(user_response.lower())
138
- st.markdown('<div class="section-header">Here Are Some Suggestions for You:</div>', unsafe_allow_html=True)
139
-
140
- # Suggestions
141
- st.markdown("#### Activities:")
142
- st.markdown('<div class="suggestions">', unsafe_allow_html=True)
143
- for suggestion in resources["suggestions"]:
144
- st.write(f"- {suggestion}")
145
- st.markdown('</div>', unsafe_allow_html=True)
146
-
147
- # Articles
148
- st.markdown("#### Articles:")
149
- for article in resources["articles"]:
150
- st.write(f"- [{article['title']}]({article['url']})")
151
-
152
- # Videos
153
- st.markdown("#### Videos:")
154
- for video in resources["videos"]:
155
- st.write(f"- [{video['title']}]({video['url']})")
156
-
157
- # Footer
158
- st.markdown('<div class="footer">Crafted with ❤️ by Your Emotion App Team</div>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
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()