razanalsulami commited on
Commit
65f5aa2
β€’
1 Parent(s): 8772735

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -63
app.py CHANGED
@@ -1,71 +1,29 @@
1
- # Import required libraries
2
- import nltk
3
- from nltk.corpus import stopwords
4
- from nltk.tokenize import word_tokenize
5
- from nltk.tag import pos_tag
6
- from transformers import pipeline
7
  import gradio as gr
 
8
 
9
- # Download NLTK data
10
- nltk.download('punkt')
11
- nltk.download('averaged_perceptron_tagger')
12
- nltk.download('stopwords')
13
-
14
- # Load Hugging Face's sentiment analysis pipeline
15
- sentiment_analyzer = pipeline('sentiment-analysis')
16
-
17
- # Function to extract keywords (nouns and verbs)
18
- def extract_keywords(text):
19
- stop_words = set(stopwords.words('english'))
20
- words = word_tokenize(text)
21
- words_filtered = [word for word in words if word.isalnum() and word.lower() not in stop_words]
22
-
23
- # Part-of-speech tagging
24
- tagged = pos_tag(words_filtered)
25
-
26
- # Keep only nouns and verbs
27
- keywords = [word for word, tag in tagged if tag.startswith('NN') or tag.startswith('VB')]
28
- return keywords
29
 
30
- # Analyze mood and provide suggestions based on keywords
31
- def analyze_journal(text):
32
- keywords = extract_keywords(text)
33
- sentiment_result = sentiment_analyzer(text)[0]
34
- mood_label = sentiment_result['label']
35
 
36
- # Generate suggestions based on keywords and mood
37
- suggestions = []
38
-
39
- if mood_label == "POSITIVE":
40
- suggestions.append("It seems you're feeling good! Keep up the positive activities.")
41
- elif mood_label == "NEGATIVE":
42
- suggestions.append("It looks like you're feeling down. Consider trying mindfulness exercises or talking to a friend.")
43
  else:
44
- suggestions.append("You're feeling neutral. It's a good time to reflect and engage in self-care.")
45
-
46
- # Personalized suggestions based on keywords
47
- if 'work' in keywords or 'job' in keywords:
48
- suggestions.append("You mentioned work. Remember to balance tasks with self-care to avoid burnout.")
49
-
50
- if 'stress' in keywords or 'anxious' in keywords:
51
- suggestions.append("It seems like you're feeling stressed. Deep breathing exercises or a short walk might help.")
52
 
53
- if 'happy' in keywords or 'joy' in keywords:
54
- suggestions.append("You're in a good mood! Keep doing activities that bring you joy.")
55
-
56
- if 'tired' in keywords or 'sleep' in keywords:
57
- suggestions.append("You're feeling tired. Getting enough rest is important for mental well-being.")
58
-
59
- return f"Keywords: {', '.join(keywords)}\nMood: {mood_label}\n\nSuggestions:\n- " + "\n- ".join(suggestions)
60
 
61
- # Gradio interface for the journal analyzer
62
- iface = gr.Interface(
63
- fn=analyze_journal, # Function to call for analyzing the journal
64
- inputs=gr.components.Textbox(lines=5, label="Write your journal entry here"), # Input for journal text
65
- outputs="text", # Output as text (keywords, mood, and suggestions)
66
- title="Mental Health Mood Analyzer",
67
- description="Write about your day, and the analyzer will suggest improvements based on your mood and keywords."
68
- )
69
 
70
- # Launch the Gradio interface
71
- iface.launch()
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # pipeline
5
+ sentiment_analysis = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # this takes user input and analyzes mood
8
+ def analyze_mood(user_input):
9
+ # Analyze input text
10
+ result = sentiment_analysis(user_input)[0]
 
11
 
12
+ # Set the mood
13
+ if result["label"] == "POSITIVE":
14
+ mood = "Happy"
15
+ suggestion = "Keep doing what you're doing! 😊"
16
+ elif result["label"] == "NEGATIVE":
17
+ mood = "Sad"
18
+ suggestion = "Try to talk to someone, or take a break πŸ’‘"
19
  else:
20
+ mood = "Neutral"
21
+ suggestion = "You're doing okay! Stay calm 🌸"
 
 
 
 
 
 
22
 
23
+ # Return the mood and the suggestion for the user
24
+ return "Your mood is: " + mood, suggestion
 
 
 
 
 
25
 
26
+ inputs = gr.Textbox(label="How are you feeling today?", placeholder="Type your thoughts here...")
27
+ outputs = gr.Textbox(label="Mood and Suggestion")
 
 
 
 
 
 
28
 
29
+ gr.Interface(fn=analyze_mood, inputs=inputs, outputs=outputs, title="Mood Analyzer").launch()