Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
7 |
-
from datetime import datetime
|
8 |
from reportlab.lib.pagesizes import letter
|
9 |
from reportlab.pdfgen import canvas
|
10 |
import io
|
|
|
|
|
11 |
|
12 |
-
# Initialize
|
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 |
-
|
22 |
"KevSun/Personality_LM",
|
23 |
ignore_mismatched_sizes=True
|
24 |
)
|
25 |
-
|
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="π§
|
32 |
|
33 |
# Custom CSS
|
34 |
st.markdown("""
|
35 |
<style>
|
36 |
-
@keyframes
|
37 |
-
0% {
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
40 |
}
|
41 |
.personality-title {
|
42 |
-
animation:
|
|
|
43 |
}
|
44 |
.social-post {
|
45 |
-
border:
|
46 |
border-radius: 15px;
|
47 |
padding: 20px;
|
48 |
margin: 15px 0;
|
49 |
-
background: #
|
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 |
-
#
|
|
|
|
|
|
|
71 |
QUESTION_BANK = [
|
72 |
-
{"text": "If you were a kitchen appliance,
|
73 |
-
{"text": "
|
74 |
-
{"text": "
|
75 |
-
{"text": "
|
76 |
-
{"text": "
|
77 |
-
{"text": "
|
78 |
-
{"text": "What
|
79 |
-
{"text": "
|
|
|
|
|
80 |
]
|
81 |
|
82 |
-
def
|
83 |
-
"""
|
84 |
-
|
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 =
|
100 |
-
|
101 |
-
|
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 |
-
|
110 |
-
|
111 |
-
|
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 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
# Main UI
|
156 |
-
st.title("π§
|
157 |
-
st.markdown("### Your
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
-
#
|
160 |
-
if st.session_state.current_q <
|
161 |
-
q = st.session_state.
|
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
|
179 |
-
|
180 |
-
|
181 |
-
st.session_state.personality = analyze_personality(combined_text)
|
182 |
-
|
183 |
-
traits = st.session_state.personality
|
184 |
-
st.balloons()
|
185 |
|
186 |
-
|
187 |
-
|
|
|
|
|
188 |
|
189 |
-
|
190 |
-
|
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 |
-
#
|
196 |
-
st.
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
horizontal=True)
|
203 |
|
204 |
-
if st.
|
205 |
-
post = generate_social_post(selected, traits)
|
206 |
st.markdown(f"""
|
207 |
<div class="social-post">
|
208 |
-
|
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 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
222 |
|
223 |
# Sidebar
|
224 |
with st.sidebar:
|
225 |
-
st.markdown("## π Features")
|
226 |
st.markdown("""
|
227 |
-
-
|
228 |
-
-
|
229 |
-
-
|
230 |
-
-
|
231 |
-
|
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 |
+
""")
|