|
|
|
import nltk |
|
from nltk.corpus import stopwords |
|
from nltk.tokenize import word_tokenize |
|
from nltk.tag import pos_tag |
|
from transformers import pipeline |
|
import gradio as gr |
|
|
|
|
|
nltk.download('punkt') |
|
nltk.download('averaged_perceptron_tagger') |
|
nltk.download('stopwords') |
|
|
|
|
|
sentiment_analyzer = pipeline('sentiment-analysis') |
|
|
|
|
|
def extract_keywords(text): |
|
stop_words = set(stopwords.words('english')) |
|
words = word_tokenize(text) |
|
words_filtered = [word for word in words if word.isalnum() and word.lower() not in stop_words] |
|
|
|
|
|
tagged = pos_tag(words_filtered) |
|
|
|
|
|
keywords = [word for word, tag in tagged if tag.startswith('NN') or tag.startswith('VB')] |
|
return keywords |
|
|
|
|
|
def analyze_journal(text): |
|
keywords = extract_keywords(text) |
|
sentiment_result = sentiment_analyzer(text)[0] |
|
mood_label = sentiment_result['label'] |
|
|
|
|
|
suggestions = [] |
|
|
|
if mood_label == "POSITIVE": |
|
suggestions.append("It seems you're feeling good! Keep up the positive activities.") |
|
elif mood_label == "NEGATIVE": |
|
suggestions.append("It looks like you're feeling down. Consider trying mindfulness exercises or talking to a friend.") |
|
else: |
|
suggestions.append("You're feeling neutral. It's a good time to reflect and engage in self-care.") |
|
|
|
|
|
if 'work' in keywords or 'job' in keywords: |
|
suggestions.append("You mentioned work. Remember to balance tasks with self-care to avoid burnout.") |
|
|
|
if 'stress' in keywords or 'anxious' in keywords: |
|
suggestions.append("It seems like you're feeling stressed. Deep breathing exercises or a short walk might help.") |
|
|
|
if 'happy' in keywords or 'joy' in keywords: |
|
suggestions.append("You're in a good mood! Keep doing activities that bring you joy.") |
|
|
|
if 'tired' in keywords or 'sleep' in keywords: |
|
suggestions.append("You're feeling tired. Getting enough rest is important for mental well-being.") |
|
|
|
return f"Keywords: {', '.join(keywords)}\nMood: {mood_label}\n\nSuggestions:\n- " + "\n- ".join(suggestions) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_journal, |
|
inputs=gr.components.Textbox(lines=5, label="Write your journal entry here"), |
|
outputs="text", |
|
title="Mental Health Mood Analyzer", |
|
description="Write about your day, and the analyzer will suggest improvements based on your mood and keywords." |
|
) |
|
|
|
|
|
iface.launch() |
|
|