Spaces:
Sleeping
Sleeping
# app.py | |
import streamlit as st | |
from groq import Groq | |
from textblob import TextBlob | |
from transformers import pipeline | |
import re | |
import random | |
from datetime import datetime | |
from reportlab.lib.pagesizes import letter | |
from reportlab.pdfgen import canvas | |
import io | |
# Initialize components with proper error handling | |
try: | |
groq_client = Groq(api_key=st.secrets["GROQ_API_KEY"]) | |
except KeyError: | |
st.error("GROQ_API_KEY missing in secrets! Please add it in Hugging Face settings.") | |
st.stop() | |
# Initialize models with verified working versions | |
try: | |
personality_classifier = pipeline( | |
"text-classification", | |
model="j-hartmann/emotion-english-distilroberta-base" | |
) | |
big5_model = pipeline( | |
"text-classification", | |
model="m3hrdadfi/bigfive-personality-traits", # Verified working model | |
top_k=5 | |
) | |
except Exception as e: | |
st.error(f"Model loading error: {str(e)}") | |
st.stop() | |
# Configure Streamlit | |
st.set_page_config(page_title="π§ Personality Prodigy", layout="wide", page_icon="π€") | |
# Custom CSS (unchanged) | |
st.markdown(""" | |
<style> | |
@keyframes rainbow { | |
0% { color: #ff0000; } | |
20% { color: #ff8000; } | |
40% { color: #ffff00; } | |
60% { color: #00ff00; } | |
80% { color: #0000ff; } | |
100% { color: #ff00ff; } | |
} | |
.personality-title { | |
animation: rainbow 3s infinite; | |
font-size: 2.5em !important; | |
} | |
.progress-bar { | |
height: 25px; | |
border-radius: 15px; | |
background: linear-gradient(90deg, #FF6B6B 0%, #4ECDC4 100%); | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Dynamic questions pool (unchanged) | |
QUESTION_BANK = [ | |
{"text": "What's your most creative procrastination method? π¨π", "trait": "openness"}, | |
{"text": "How do you react when plans change suddenly? πͺοΈπ€", "trait": "neuroticism"}, | |
{"text": "Describe your ideal weekend vs reality ποΈπ οΈ", "trait": "extraversion"}, | |
{"text": "What's your 'guilty pleasure' productivity hack? π«π", "trait": "conscientiousness"}, | |
{"text": "How do you comfort a stressed friend? π€π¬", "trait": "agreeableness"}, | |
{"text": "What would you do if you were invisible for a day? π»π", "trait": "openness"}, | |
{"text": "Your reaction to unexpected criticism? π₯π‘", "trait": "neuroticism"}, | |
{"text": "Plan a party vs enjoy quiet night? ππ", "trait": "extraversion"}, | |
{"text": "How do you handle missed deadlines? βπ€·", "trait": "conscientiousness"}, | |
{"text": "Describe your perfect collaboration π₯β¨", "trait": "agreeableness"} | |
] | |
def analyze_big5(text): | |
"""Process Big Five personality traits from model output""" | |
results = big5_model(text[:512]) | |
trait_map = { | |
'EXTRAVERSION': 'extraversion', | |
'NEUROTICISM': 'neuroticism', | |
'CONSCIENTIOUSNESS': 'conscientiousness', | |
'OPENNESS': 'openness', | |
'AGREEABLENESS': 'agreeableness' | |
} | |
return {trait_map[res['label']: res['score'] for res in results} | |
def get_dynamic_questions(responses): | |
"""Select questions based on personality patterns""" | |
if not responses: | |
return random.sample(QUESTION_BANK, 5) | |
traits = analyze_big5("\n".join(responses)) | |
dominant_trait = max(traits, key=traits.get) | |
return [q for q in QUESTION_BANK if q['trait'] == dominant_trait][:3] + random.sample(QUESTION_BANK, 2) | |
# Rest of the functions remain unchanged from previous working version | |
# [Keep analyze_emotions, generate_quote, generate_tips, create_personality_report, create_pdf_report] | |
# Session state management | |
if 'responses' not in st.session_state: | |
st.session_state.responses = [] | |
if 'current_q' not in st.session_state: | |
st.session_state.current_q = 0 | |
if 'questions' not in st.session_state: | |
st.session_state.questions = get_dynamic_questions([]) | |
# Main UI (unchanged except for rerun) | |
# ... | |
# In the dynamic question flow section: | |
if st.button("Next β‘οΈ"): | |
st.session_state.responses.append(user_input) | |
st.session_state.current_q += 1 | |
# Update questions based on responses | |
if st.session_state.current_q == len(st.session_state.questions): | |
st.session_state.questions = get_dynamic_questions(st.session_state.responses) | |
st.session_state.current_q = 0 | |
st.rerun() # Changed from experimental_rerun() |