Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from transformers import pipeline
|
|
| 6 |
import numpy as np
|
| 7 |
import re
|
| 8 |
import random
|
|
|
|
| 9 |
from reportlab.lib.pagesizes import letter
|
| 10 |
from reportlab.pdfgen import canvas
|
| 11 |
import io
|
|
@@ -13,7 +14,11 @@ import io
|
|
| 13 |
# Initialize components
|
| 14 |
groq_client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
| 15 |
personality_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
| 16 |
-
big5_model = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Configure Streamlit
|
| 19 |
st.set_page_config(page_title="π§ Personality Prodigy", layout="wide", page_icon="π€")
|
|
@@ -68,8 +73,8 @@ def get_dynamic_questions(responses):
|
|
| 68 |
|
| 69 |
def analyze_big5(text):
|
| 70 |
"""Analyze Big Five personality traits using OCEAN model"""
|
| 71 |
-
|
| 72 |
-
return {label.split("_")[1].lower(): score for
|
| 73 |
|
| 74 |
def analyze_emotions(text):
|
| 75 |
"""Detect emotional tone using RoBERTa"""
|
|
@@ -91,13 +96,13 @@ def generate_quote(traits):
|
|
| 91 |
def generate_tips(traits):
|
| 92 |
"""Generate evidence-based psychological tips"""
|
| 93 |
tips = []
|
| 94 |
-
if traits
|
| 95 |
tips.extend(["π¨ Try creative cross-training (write a poem about your work)",
|
| 96 |
"π Learn something new daily about different cultures"])
|
| 97 |
-
if traits
|
| 98 |
tips.extend(["π§ Practice box breathing: 4s in, 4s hold, 4s out",
|
| 99 |
"π Keep a worry journal to externalize anxieties"])
|
| 100 |
-
return tips[:10]
|
| 101 |
|
| 102 |
def create_personality_report(responses):
|
| 103 |
"""Generate comprehensive personality analysis"""
|
|
@@ -109,6 +114,41 @@ def create_personality_report(responses):
|
|
| 109 |
"tips": generate_tips(analyze_big5(text))
|
| 110 |
}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
# Session state management
|
| 113 |
if 'responses' not in st.session_state:
|
| 114 |
st.session_state.responses = []
|
|
@@ -181,13 +221,13 @@ else:
|
|
| 181 |
st.markdown(f"- {tip}")
|
| 182 |
|
| 183 |
# Downloadable Report
|
| 184 |
-
pdf_buffer =
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
|
| 192 |
# Sidebar
|
| 193 |
with st.sidebar:
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import re
|
| 8 |
import random
|
| 9 |
+
from datetime import datetime
|
| 10 |
from reportlab.lib.pagesizes import letter
|
| 11 |
from reportlab.pdfgen import canvas
|
| 12 |
import io
|
|
|
|
| 14 |
# Initialize components
|
| 15 |
groq_client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
| 16 |
personality_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
| 17 |
+
big5_model = pipeline(
|
| 18 |
+
"text-classification",
|
| 19 |
+
model="ElizaB/bigfive-personality-traits",
|
| 20 |
+
top_k=5 # Get all Big Five traits
|
| 21 |
+
)
|
| 22 |
|
| 23 |
# Configure Streamlit
|
| 24 |
st.set_page_config(page_title="π§ Personality Prodigy", layout="wide", page_icon="π€")
|
|
|
|
| 73 |
|
| 74 |
def analyze_big5(text):
|
| 75 |
"""Analyze Big Five personality traits using OCEAN model"""
|
| 76 |
+
results = big5_model(text[:512])
|
| 77 |
+
return {result['label'].split("_")[1].lower(): result['score'] for result in results}
|
| 78 |
|
| 79 |
def analyze_emotions(text):
|
| 80 |
"""Detect emotional tone using RoBERTa"""
|
|
|
|
| 96 |
def generate_tips(traits):
|
| 97 |
"""Generate evidence-based psychological tips"""
|
| 98 |
tips = []
|
| 99 |
+
if traits.get('openness', 0) > 0.7:
|
| 100 |
tips.extend(["π¨ Try creative cross-training (write a poem about your work)",
|
| 101 |
"π Learn something new daily about different cultures"])
|
| 102 |
+
if traits.get('neuroticism', 0) > 0.6:
|
| 103 |
tips.extend(["π§ Practice box breathing: 4s in, 4s hold, 4s out",
|
| 104 |
"π Keep a worry journal to externalize anxieties"])
|
| 105 |
+
return tips[:10]
|
| 106 |
|
| 107 |
def create_personality_report(responses):
|
| 108 |
"""Generate comprehensive personality analysis"""
|
|
|
|
| 114 |
"tips": generate_tips(analyze_big5(text))
|
| 115 |
}
|
| 116 |
|
| 117 |
+
def create_pdf_report(report):
|
| 118 |
+
"""Generate downloadable PDF report"""
|
| 119 |
+
buffer = io.BytesIO()
|
| 120 |
+
p = canvas.Canvas(buffer, pagesize=letter)
|
| 121 |
+
|
| 122 |
+
# Header
|
| 123 |
+
p.setFont("Helvetica-Bold", 16)
|
| 124 |
+
p.drawString(100, 750, "π Personality Prodigy Report π")
|
| 125 |
+
p.setFont("Helvetica", 12)
|
| 126 |
+
|
| 127 |
+
# Content
|
| 128 |
+
y_position = 700
|
| 129 |
+
content = [
|
| 130 |
+
f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M')}",
|
| 131 |
+
"",
|
| 132 |
+
"Big Five Personality Traits:",
|
| 133 |
+
*[f"- {trait.upper()}: {score:.2f}" for trait, score in report['big5'].items()],
|
| 134 |
+
"",
|
| 135 |
+
"Emotional Analysis:",
|
| 136 |
+
*[f"- {emo['label']}: {emo['score']:.2f}" for emo in report['emotions']],
|
| 137 |
+
"",
|
| 138 |
+
"Personalized Quote:",
|
| 139 |
+
report['quote'],
|
| 140 |
+
"",
|
| 141 |
+
"Growth Tips:",
|
| 142 |
+
*[f"{i+1}. {tip}" for i, tip in enumerate(report['tips'])]
|
| 143 |
+
|
| 144 |
+
for line in content:
|
| 145 |
+
p.drawString(100, y_position, line)
|
| 146 |
+
y_position -= 15
|
| 147 |
+
|
| 148 |
+
p.save()
|
| 149 |
+
buffer.seek(0)
|
| 150 |
+
return buffer
|
| 151 |
+
|
| 152 |
# Session state management
|
| 153 |
if 'responses' not in st.session_state:
|
| 154 |
st.session_state.responses = []
|
|
|
|
| 221 |
st.markdown(f"- {tip}")
|
| 222 |
|
| 223 |
# Downloadable Report
|
| 224 |
+
pdf_buffer = create_pdf_report(report)
|
| 225 |
+
st.download_button(
|
| 226 |
+
"π₯ Download Full Report",
|
| 227 |
+
data=pdf_buffer,
|
| 228 |
+
file_name="personality_report.pdf",
|
| 229 |
+
mime="application/pdf"
|
| 230 |
+
)
|
| 231 |
|
| 232 |
# Sidebar
|
| 233 |
with st.sidebar:
|