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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -108
app.py CHANGED
@@ -1,110 +1,62 @@
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"},
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
- 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()
 
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.")