Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,135 @@
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from textblob import TextBlob
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
Generates a roast based on the user's input.
|
9 |
-
(You can replace or extend this logic by integrating a model like Mixtral-8x7B.)
|
10 |
-
"""
|
11 |
-
if "procrastinate" in user_text.lower():
|
12 |
-
return "Ah, the art of doing nothing! Do you charge Netflix for your couch imprint? ποΈ"
|
13 |
-
elif "always" in user_text.lower() or "never" in user_text.lower():
|
14 |
-
return "Wow, painting your world in extremes? Maybe it's time to add some shades of gray!"
|
15 |
-
else:
|
16 |
-
return "Is that a problem or a lifestyle choice? Time to get serious... or maybe not."
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
Analyzes the input text for sentiment and detects basic cognitive distortions.
|
21 |
-
For now, it counts occurrences of words like 'always' or 'never'.
|
22 |
-
"""
|
23 |
-
blob = TextBlob(user_text)
|
24 |
-
sentiment = blob.sentiment.polarity # Ranges from -1 (negative) to 1 (positive)
|
25 |
-
distortions = sum(word in user_text.lower() for word in ["always", "never"])
|
26 |
-
return sentiment, distortions
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
# Penalize for cognitive distortions
|
37 |
-
score -= distortions * 10
|
38 |
-
# Ensure score stays within bounds
|
39 |
-
score = max(0, min(score, 100))
|
40 |
-
return score
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
return "Remember: small steps lead to big changes. Try breaking tasks into manageable chunks and celebrate every little victory!"
|
48 |
-
elif score < 75:
|
49 |
-
return "You're on your way! Consider setting specific goals and challenge those negative thoughts with evidence."
|
50 |
-
else:
|
51 |
-
return "Keep up the great work! Your resilience is inspiring β maybe share some of that energy with someone who needs it!"
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
#
|
58 |
-
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
st.warning("Please share something so we can get roasting!")
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
import streamlit as st
|
3 |
+
from groq import Groq
|
4 |
from textblob import TextBlob
|
5 |
+
import re
|
6 |
+
import time
|
7 |
|
8 |
+
# Initialize Groq client
|
9 |
+
client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Configure Streamlit
|
12 |
+
st.set_page_config(page_title="π€£ Roast Master Therapist", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Custom CSS for animations
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
@keyframes laugh {
|
18 |
+
0% { transform: scale(1); }
|
19 |
+
50% { transform: scale(1.2); }
|
20 |
+
100% { transform: scale(1); }
|
21 |
+
}
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
.laugh-emoji {
|
24 |
+
animation: laugh 0.5s ease-in-out infinite;
|
25 |
+
font-size: 3em;
|
26 |
+
text-align: center;
|
27 |
+
}
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
.progress-bar {
|
30 |
+
height: 20px;
|
31 |
+
border-radius: 10px;
|
32 |
+
background: #ff4b4b;
|
33 |
+
transition: width 0.5s ease-in-out;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
""", unsafe_allow_html=True)
|
37 |
+
|
38 |
+
def analyze_cbt(text):
|
39 |
+
"""Analyze text for cognitive distortions using CBT principles"""
|
40 |
+
analysis = {
|
41 |
+
'absolutes': len(re.findall(r'\b(always|never|everyone|nobody)\b', text, re.I)),
|
42 |
+
'negative_words': TextBlob(text).sentiment.polarity,
|
43 |
+
'resilience_score': 100 # Start with perfect score
|
44 |
+
}
|
45 |
|
46 |
+
# Deduct points for cognitive distortions
|
47 |
+
analysis['resilience_score'] -= analysis['absolutes'] * 5
|
48 |
+
analysis['resilience_score'] = max(analysis['resilience_score'], 0)
|
49 |
|
50 |
+
return analysis
|
51 |
+
|
52 |
+
def generate_roast(prompt):
|
53 |
+
"""Generate a humorous roast using Groq/Mixtral"""
|
54 |
+
system_prompt = """You are a sarcastic therapist who roasts bad habits in a funny way.
|
55 |
+
Respond with 1-2 short sentences including emojis. Example: "Ah, the art of doing nothing!
|
56 |
+
Do you charge Netflix for your couch imprint? ποΈ" """
|
57 |
+
|
58 |
+
response = client.chat.completions.create(
|
59 |
+
model="mixtral-8x7b-32768",
|
60 |
+
messages=[
|
61 |
+
{"role": "system", "content": system_prompt},
|
62 |
+
{"role": "user", "content": prompt}
|
63 |
+
],
|
64 |
+
temperature=0.9,
|
65 |
+
max_tokens=100
|
66 |
+
)
|
67 |
+
return response.choices[0].message.content
|
68 |
+
|
69 |
+
def get_cbt_tips(analysis):
|
70 |
+
"""Generate personalized CBT tips"""
|
71 |
+
tips = []
|
72 |
+
if analysis['absolutes'] > 0:
|
73 |
+
tips.append("π Notice absolute words like 'always/never' - reality is usually more nuanced!")
|
74 |
+
if analysis['negative_words'] < -0.2:
|
75 |
+
tips.append("π Try reframing negative statements: 'I sometimes...' instead of 'I never...'")
|
76 |
+
if analysis['resilience_score'] < 70:
|
77 |
+
tips.append("πͺ Practice thought challenging: 'Is this truly catastrophic or just inconvenient?'")
|
78 |
+
return tips
|
79 |
+
|
80 |
+
# Main app interface
|
81 |
+
st.title("π€£ Roast Master Therapist")
|
82 |
+
st.markdown("### Your hilarious path to emotional resilience!")
|
83 |
+
|
84 |
+
user_input = st.text_input("Share your problem or bad habit:",
|
85 |
+
placeholder="e.g., 'I always procrastinate...'")
|
86 |
+
|
87 |
+
if user_input:
|
88 |
+
with st.spinner("Consulting the comedy gods..."):
|
89 |
+
# Show laughing animation
|
90 |
+
st.markdown('<div class="laugh-emoji">π</div>', unsafe_allow_html=True)
|
91 |
+
time.sleep(1.5)
|
92 |
+
|
93 |
+
# Generate and display roast
|
94 |
+
roast = generate_roast(user_input)
|
95 |
+
st.subheader("π₯ Your Roast:")
|
96 |
+
st.write(roast)
|
97 |
+
|
98 |
+
# Analyze and show results
|
99 |
+
analysis = analyze_cbt(user_input)
|
100 |
+
st.divider()
|
101 |
+
|
102 |
+
# Resilience score visualization
|
103 |
+
st.subheader("π§ Resilience Analysis")
|
104 |
+
score = analysis['resilience_score']
|
105 |
+
st.markdown(f"""
|
106 |
+
<div style="background: #f0f2f6; border-radius: 10px; padding: 10px;">
|
107 |
+
<div class="progress-bar" style="width: {score}%;"></div>
|
108 |
+
</div>
|
109 |
+
<br>Current Resilience Score: {score}/100
|
110 |
+
""", unsafe_allow_html=True)
|
111 |
+
|
112 |
+
# Display CBT tips
|
113 |
+
st.subheader("π‘ Growth Tips")
|
114 |
+
for tip in get_cbt_tips(analysis):
|
115 |
+
st.markdown(f"- {tip}")
|
116 |
|
117 |
+
# Add disclaimer
|
118 |
+
st.divider()
|
119 |
+
st.caption("Disclaimer: This is not real therapy - it's therapy with jokes! Always consult a professional for serious concerns.")
|
|
|
120 |
|
121 |
+
# Sidebar information
|
122 |
+
with st.sidebar:
|
123 |
+
st.header("How It Works")
|
124 |
+
st.markdown("""
|
125 |
+
1. Share your bad habit/problem
|
126 |
+
2. Get hilariously roasted π€£
|
127 |
+
3. Receive psychological insights
|
128 |
+
4. Get personalized growth tips
|
129 |
+
|
130 |
+
**Psychological Basis:**
|
131 |
+
- Cognitive Behavioral Therapy (CBT)
|
132 |
+
- Humor as emotional distancing
|
133 |
+
- Positive reframing techniques
|
134 |
+
""")
|
135 |
+
st.image("https://i.imgur.com/7Q4X4yN.png", width=200)
|