sunbal7 commited on
Commit
7077f97
Β·
verified Β·
1 Parent(s): e7b8cf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -72
app.py CHANGED
@@ -1,83 +1,135 @@
 
1
  import streamlit as st
 
2
  from textblob import TextBlob
3
- # The transformers import is here in case you want to integrate a pre-trained model later.
4
- # from transformers import pipeline
5
 
6
- def get_roast(user_text):
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
- def analyze_text(user_text):
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
- def calculate_resilience_score(sentiment, distortions):
29
- """
30
- Calculates a resilience score based on sentiment and the number of detected distortions.
31
- The score is capped between 0 and 100.
32
- """
33
- score = 100
34
- # Adjust score by sentiment (scaled)
35
- score += int(sentiment * 20)
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
- def get_reframe_tips(score):
43
- """
44
- Provides reframe tips based on the resilience score.
45
- """
46
- if score < 50:
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
- def main():
54
- st.title("πŸ€– Roast Master Therapist Bot")
55
- st.write("Share your problem, and let the roast and resilience tips begin!")
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- # User input
58
- user_input = st.text_area("What's troubling you?", placeholder="e.g., I procrastinate on everything...")
 
59
 
60
- if st.button("Get Roast and Tips"):
61
- if user_input.strip():
62
- # Generate roast response
63
- roast = get_roast(user_input)
64
- st.markdown("### Roast:")
65
- st.write(roast)
66
-
67
- # Analyze user input
68
- sentiment, distortions = analyze_text(user_input)
69
- resilience_score = calculate_resilience_score(sentiment, distortions)
70
- tips = get_reframe_tips(resilience_score)
71
-
72
- # Display analysis and tips
73
- st.markdown("### Resilience Analysis:")
74
- st.write(f"**Resilience Score:** {resilience_score}/100")
75
- st.write(tips)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- # Fun Streamlit animation!
78
- st.balloons()
79
- else:
80
- st.warning("Please share something so we can get roasting!")
81
 
82
- if __name__ == '__main__':
83
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)