Spaces:
Sleeping
Sleeping
razanalsulami
commited on
Commit
•
3c3ff47
1
Parent(s):
784b651
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|