Spaces:
Sleeping
Sleeping
File size: 4,274 Bytes
7077f97 81379ec 7077f97 bd3a27a 11a490b 7077f97 11a490b 116c6ba 44faf53 6a3132f a88fa0b 6a3132f a88fa0b 6a3132f a88fa0b 6a3132f db0155e 6a3132f a88fa0b 6a3132f a88fa0b 6a3132f a88fa0b 7077f97 11a490b 7077f97 11a490b 7077f97 44faf53 11a490b 7077f97 a88fa0b 11a490b 44faf53 db0155e a88fa0b db0155e a88fa0b db0155e 11a490b a88fa0b 11a490b 81379ec 11a490b a88fa0b 116c6ba 11a490b 44faf53 11a490b 44faf53 a88fa0b 44faf53 a88fa0b 11a490b a88fa0b 11a490b a88fa0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# 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() |