Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@
|
|
2 |
import streamlit as st
|
3 |
import torch
|
4 |
import random
|
5 |
-
import altair as alt
|
6 |
import pandas as pd
|
7 |
from datetime import datetime
|
8 |
from reportlab.lib.pagesizes import letter
|
@@ -21,7 +20,7 @@ except Exception as e:
|
|
21 |
st.stop()
|
22 |
|
23 |
# Configure Streamlit
|
24 |
-
st.set_page_config(page_title="
|
25 |
|
26 |
# Custom CSS
|
27 |
st.markdown("""
|
@@ -41,31 +40,22 @@ st.markdown("""
|
|
41 |
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
42 |
}
|
43 |
|
44 |
-
.tip-card {
|
45 |
-
padding: 15px;
|
46 |
-
margin: 10px 0;
|
47 |
-
background: #fff3e0;
|
48 |
-
border-radius: 10px;
|
49 |
-
border: 1px solid #ffab40;
|
50 |
-
}
|
51 |
-
|
52 |
.social-post {
|
53 |
background: #e3f2fd;
|
54 |
padding: 20px;
|
55 |
border-radius: 15px;
|
56 |
margin: 15px 0;
|
57 |
-
|
58 |
-
|
59 |
-
.viz-box {
|
60 |
-
background: white;
|
61 |
-
padding: 20px;
|
62 |
-
border-radius: 15px;
|
63 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
64 |
}
|
65 |
|
66 |
.nav-btn {
|
67 |
margin: 8px 0;
|
68 |
width: 100%;
|
|
|
|
|
|
|
|
|
|
|
69 |
}
|
70 |
</style>
|
71 |
""", unsafe_allow_html=True)
|
@@ -77,12 +67,7 @@ QUESTION_BANK = [
|
|
77 |
{"text": "Describe your ideal alarm clock vs reality β°", "trait": "conscientiousness"},
|
78 |
{"text": "How would you survive a surprise party? π", "trait": "neuroticism"},
|
79 |
{"text": "What's your spirit animal during deadlines? πΎ", "trait": "agreeableness"},
|
80 |
-
{"text": "Plan a perfect day... for your nemesis! π", "trait": "extraversion"}
|
81 |
-
{"text": "If stress was a color, what's your current shade? π¨", "trait": "neuroticism"},
|
82 |
-
{"text": "What would your browser history say about you? π", "trait": "openness"},
|
83 |
-
{"text": "Describe your phone's home screen as a poem π±", "trait": "conscientiousness"},
|
84 |
-
{"text": "React to 'Your idea is interesting, but...' π‘", "trait": "agreeableness"},
|
85 |
-
{"text": "What's your superpower in awkward situations? π¦Έ", "trait": "extraversion"}
|
86 |
]
|
87 |
|
88 |
# Session state management
|
@@ -93,11 +78,11 @@ if 'current_q' not in st.session_state:
|
|
93 |
if 'responses' not in st.session_state:
|
94 |
st.session_state.responses = []
|
95 |
if 'page' not in st.session_state:
|
96 |
-
st.session_state.page = "
|
97 |
|
98 |
-
#
|
99 |
-
def
|
100 |
-
|
101 |
response = groq_client.chat.completions.create(
|
102 |
model="mixtral-8x7b-32768",
|
103 |
messages=[{"role": "user", "content": prompt}],
|
@@ -106,17 +91,19 @@ def generate_quote():
|
|
106 |
return response.choices[0].message.content
|
107 |
|
108 |
def analyze_personality(text):
|
|
|
109 |
encoded_input = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=64)
|
110 |
with torch.no_grad():
|
111 |
outputs = model(**encoded_input)
|
112 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
113 |
return {trait: score.item() for trait, score in zip(OCEAN_TRAITS, predictions[0])}
|
114 |
|
115 |
-
def create_pdf_report(traits
|
|
|
116 |
buffer = io.BytesIO()
|
117 |
p = canvas.Canvas(buffer, pagesize=letter)
|
118 |
p.setFont("Helvetica-Bold", 16)
|
119 |
-
p.drawString(100, 750, "π PersonaCraft
|
120 |
p.setFont("Helvetica", 12)
|
121 |
|
122 |
y_position = 700
|
@@ -124,25 +111,184 @@ def create_pdf_report(traits, quote):
|
|
124 |
p.drawString(100, y_position, f"{trait.upper()}: {score:.2f}")
|
125 |
y_position -= 20
|
126 |
|
127 |
-
p.drawString(100, y_position-40, "Motivational Quote:")
|
128 |
-
p.drawString(100, y_position-60, quote)
|
129 |
p.save()
|
130 |
buffer.seek(0)
|
131 |
return buffer
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
"
|
139 |
-
|
140 |
-
|
141 |
-
prompt = f"""Create a {platform} post about personal growth with these personality traits:
|
142 |
-
{traits}
|
143 |
-
Format: {platform_formats[platform]}
|
144 |
-
Include relevant emojis and keep under 280 characters."""
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
response = groq_client.chat.completions.create(
|
147 |
model="mixtral-8x7b-32768",
|
148 |
messages=[{"role": "user", "content": prompt}],
|
@@ -150,16 +296,41 @@ Include relevant emojis and keep under 280 characters."""
|
|
150 |
)
|
151 |
return response.choices[0].message.content
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
# Main UI
|
154 |
if not st.session_state.started:
|
155 |
st.markdown(f"""
|
156 |
<div class="quote-box">
|
157 |
-
<h2>π Welcome to PersonaCraft! π</h2>
|
158 |
-
<h3>{
|
159 |
</div>
|
160 |
""", unsafe_allow_html=True)
|
161 |
|
162 |
-
if st.button("π Start Personality
|
163 |
st.session_state.started = True
|
164 |
st.session_state.selected_questions = random.sample(QUESTION_BANK, 5)
|
165 |
st.rerun()
|
@@ -168,30 +339,20 @@ else:
|
|
168 |
with st.sidebar:
|
169 |
st.title("π§ Navigation")
|
170 |
st.markdown("---")
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
help="Explore visual representations of your personality"):
|
177 |
-
st.session_state.page = "π Visual Analysis"
|
178 |
-
|
179 |
-
if st.button("π± Social Media Post", key="btn3", use_container_width=True,
|
180 |
-
help="Generate personalized social media posts"):
|
181 |
-
st.session_state.page = "π± Social Media Post"
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
if st.button("π₯ Download Report", key="btn5", use_container_width=True,
|
188 |
-
help="Download your complete personality report"):
|
189 |
-
st.session_state.page = "π₯ Download Report"
|
190 |
|
191 |
# Question flow
|
192 |
if st.session_state.current_q < 5:
|
193 |
q = st.session_state.selected_questions[st.session_state.current_q]
|
194 |
-
st.progress(st.session_state.current_q/5, text="
|
195 |
|
196 |
with st.chat_message("assistant"):
|
197 |
st.markdown(f"### {q['text']}")
|
@@ -204,85 +365,49 @@ else:
|
|
204 |
else:
|
205 |
# Process responses
|
206 |
traits = analyze_personality("\n".join(st.session_state.responses))
|
207 |
-
quote = generate_quote()
|
208 |
|
209 |
-
#
|
210 |
-
if st.session_state.page == "π
|
211 |
-
st.header("
|
212 |
cols = st.columns(5)
|
213 |
for i, (trait, score) in enumerate(traits.items()):
|
214 |
cols[i].metric(label=trait.upper(), value=f"{score:.2f}")
|
215 |
|
216 |
st.divider()
|
217 |
-
st.header("
|
218 |
-
|
219 |
-
|
220 |
-
"Score": traits.values()
|
221 |
-
})
|
222 |
-
st.altair_chart(alt.Chart(emotion_data).mark_bar().encode(
|
223 |
-
x="Trait",
|
224 |
-
y="Score",
|
225 |
-
color=alt.Color("Trait", legend=None)
|
226 |
-
), use_container_width=True)
|
227 |
-
|
228 |
-
elif st.session_state.page == "π Visual Analysis":
|
229 |
-
st.header("π Personality Visualization")
|
230 |
-
# Fix radar chart encoding
|
231 |
-
radar_data = pd.DataFrame({
|
232 |
-
"Trait": list(traits.keys()),
|
233 |
-
"Score": list(traits.values()),
|
234 |
-
"Angle": [i*(360/5) for i in range(5)]
|
235 |
-
})
|
236 |
-
|
237 |
-
chart = alt.Chart(radar_data).mark_line(point=True).encode(
|
238 |
-
theta=alt.Theta("Angle:Q", stack=True),
|
239 |
-
radius=alt.Radius("Score:Q", scale=alt.Scale(type='linear', zero=True)),
|
240 |
-
color=alt.value("#4CAF50"),
|
241 |
-
tooltip=["Trait", "Score"]
|
242 |
-
).project(type='radial')
|
243 |
-
|
244 |
-
st.altair_chart(chart, use_container_width=True)
|
245 |
|
246 |
-
elif st.session_state.page == "π± Social
|
247 |
-
st.header("
|
248 |
-
|
|
|
|
|
|
|
|
|
249 |
|
250 |
-
if st.button("Generate Post
|
251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
st.session_state.post = post
|
253 |
|
254 |
if 'post' in st.session_state:
|
255 |
-
st.markdown(f""
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
elif st.session_state.page == "π‘ Success Tips":
|
263 |
-
st.header("π‘ Personality Success Tips")
|
264 |
-
tips = [
|
265 |
-
"π± Practice daily self-reflection journaling",
|
266 |
-
"π€ Seek diverse social interactions weekly",
|
267 |
-
"π― Set SMART goals for personal development",
|
268 |
-
"π§βοΈ Incorporate mindfulness practices daily",
|
269 |
-
"π Engage in cross-disciplinary learning",
|
270 |
-
"π£οΈ Practice active listening techniques",
|
271 |
-
"π Embrace constructive feedback regularly",
|
272 |
-
"βοΈ Maintain work-life harmony",
|
273 |
-
"π Develop emotional granularity skills",
|
274 |
-
"π Challenge comfort zones monthly"
|
275 |
-
]
|
276 |
-
for tip in tips:
|
277 |
-
st.markdown(f"<div class='tip-card'>{tip}</div>", unsafe_allow_html=True)
|
278 |
-
|
279 |
-
elif st.session_state.page == "π₯ Download Report":
|
280 |
-
st.header("π₯ Download Complete Report")
|
281 |
-
pdf_buffer = create_pdf_report(traits, quote)
|
282 |
st.download_button(
|
283 |
"β¬οΈ Download PDF Report",
|
284 |
data=pdf_buffer,
|
285 |
-
file_name="
|
286 |
mime="application/pdf",
|
287 |
use_container_width=True
|
288 |
)
|
|
|
2 |
import streamlit as st
|
3 |
import torch
|
4 |
import random
|
|
|
5 |
import pandas as pd
|
6 |
from datetime import datetime
|
7 |
from reportlab.lib.pagesizes import letter
|
|
|
20 |
st.stop()
|
21 |
|
22 |
# Configure Streamlit
|
23 |
+
st.set_page_config(page_title="π PersonaCraft Pro", layout="wide", page_icon="π")
|
24 |
|
25 |
# Custom CSS
|
26 |
st.markdown("""
|
|
|
40 |
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
41 |
}
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
.social-post {
|
44 |
background: #e3f2fd;
|
45 |
padding: 20px;
|
46 |
border-radius: 15px;
|
47 |
margin: 15px 0;
|
48 |
+
border: 2px solid #2196F3;
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
}
|
50 |
|
51 |
.nav-btn {
|
52 |
margin: 8px 0;
|
53 |
width: 100%;
|
54 |
+
transition: all 0.3s ease;
|
55 |
+
}
|
56 |
+
|
57 |
+
.nav-btn:hover {
|
58 |
+
transform: scale(1.02);
|
59 |
}
|
60 |
</style>
|
61 |
""", unsafe_allow_html=True)
|
|
|
67 |
{"text": "Describe your ideal alarm clock vs reality β°", "trait": "conscientiousness"},
|
68 |
{"text": "How would you survive a surprise party? π", "trait": "neuroticism"},
|
69 |
{"text": "What's your spirit animal during deadlines? πΎ", "trait": "agreeableness"},
|
70 |
+
{"text": "Plan a perfect day... for your nemesis! π", "trait": "extraversion"}
|
|
|
|
|
|
|
|
|
|
|
71 |
]
|
72 |
|
73 |
# Session state management
|
|
|
78 |
if 'responses' not in st.session_state:
|
79 |
st.session_state.responses = []
|
80 |
if 'page' not in st.session_state:
|
81 |
+
st.session_state.page = "π Report"
|
82 |
|
83 |
+
# Core functions
|
84 |
+
def generate_content(prompt):
|
85 |
+
"""Unified content generation using Groq"""
|
86 |
response = groq_client.chat.completions.create(
|
87 |
model="mixtral-8x7b-32768",
|
88 |
messages=[{"role": "user", "content": prompt}],
|
|
|
91 |
return response.choices[0].message.content
|
92 |
|
93 |
def analyze_personality(text):
|
94 |
+
"""Personality analysis using KevSun model"""
|
95 |
encoded_input = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=64)
|
96 |
with torch.no_grad():
|
97 |
outputs = model(**encoded_input)
|
98 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
99 |
return {trait: score.item() for trait, score in zip(OCEAN_TRAITS, predictions[0])}
|
100 |
|
101 |
+
def create_pdf_report(traits):
|
102 |
+
"""Generate PDF report"""
|
103 |
buffer = io.BytesIO()
|
104 |
p = canvas.Canvas(buffer, pagesize=letter)
|
105 |
p.setFont("Helvetica-Bold", 16)
|
106 |
+
p.drawString(100, 750, "π PersonaCraft Pro Report π")
|
107 |
p.setFont("Helvetica", 12)
|
108 |
|
109 |
y_position = 700
|
|
|
111 |
p.drawString(100, y_position, f"{trait.upper()}: {score:.2f}")
|
112 |
y_position -= 20
|
113 |
|
|
|
|
|
114 |
p.save()
|
115 |
buffer.seek(0)
|
116 |
return buffer
|
117 |
|
118 |
+
# Main UI
|
119 |
+
if not st.session_state.started:
|
120 |
+
st.markdown(f"""
|
121 |
+
<div class="quote-box">
|
122 |
+
<h2>π Welcome to PersonaCraft Pro! π</h2>
|
123 |
+
<h3>{generate_content("Generate an inspirational quote about self-discovery with emojis")}</h3>
|
124 |
+
</div>
|
125 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
if st.button("π Start Personality Analysis!", use_container_width=True):
|
128 |
+
st.session_state.started = True
|
129 |
+
st.session_state.selected_questions = random.sample(QUESTION_BANK, 5)
|
130 |
+
st.rerun()
|
131 |
+
else:
|
132 |
+
# Sidebar navigation
|
133 |
+
with st.sidebar:
|
134 |
+
st.title("π§ Navigation")
|
135 |
+
st.markdown("---")
|
136 |
+
nav_options = {
|
137 |
+
"π Report": "View personality breakdown",
|
138 |
+
"π± Social Post": "Generate social media content",
|
139 |
+
"π₯ Download": "Get full PDF report"
|
140 |
+
}
|
141 |
+
|
142 |
+
for option, help_text in nav_options.items():
|
143 |
+
if st.button(option, key=option, use_container_width=True, help=help_text):
|
144 |
+
st.session_state.page = option
|
145 |
+
|
146 |
+
# Question flow
|
147 |
+
if st.session_state.current_q < 5:
|
148 |
+
q = st.session_state.selected_questions[st.session_state.current_q]
|
149 |
+
st.progress(st.session_state.current_q/5, text="Analysis Progress")
|
150 |
+
|
151 |
+
with st.chat_message("assistant"):
|
152 |
+
st.markdown(f"### {q['text']}")
|
153 |
+
user_input = st.text_input("Your response:", key=f"q{st.session_state.current_q}")
|
154 |
+
|
155 |
+
if st.button("Next β‘οΈ"):
|
156 |
+
st.session_state.responses.append(user_input)
|
157 |
+
st.session_state.current_q += 1
|
158 |
+
st.rerun()
|
159 |
+
else:
|
160 |
+
# Process responses
|
161 |
+
traits = analyze_personality("\n".join(st.session_state.responses))
|
162 |
+
|
163 |
+
# Page rendering
|
164 |
+
if st.session_state.page == "π Report":
|
165 |
+
st.header("π Personality Breakdown")
|
166 |
+
cols = st.columns(5)
|
167 |
+
for i, (trait, score) in enumerate(traits.items()):
|
168 |
+
cols[i].metric(label=trait.upper(), value=f"{score:.2f}")
|
169 |
+
|
170 |
+
st.divider()
|
171 |
+
st.header("π‘ Key Insights")
|
172 |
+
insights = generate_content(f"Generate 3 concise personality insights based on these scores: {traits}")
|
173 |
+
st.markdown(f"<div class='social-post'>{insights}</div>", unsafe_allow_html=True)
|
174 |
+
|
175 |
+
elif st.session_state.page == "π± Social Post":
|
176 |
+
st.header("π― Create Social Content")
|
177 |
+
col1, col2 = st.columns(2)
|
178 |
+
with col1:
|
179 |
+
platform = st.selectbox("Select Platform:", ["LinkedIn", "Instagram", "Facebook", "WhatsApp", "Twitter"])
|
180 |
+
with col2:
|
181 |
+
post_style = st.radio("Post Style:", ["π Funny", "πΌ Professional"])
|
182 |
+
|
183 |
+
if st.button("β¨ Generate Post"):
|
184 |
+
style_prompt = "Include humor and memes" if "Funny" in post_style else "Keep professional and inspirational"
|
185 |
+
prompt = f"""Create a {platform} post about personal growth with these traits: {traits}
|
186 |
+
- Style: {style_prompt}
|
187 |
+
- Include relevant emojis
|
188 |
+
- Max 280 characters
|
189 |
+
- Add platform-appropriate hashtags"""
|
190 |
+
|
191 |
+
post = generate_content(prompt)
|
192 |
+
st.session_state.post = post
|
193 |
+
|
194 |
+
if 'post' in st.session_state:
|
195 |
+
st.markdown(f"<div class='social-post'>{st.session_state.post}</div>", unsafe_allow_html=True)
|
196 |
+
st.button("π Copy Content", on_click=lambda: st.write(st.session_state.post))
|
197 |
+
|
198 |
+
elif st.session_state.page == "π₯ Download":
|
199 |
+
st.header("π Complete Report")
|
200 |
+
pdf_buffer = create_pdf_report(traits)
|
201 |
+
st.download_button(
|
202 |
+
"β¬οΈ Download PDF Report",
|
203 |
+
data=pdf_buffer,
|
204 |
+
file_name="persona_report.pdf",
|
205 |
+
mime="application/pdf",
|
206 |
+
use_container_width=True
|
207 |
+
)# app.py
|
208 |
+
import streamlit as st
|
209 |
+
import torch
|
210 |
+
import random
|
211 |
+
import pandas as pd
|
212 |
+
from datetime import datetime
|
213 |
+
from reportlab.lib.pagesizes import letter
|
214 |
+
from reportlab.pdfgen import canvas
|
215 |
+
import io
|
216 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
217 |
+
from groq import Groq
|
218 |
+
|
219 |
+
# Initialize components
|
220 |
+
try:
|
221 |
+
groq_client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
222 |
+
model = AutoModelForSequenceClassification.from_pretrained("KevSun/Personality_LM", ignore_mismatched_sizes=True)
|
223 |
+
tokenizer = AutoTokenizer.from_pretrained("KevSun/Personality_LM")
|
224 |
+
except Exception as e:
|
225 |
+
st.error(f"Initialization error: {str(e)}")
|
226 |
+
st.stop()
|
227 |
+
|
228 |
+
# Configure Streamlit
|
229 |
+
st.set_page_config(page_title="π PersonaCraft Pro", layout="wide", page_icon="π")
|
230 |
+
|
231 |
+
# Custom CSS
|
232 |
+
st.markdown("""
|
233 |
+
<style>
|
234 |
+
@keyframes fadeIn {
|
235 |
+
from { opacity: 0; }
|
236 |
+
to { opacity: 1; }
|
237 |
+
}
|
238 |
+
|
239 |
+
.quote-box {
|
240 |
+
animation: fadeIn 1s ease-in;
|
241 |
+
border-left: 5px solid #4CAF50;
|
242 |
+
padding: 20px;
|
243 |
+
margin: 20px 0;
|
244 |
+
background: #f8fff9;
|
245 |
+
border-radius: 10px;
|
246 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
247 |
+
}
|
248 |
+
|
249 |
+
.social-post {
|
250 |
+
background: #e3f2fd;
|
251 |
+
padding: 20px;
|
252 |
+
border-radius: 15px;
|
253 |
+
margin: 15px 0;
|
254 |
+
border: 2px solid #2196F3;
|
255 |
+
}
|
256 |
+
|
257 |
+
.nav-btn {
|
258 |
+
margin: 8px 0;
|
259 |
+
width: 100%;
|
260 |
+
transition: all 0.3s ease;
|
261 |
+
}
|
262 |
+
|
263 |
+
.nav-btn:hover {
|
264 |
+
transform: scale(1.02);
|
265 |
+
}
|
266 |
+
</style>
|
267 |
+
""", unsafe_allow_html=True)
|
268 |
+
|
269 |
+
# Personality configuration
|
270 |
+
OCEAN_TRAITS = ["agreeableness", "openness", "conscientiousness", "extraversion", "neuroticism"]
|
271 |
+
QUESTION_BANK = [
|
272 |
+
{"text": "If your personality was a pizza topping, what would it be? π", "trait": "openness"},
|
273 |
+
{"text": "Describe your ideal alarm clock vs reality β°", "trait": "conscientiousness"},
|
274 |
+
{"text": "How would you survive a surprise party? π", "trait": "neuroticism"},
|
275 |
+
{"text": "What's your spirit animal during deadlines? πΎ", "trait": "agreeableness"},
|
276 |
+
{"text": "Plan a perfect day... for your nemesis! π", "trait": "extraversion"}
|
277 |
+
]
|
278 |
+
|
279 |
+
# Session state management
|
280 |
+
if 'started' not in st.session_state:
|
281 |
+
st.session_state.started = False
|
282 |
+
if 'current_q' not in st.session_state:
|
283 |
+
st.session_state.current_q = 0
|
284 |
+
if 'responses' not in st.session_state:
|
285 |
+
st.session_state.responses = []
|
286 |
+
if 'page' not in st.session_state:
|
287 |
+
st.session_state.page = "π Report"
|
288 |
+
|
289 |
+
# Core functions
|
290 |
+
def generate_content(prompt):
|
291 |
+
"""Unified content generation using Groq"""
|
292 |
response = groq_client.chat.completions.create(
|
293 |
model="mixtral-8x7b-32768",
|
294 |
messages=[{"role": "user", "content": prompt}],
|
|
|
296 |
)
|
297 |
return response.choices[0].message.content
|
298 |
|
299 |
+
def analyze_personality(text):
|
300 |
+
"""Personality analysis using KevSun model"""
|
301 |
+
encoded_input = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=64)
|
302 |
+
with torch.no_grad():
|
303 |
+
outputs = model(**encoded_input)
|
304 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
305 |
+
return {trait: score.item() for trait, score in zip(OCEAN_TRAITS, predictions[0])}
|
306 |
+
|
307 |
+
def create_pdf_report(traits):
|
308 |
+
"""Generate PDF report"""
|
309 |
+
buffer = io.BytesIO()
|
310 |
+
p = canvas.Canvas(buffer, pagesize=letter)
|
311 |
+
p.setFont("Helvetica-Bold", 16)
|
312 |
+
p.drawString(100, 750, "π PersonaCraft Pro Report π")
|
313 |
+
p.setFont("Helvetica", 12)
|
314 |
+
|
315 |
+
y_position = 700
|
316 |
+
for trait, score in traits.items():
|
317 |
+
p.drawString(100, y_position, f"{trait.upper()}: {score:.2f}")
|
318 |
+
y_position -= 20
|
319 |
+
|
320 |
+
p.save()
|
321 |
+
buffer.seek(0)
|
322 |
+
return buffer
|
323 |
+
|
324 |
# Main UI
|
325 |
if not st.session_state.started:
|
326 |
st.markdown(f"""
|
327 |
<div class="quote-box">
|
328 |
+
<h2>π Welcome to PersonaCraft Pro! π</h2>
|
329 |
+
<h3>{generate_content("Generate an inspirational quote about self-discovery with emojis")}</h3>
|
330 |
</div>
|
331 |
""", unsafe_allow_html=True)
|
332 |
|
333 |
+
if st.button("π Start Personality Analysis!", use_container_width=True):
|
334 |
st.session_state.started = True
|
335 |
st.session_state.selected_questions = random.sample(QUESTION_BANK, 5)
|
336 |
st.rerun()
|
|
|
339 |
with st.sidebar:
|
340 |
st.title("π§ Navigation")
|
341 |
st.markdown("---")
|
342 |
+
nav_options = {
|
343 |
+
"π Report": "View personality breakdown",
|
344 |
+
"π± Social Post": "Generate social media content",
|
345 |
+
"π₯ Download": "Get full PDF report"
|
346 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
347 |
|
348 |
+
for option, help_text in nav_options.items():
|
349 |
+
if st.button(option, key=option, use_container_width=True, help=help_text):
|
350 |
+
st.session_state.page = option
|
|
|
|
|
|
|
|
|
351 |
|
352 |
# Question flow
|
353 |
if st.session_state.current_q < 5:
|
354 |
q = st.session_state.selected_questions[st.session_state.current_q]
|
355 |
+
st.progress(st.session_state.current_q/5, text="Analysis Progress")
|
356 |
|
357 |
with st.chat_message("assistant"):
|
358 |
st.markdown(f"### {q['text']}")
|
|
|
365 |
else:
|
366 |
# Process responses
|
367 |
traits = analyze_personality("\n".join(st.session_state.responses))
|
|
|
368 |
|
369 |
+
# Page rendering
|
370 |
+
if st.session_state.page == "π Report":
|
371 |
+
st.header("π Personality Breakdown")
|
372 |
cols = st.columns(5)
|
373 |
for i, (trait, score) in enumerate(traits.items()):
|
374 |
cols[i].metric(label=trait.upper(), value=f"{score:.2f}")
|
375 |
|
376 |
st.divider()
|
377 |
+
st.header("π‘ Key Insights")
|
378 |
+
insights = generate_content(f"Generate 3 concise personality insights based on these scores: {traits}")
|
379 |
+
st.markdown(f"<div class='social-post'>{insights}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
380 |
|
381 |
+
elif st.session_state.page == "π± Social Post":
|
382 |
+
st.header("π― Create Social Content")
|
383 |
+
col1, col2 = st.columns(2)
|
384 |
+
with col1:
|
385 |
+
platform = st.selectbox("Select Platform:", ["LinkedIn", "Instagram", "Facebook", "WhatsApp", "Twitter"])
|
386 |
+
with col2:
|
387 |
+
post_style = st.radio("Post Style:", ["π Funny", "πΌ Professional"])
|
388 |
|
389 |
+
if st.button("β¨ Generate Post"):
|
390 |
+
style_prompt = "Include humor and memes" if "Funny" in post_style else "Keep professional and inspirational"
|
391 |
+
prompt = f"""Create a {platform} post about personal growth with these traits: {traits}
|
392 |
+
- Style: {style_prompt}
|
393 |
+
- Include relevant emojis
|
394 |
+
- Max 280 characters
|
395 |
+
- Add platform-appropriate hashtags"""
|
396 |
+
|
397 |
+
post = generate_content(prompt)
|
398 |
st.session_state.post = post
|
399 |
|
400 |
if 'post' in st.session_state:
|
401 |
+
st.markdown(f"<div class='social-post'>{st.session_state.post}</div>", unsafe_allow_html=True)
|
402 |
+
st.button("π Copy Content", on_click=lambda: st.write(st.session_state.post))
|
403 |
+
|
404 |
+
elif st.session_state.page == "π₯ Download":
|
405 |
+
st.header("π Complete Report")
|
406 |
+
pdf_buffer = create_pdf_report(traits)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
407 |
st.download_button(
|
408 |
"β¬οΈ Download PDF Report",
|
409 |
data=pdf_buffer,
|
410 |
+
file_name="persona_report.pdf",
|
411 |
mime="application/pdf",
|
412 |
use_container_width=True
|
413 |
)
|