tarrasyed19472007 commited on
Commit
18119f4
·
verified ·
1 Parent(s): d4caaed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -36
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- # Load the emotion classification model
5
- emotion_analyzer = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
6
 
7
- # Enhanced Suggestion Database (Now includes resources)
8
  suggestion_database = {
9
- "sadness": {
10
  "suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
11
  "articles": [
12
  {"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
@@ -17,7 +17,7 @@ suggestion_database = {
17
  {"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"},
18
  ],
19
  },
20
- "joy": {
21
  "suggestions": ["Practice gratitude", "Engage in a hobby", "Spend time with loved ones"],
22
  "articles": [
23
  {"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
@@ -28,7 +28,7 @@ suggestion_database = {
28
  {"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
29
  ],
30
  },
31
- "neutral": {
32
  "suggestions": ["Take a break", "Engage in a relaxing activity", "Spend time in nature"],
33
  "articles": [
34
  {"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
@@ -46,9 +46,9 @@ def get_relevant_resources(emotion):
46
  resources = suggestion_database.get(emotion, {})
47
  return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
48
 
49
- # Enhanced Suggestion Function
50
  def suggest_activity(emotion_analysis):
51
- max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "neutral"
52
  suggestions, articles, videos = get_relevant_resources(max_emotion)
53
  return {
54
  "suggestions": suggestions,
@@ -56,33 +56,23 @@ def suggest_activity(emotion_analysis):
56
  "videos": videos,
57
  }
58
 
59
- # Streamlit app interface
60
- st.title("Emotion Prediction and Well-Being Suggestions")
61
-
62
- # Ask 3 questions to the user
63
- question1 = st.text_input("How are you feeling today?")
64
- question2 = st.text_input("Have you felt stressed recently?")
65
- question3 = st.text_input("Do you have enough time for relaxation?")
66
-
67
- if st.button("Get Suggestions"):
68
- # Combine responses to analyze sentiment
69
- user_input = f"{question1} {question2} {question3}"
70
-
71
- # Perform emotion analysis
72
- emotion_analysis = emotion_analyzer(user_input)
73
 
74
- # Get suggestions, articles, and videos based on the emotion
75
- resources = suggest_activity(emotion_analysis)
76
 
77
- # Display suggestions, articles, and videos
78
- st.write("Here are some suggestions to help you:")
79
- for suggestion in resources["suggestions"]:
80
- st.write(f"- {suggestion}")
81
-
82
- st.write("Articles you may find helpful:")
83
- for article in resources["articles"]:
84
- st.markdown(f"[{article['title']}]({article['url']})")
85
-
86
- st.write("Videos you may find helpful:")
87
- for video in resources["videos"]:
88
- st.markdown(f"[{video['title']}]({video['url']})")
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ # Emotion classifier
5
+ emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
6
 
7
+ # Enhanced Suggestion Database with resources
8
  suggestion_database = {
9
+ "NEGATIVE": {
10
  "suggestions": ["Try a guided meditation", "Take a walk in nature", "Connect with a friend"],
11
  "articles": [
12
  {"title": "Overcoming Sadness", "url": "https://example.com/sadness1"},
 
17
  {"title": "Coping with Grief", "url": "https://www.youtube.com/watch?v=sadnessvideo2"},
18
  ],
19
  },
20
+ "POSITIVE": {
21
  "suggestions": ["Practice gratitude", "Engage in a hobby", "Spend time with loved ones"],
22
  "articles": [
23
  {"title": "The Benefits of Joy", "url": "https://example.com/joy1"},
 
28
  {"title": "Practicing Gratitude", "url": "https://www.youtube.com/watch?v=joyvideo2"},
29
  ],
30
  },
31
+ "NEUTRAL": {
32
  "suggestions": ["Take a break", "Engage in a relaxing activity", "Spend time in nature"],
33
  "articles": [
34
  {"title": "Importance of Self-Care", "url": "https://example.com/selfcare1"},
 
46
  resources = suggestion_database.get(emotion, {})
47
  return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
48
 
49
+ # Function to suggest activities based on the emotion analysis result
50
  def suggest_activity(emotion_analysis):
51
+ max_emotion = max(emotion_analysis, key=emotion_analysis.get) if emotion_analysis else "NEUTRAL"
52
  suggestions, articles, videos = get_relevant_resources(max_emotion)
53
  return {
54
  "suggestions": suggestions,
 
56
  "videos": videos,
57
  }
58
 
59
+ # Streamlit app
60
+ def main():
61
+ st.title("Emotion Detection and Suggestions")
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ st.write("Please answer the following questions:")
 
64
 
65
+ # Step 1: Collect answers to three questions
66
+ question_1 = st.text_input("How are you feeling today? (e.g., happy, sad, stressed)")
67
+ question_2 = st.text_input("What's something that is currently on your mind?")
68
+ question_3 = st.text_input("Do you feel overwhelmed or calm right now?")
69
+
70
+ if question_1 and question_2 and question_3:
71
+ # Step 2: Analyze sentiment of responses
72
+ text_to_analyze = f"{question_1} {question_2} {question_3}"
73
+ analysis_result = emotion_analyzer(text_to_analyze)
74
+ emotion = analysis_result[0]['label'] # Get the emotion from the analysis result
75
+
76
+ # Map emotion label from the model to our suggestion database
77
+ if emotion == "LABEL_0":
78
+