Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,32 +5,72 @@ st.set_page_config(page_title="Mental Wellness Diary Analyzer", page_icon="🧠"
|
|
| 5 |
st.title("🧠 Mental Wellness Diary Analyzer")
|
| 6 |
|
| 7 |
# Load a lightweight language model
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def analyze_journal(entry):
|
| 11 |
prompt = f"""
|
| 12 |
Analyze the following journal entry:
|
| 13 |
"{entry}"
|
| 14 |
-
|
| 15 |
Determine the following:
|
| 16 |
-
1. Emotional tone
|
| 17 |
-
2.
|
| 18 |
-
3.
|
| 19 |
-
|
| 20 |
-
Be friendly and encouraging in your tone.
|
| 21 |
"""
|
| 22 |
output = generator(prompt)[0]["generated_text"]
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# User
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if journal_entry.strip():
|
| 30 |
-
st.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
else:
|
| 35 |
-
st.warning("Please
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
st.title("🧠 Mental Wellness Diary Analyzer")
|
| 6 |
|
| 7 |
# Load a lightweight language model
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
return pipeline("text-generation", model="distilgpt2", max_new_tokens=200)
|
| 11 |
+
|
| 12 |
+
generator = load_model()
|
| 13 |
|
| 14 |
def analyze_journal(entry):
|
| 15 |
prompt = f"""
|
| 16 |
Analyze the following journal entry:
|
| 17 |
"{entry}"
|
|
|
|
| 18 |
Determine the following:
|
| 19 |
+
1. Emotional tone: [Identify the primary emotions]
|
| 20 |
+
2. Recurring themes: [Identify key themes]
|
| 21 |
+
3. Advice: [Provide encouraging advice]
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
output = generator(prompt)[0]["generated_text"]
|
| 24 |
+
|
| 25 |
+
# Extract the analysis part
|
| 26 |
+
analysis = output.split("Analyze the following journal entry:")[-1].strip()
|
| 27 |
+
|
| 28 |
+
# Format the output in a cleaner way without JSON
|
| 29 |
+
formatted_analysis = ""
|
| 30 |
+
if "Emotional tone:" in analysis:
|
| 31 |
+
emotional_tone = analysis.split("Emotional tone:")[1].split("Recurring themes:")[0].strip()
|
| 32 |
+
formatted_analysis += f"😊 **Emotional tone:** {emotional_tone}\n\n"
|
| 33 |
+
|
| 34 |
+
if "Recurring themes:" in analysis:
|
| 35 |
+
themes = analysis.split("Recurring themes:")[1].split("Advice:")[0].strip()
|
| 36 |
+
formatted_analysis += f"🔍 **Recurring themes:** {themes}\n\n"
|
| 37 |
+
|
| 38 |
+
if "Advice:" in analysis:
|
| 39 |
+
advice = analysis.split("Advice:")[1].strip()
|
| 40 |
+
formatted_analysis += f"💡 **Advice:** {advice}"
|
| 41 |
+
|
| 42 |
+
return formatted_analysis if formatted_analysis else analysis
|
| 43 |
|
| 44 |
+
# User interface
|
| 45 |
+
st.markdown("### Share what's on your mind today")
|
| 46 |
+
journal_entry = st.text_area("✍️ Write your journal entry here:", height=150)
|
| 47 |
|
| 48 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 49 |
+
with col2:
|
| 50 |
+
analyze_button = st.button("🧠 Analyze My Entry", use_container_width=True)
|
| 51 |
+
|
| 52 |
+
if analyze_button:
|
| 53 |
if journal_entry.strip():
|
| 54 |
+
with st.spinner("Analyzing your thoughts..."):
|
| 55 |
+
result = analyze_journal(journal_entry)
|
| 56 |
+
|
| 57 |
+
st.markdown("### 💭 Your Wellness Insights")
|
| 58 |
+
st.markdown(result)
|
| 59 |
+
|
| 60 |
+
# Add some supportive message
|
| 61 |
+
st.markdown("---")
|
| 62 |
+
st.markdown("💪 **Remember**: Writing about your feelings is already a positive step for your mental wellness!")
|
| 63 |
else:
|
| 64 |
+
st.warning("Please share your thoughts in the journal entry box first.")
|
| 65 |
+
|
| 66 |
+
# Add a sample entry option
|
| 67 |
+
with st.expander("Need inspiration? Try a sample entry"):
|
| 68 |
+
if st.button("Use sample entry"):
|
| 69 |
+
sample_text = "I feel really stressed and anxious about my work. I haven't been sleeping well, and I'm worried about meeting deadlines."
|
| 70 |
+
st.session_state["sample_text"] = sample_text
|
| 71 |
+
st.experimental_rerun()
|
| 72 |
|
| 73 |
+
# Use sample if available in session state
|
| 74 |
+
if "sample_text" in st.session_state:
|
| 75 |
+
journal_entry = st.session_state["sample_text"]
|
| 76 |
+
del st.session_state["sample_text"]
|