Spaces:
Sleeping
Sleeping
razanalsulami
commited on
Commit
β’
65f5aa2
1
Parent(s):
8772735
Update app.py
Browse files
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 |
-
#
|
10 |
-
|
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 |
-
#
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
mood_label = sentiment_result['label']
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
else:
|
44 |
-
|
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 |
-
|
54 |
-
|
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 |
-
|
62 |
-
|
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 |
-
|
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()
|
|