sunbal7 commited on
Commit
fe3a4fa
Β·
verified Β·
1 Parent(s): 1e1fdd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -156
app.py CHANGED
@@ -1,141 +1,90 @@
1
  # app.py
2
  import streamlit as st
3
  from groq import Groq
4
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
5
  import torch
6
- import random
7
- from datetime import datetime
8
  from reportlab.lib.pagesizes import letter
9
  from reportlab.pdfgen import canvas
10
  import io
 
 
11
 
12
- # Initialize components
13
  try:
14
  groq_client = Groq(api_key=st.secrets["GROQ_API_KEY"])
15
  except KeyError:
16
- st.error("GROQ_API_KEY missing in secrets!")
17
  st.stop()
18
 
19
  # Load personality model
20
  try:
21
- personality_model = AutoModelForSequenceClassification.from_pretrained(
22
  "KevSun/Personality_LM",
23
  ignore_mismatched_sizes=True
24
  )
25
- personality_tokenizer = AutoTokenizer.from_pretrained("KevSun/Personality_LM")
26
  except Exception as e:
27
  st.error(f"Model loading error: {str(e)}")
28
  st.stop()
29
 
30
  # Configure Streamlit
31
- st.set_page_config(page_title="🧠 PsychBot Pro", layout="wide", page_icon="πŸ€–")
32
 
33
  # Custom CSS
34
  st.markdown("""
35
  <style>
36
- @keyframes float {
37
- 0% { transform: translateY(0px); }
38
- 50% { transform: translateY(-20px); }
39
- 100% { transform: translateY(0px); }
 
 
 
40
  }
41
  .personality-title {
42
- animation: float 3s ease-in-out infinite;
 
43
  }
44
  .social-post {
45
- border: 1px solid #e0e0e0;
46
  border-radius: 15px;
47
  padding: 20px;
48
  margin: 15px 0;
49
- background: #f8f9fa;
50
- }
51
- .platform-selector {
52
- display: flex;
53
- gap: 10px;
54
- margin: 20px 0;
55
- }
56
- .response-box {
57
- border-left: 3px solid #4CAF50;
58
- padding: 10px;
59
- margin: 10px 0;
60
- }
61
- .question-card {
62
- padding: 20px;
63
- border-radius: 15px;
64
- margin: 10px 0;
65
- background: #f8f9fa;
66
  }
67
  </style>
68
  """, unsafe_allow_html=True)
69
 
70
- # Enhanced question bank with mix of funny/serious questions
 
 
 
71
  QUESTION_BANK = [
72
- {"text": "If you were a kitchen appliance, what would you be? πŸ”ͺ", "type": "funny", "trait": "openness"},
73
- {"text": "How do you handle unexpected changes? πŸŒ€", "type": "serious", "trait": "neuroticism"},
74
- {"text": "What's your spirit animal and why? 🦁", "type": "funny", "trait": "agreeableness"},
75
- {"text": "Describe your ideal work environment πŸ–₯️", "type": "serious", "trait": "conscientiousness"},
76
- {"text": "If your life was a movie title, what would it be? 🎬", "type": "funny", "trait": "openness"},
77
- {"text": "How do you approach conflict resolution? ✌️", "type": "serious", "trait": "agreeableness"},
78
- {"text": "What superhero power matches your personality? 🦸", "type": "funny", "trait": "extraversion"},
79
- {"text": "How do you prioritize tasks? πŸ“…", "type": "serious", "trait": "conscientiousness"}
 
 
80
  ]
81
 
82
- def initialize_session():
83
- """Initialize all session state variables"""
84
- defaults = {
85
- 'questions': random.sample(QUESTION_BANK, 5),
86
- 'current_q': 0,
87
- 'show_post': False,
88
- 'responses': [],
89
- 'personality': None
90
- }
91
- for key, value in defaults.items():
92
- if key not in st.session_state:
93
- st.session_state[key] = value
94
-
95
- def analyze_personality(text):
96
- """Predict personality traits using LM"""
97
- inputs = personality_tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
98
  with torch.no_grad():
99
- outputs = personality_model(**inputs)
100
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
101
- traits = ["openness", "conscientiousness", "extraversion", "agreeableness", "neuroticism"]
102
- return {trait: float(prob) for trait, prob in zip(traits, probs[0])}
103
-
104
-
105
-
106
 
107
  def generate_social_post(platform, traits):
108
- """Generate platform-specific social post"""
109
- platform_prompts = {
110
- "instagram": {
111
- "emoji": "πŸ“Έ",
112
- "prompt": "Create an Instagram post with 3 emojis and 2 hashtags about personal growth:"
113
- },
114
- "linkedin": {
115
- "emoji": "πŸ’Ό",
116
- "prompt": "Create a professional LinkedIn post about self-improvement:"
117
- },
118
- "facebook": {
119
- "emoji": "πŸ‘₯",
120
- "prompt": "Create a friendly Facebook post with 2 emojis:"
121
- },
122
- "whatsapp": {
123
- "emoji": "πŸ’¬",
124
- "prompt": "Create a casual WhatsApp status with 2 emojis:"
125
- }
126
- }
127
-
128
- # Define the style for each platform separately
129
- styles = {
130
- 'instagram': 'visually appealing with hashtags',
131
- 'linkedin': 'professional and inspiring',
132
- 'facebook': 'friendly and engaging',
133
- 'whatsapp': 'casual and fun'
134
- }
135
-
136
- prompt = f"""{platform_prompts[platform]['prompt']}
137
- Based on these personality traits: {traits}
138
- Include {platform_prompts[platform]['emoji']} emoji and make it {styles[platform]}"""
139
 
140
  response = groq_client.chat.completions.create(
141
  model="mixtral-8x7b-32768",
@@ -144,30 +93,60 @@ Include {platform_prompts[platform]['emoji']} emoji and make it {styles[platform
144
  )
145
  return response.choices[0].message.content
146
 
147
-
148
-
149
-
150
-
151
-
152
- # Initialize session state
153
- initialize_session()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  # Main UI
156
- st.title("🧠 PsychBot Pro")
157
- st.markdown("### Your AI Personality Companion πŸ€–πŸ’¬")
 
 
 
 
 
 
 
 
158
 
159
- # Dynamic question flow
160
- if st.session_state.current_q < len(st.session_state.questions):
161
- q = st.session_state.questions[st.session_state.current_q]
162
 
163
  with st.chat_message("assistant"):
164
- st.markdown(f"""
165
- <div class="question-card">
166
- <h4>{q['text']}</h4>
167
- <p>{'🌟 Fun question!' if q['type'] == 'funny' else 'πŸ€” Serious reflection'}</p>
168
- </div>
169
- """, unsafe_allow_html=True)
170
-
171
  user_input = st.text_input("Your response:", key=f"q{st.session_state.current_q}")
172
 
173
  if st.button("Next ➑️"):
@@ -175,59 +154,60 @@ if st.session_state.current_q < len(st.session_state.questions):
175
  st.session_state.current_q += 1
176
  st.rerun()
177
  else:
178
- # Generate personality report
179
- if not st.session_state.personality:
180
- combined_text = " ".join(st.session_state.responses)
181
- st.session_state.personality = analyze_personality(combined_text)
182
-
183
- traits = st.session_state.personality
184
- st.balloons()
185
 
186
- # Personality report section
187
- st.markdown(f"## <div class='personality-title'>🌟 Your Personality Report</div>", unsafe_allow_html=True)
 
 
188
 
189
- # Personality visualization
190
- cols = st.columns(5)
191
- sorted_traits = sorted(traits.items(), key=lambda x: x[1], reverse=True)
192
- for i, (trait, score) in enumerate(sorted_traits):
193
- cols[i].metric(label=trait.upper(), value=f"{score*100:.0f}%")
194
 
195
- # Social post generation
196
- st.markdown("---")
197
- st.markdown("### πŸ“± Social Media Post Generator")
 
 
 
 
 
 
198
 
199
- platforms = ["instagram", "linkedin", "facebook", "whatsapp"]
200
- selected = st.radio("Choose platform:", platforms,
201
- format_func=lambda x: f"{x.capitalize()} {'πŸ“ΈπŸ’ΌπŸ‘₯πŸ’¬'[platforms.index(x)]}",
202
- horizontal=True)
203
 
204
- if st.button("Generate Post ✨"):
205
- post = generate_social_post(selected, traits)
206
  st.markdown(f"""
207
  <div class="social-post">
208
- <h4>🎨 {selected.capitalize()} Post Draft</h4>
209
- <div class="response-box">{post}</div>
210
- <button onclick="navigator.clipboard.writeText(`{post}`)"
211
- style="margin-top:10px; background: #4CAF50; color: white; border: none; padding: 8px 15px; border-radius: 5px;">
212
- πŸ“‹ Copy Text
213
- </button>
214
  </div>
215
  """, unsafe_allow_html=True)
216
-
217
- # Restart conversation
218
- if st.button("πŸ”„ Start New Analysis"):
219
- for key in list(st.session_state.keys()):
220
- del st.session_state[key]
221
- st.rerun()
 
 
 
 
222
 
223
  # Sidebar
224
  with st.sidebar:
225
- st.markdown("## 🌈 Features")
226
  st.markdown("""
227
- - 🎭 Dynamic personality assessment
228
- - πŸ€– AI-generated social posts
229
- - πŸ“Š Visual trait analysis
230
- - πŸ’¬ Mix of fun/serious questions
231
- - πŸ“₯ PDF report download
232
- """)
233
-
 
 
 
 
1
  # app.py
2
  import streamlit as st
3
  from groq import Groq
 
4
  import torch
5
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
 
6
  from reportlab.lib.pagesizes import letter
7
  from reportlab.pdfgen import canvas
8
  import io
9
+ import random
10
+ from datetime import datetime
11
 
12
+ # Initialize Groq client
13
  try:
14
  groq_client = Groq(api_key=st.secrets["GROQ_API_KEY"])
15
  except KeyError:
16
+ st.error("GROQ_API_KEY missing in secrets! Add it in Hugging Face settings.")
17
  st.stop()
18
 
19
  # Load personality model
20
  try:
21
+ model = AutoModelForSequenceClassification.from_pretrained(
22
  "KevSun/Personality_LM",
23
  ignore_mismatched_sizes=True
24
  )
25
+ tokenizer = AutoTokenizer.from_pretrained("KevSun/Personality_LM")
26
  except Exception as e:
27
  st.error(f"Model loading error: {str(e)}")
28
  st.stop()
29
 
30
  # Configure Streamlit
31
+ st.set_page_config(page_title="🧠 Mind Mapper Pro", layout="wide", page_icon="πŸ€–")
32
 
33
  # Custom CSS
34
  st.markdown("""
35
  <style>
36
+ @keyframes rainbow {
37
+ 0% { color: #ff0000; }
38
+ 20% { color: #ff8000; }
39
+ 40% { color: #ffff00; }
40
+ 60% { color: #00ff00; }
41
+ 80% { color: #0000ff; }
42
+ 100% { color: #ff00ff; }
43
  }
44
  .personality-title {
45
+ animation: rainbow 3s infinite;
46
+ font-size: 2.5em !important;
47
  }
48
  .social-post {
49
+ border: 2px solid #4CAF50;
50
  border-radius: 15px;
51
  padding: 20px;
52
  margin: 15px 0;
53
+ background: #f8fff9;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  }
55
  </style>
56
  """, unsafe_allow_html=True)
57
 
58
+ # Personality traits
59
+ OCEAN_TRAITS = ["agreeableness", "openness", "conscientiousness", "extraversion", "neuroticism"]
60
+
61
+ # Enhanced question bank with mix of serious and funny questions
62
  QUESTION_BANK = [
63
+ {"text": "If you were a kitchen appliance, which would you be and why? πŸ₯„", "trait": "openness"},
64
+ {"text": "Describe your ideal Sunday vs your actual last Sunday πŸ“†", "trait": "conscientiousness"},
65
+ {"text": "How would you survive a zombie apocalypse? 🧟", "trait": "neuroticism"},
66
+ {"text": "What's your spirit animal during tax season? 🦁", "trait": "agreeableness"},
67
+ {"text": "Plan a perfect date... with yourself! πŸ’ƒ", "trait": "extraversion"},
68
+ {"text": "If emotions were weather patterns, what's your forecast? β›…", "trait": "neuroticism"},
69
+ {"text": "What would your childhood toy say about you? 🧸", "trait": "openness"},
70
+ {"text": "Describe your email inbox as a movie title πŸ“§", "trait": "conscientiousness"},
71
+ {"text": "How do you react when someone says 'We need to talk'? 😬", "trait": "agreeableness"},
72
+ {"text": "What's your superpower in group projects? 🦸", "trait": "extraversion"}
73
  ]
74
 
75
+ def analyze_psychology(text):
76
+ """Analyze personality traits using the specified model"""
77
+ encoded_input = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=64)
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  with torch.no_grad():
79
+ outputs = model(**encoded_input)
80
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
81
+ return {trait: score.item() for trait, score in zip(OCEAN_TRAITS, predictions[0])}
 
 
 
 
82
 
83
  def generate_social_post(platform, traits):
84
+ """Generate platform-specific social media post"""
85
+ prompt = f"""Create a {platform} post about personal growth based on these personality scores:
86
+ {traits}
87
+ Include relevant emojis and make it {platform}-appropriate. Keep it under 280 characters."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  response = groq_client.chat.completions.create(
90
  model="mixtral-8x7b-32768",
 
93
  )
94
  return response.choices[0].message.content
95
 
96
+ def create_pdf_report(report):
97
+ """Generate downloadable PDF report"""
98
+ buffer = io.BytesIO()
99
+ p = canvas.Canvas(buffer, pagesize=letter)
100
+
101
+ p.setFont("Helvetica-Bold", 16)
102
+ p.drawString(100, 750, "🌟 Psychological Profile Report 🌟")
103
+ p.setFont("Helvetica", 12)
104
+
105
+ y_position = 700
106
+ content = [
107
+ f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M')}",
108
+ "",
109
+ "Personality Traits:",
110
+ *[f"- {trait.upper()}: {score:.2f}" for trait, score in report['traits'].items()],
111
+ "",
112
+ "Personalized Quote:",
113
+ report['quote'],
114
+ ]
115
+
116
+ for line in content:
117
+ p.drawString(100, y_position, line)
118
+ y_position -= 15
119
+
120
+ p.save()
121
+ buffer.seek(0)
122
+ return buffer
123
+
124
+ # Session state management
125
+ if 'responses' not in st.session_state:
126
+ st.session_state.responses = []
127
+ if 'current_q' not in st.session_state:
128
+ st.session_state.current_q = 0
129
+ if 'selected_questions' not in st.session_state:
130
+ st.session_state.selected_questions = random.sample(QUESTION_BANK, 5)
131
 
132
  # Main UI
133
+ st.title("🧠 Mind Mapper Pro")
134
+ st.markdown("### Your Advanced Psychology Assistant πŸ”βœ¨")
135
+
136
+ # Progress tracker
137
+ progress = st.session_state.current_q / 5
138
+ st.markdown(f"""
139
+ <div style="background: #f0f2f6; border-radius: 15px; padding: 5px;">
140
+ <div style="width: {progress*100}%; height: 25px; border-radius: 15px; background: linear-gradient(90deg, #FF6B6B 0%, #4ECDC4 100%);"></div>
141
+ </div>
142
+ """, unsafe_allow_html=True)
143
 
144
+ # Question flow
145
+ if st.session_state.current_q < 5:
146
+ q = st.session_state.selected_questions[st.session_state.current_q]
147
 
148
  with st.chat_message("assistant"):
149
+ st.markdown(f"### {q['text']}")
 
 
 
 
 
 
150
  user_input = st.text_input("Your response:", key=f"q{st.session_state.current_q}")
151
 
152
  if st.button("Next ➑️"):
 
154
  st.session_state.current_q += 1
155
  st.rerun()
156
  else:
157
+ # Generate report
158
+ combined_text = "\n".join(st.session_state.responses)
159
+ traits = analyze_psychology(combined_text)
 
 
 
 
160
 
161
+ report = {
162
+ "traits": traits,
163
+ "quote": generate_social_post("general", traits) # Initial quote
164
+ }
165
 
166
+ st.balloons()
167
+ st.markdown(f"## <div class='personality-title'>🌟 Your Personality Profile 🌟</div>", unsafe_allow_html=True)
 
 
 
168
 
169
+ # Trait Visualization
170
+ with st.expander("🧩 Personality Breakdown"):
171
+ cols = st.columns(5)
172
+ for i, (trait, score) in enumerate(report['traits'].items()):
173
+ cols[i].metric(label=trait.upper(), value=f"{score:.2f}")
174
+
175
+ # Social Media Post Generation
176
+ st.markdown("### πŸ“± Create Social Media Post")
177
+ platform = st.selectbox("Choose platform:", ["LinkedIn", "Instagram", "Facebook", "WhatsApp"])
178
 
179
+ if st.button("Generate Post πŸš€"):
180
+ post = generate_social_post(platform, report['traits'])
181
+ st.session_state.post = post
 
182
 
183
+ if 'post' in st.session_state:
 
184
  st.markdown(f"""
185
  <div class="social-post">
186
+ {st.session_state.post}
 
 
 
 
 
187
  </div>
188
  """, unsafe_allow_html=True)
189
+ st.button("πŸ“‹ Copy Post", on_click=lambda: st.write(st.session_state.post))
190
+
191
+ # PDF Report
192
+ pdf_buffer = create_pdf_report(report)
193
+ st.download_button(
194
+ "πŸ“₯ Download Full Report",
195
+ data=pdf_buffer,
196
+ file_name="personality_profile.pdf",
197
+ mime="application/pdf"
198
+ )
199
 
200
  # Sidebar
201
  with st.sidebar:
202
+ st.markdown("## 🌈 Features Overview")
203
  st.markdown("""
204
+ - **Enhanced Personality Assessment**
205
+ - **Social Media Post Generator**
206
+ - **Interactive Question Flow**
207
+ - **PDF Report Download**
208
+
209
+ **New Features:**
210
+ - 🎭 Fun psychological questions
211
+ - πŸ“± Platform-specific post crafting
212
+ - 🧠 Advanced personality modeling
213
+ """)