import streamlit as st from fpdf import FPDF import matplotlib.pyplot as plt import tempfile import os import io # 1. Radar chart for cognitive scores def create_radar_chart(scores): labels = list(scores.keys()) values = list(scores.values()) values += values[:1] angles = [n / float(len(labels)) * 2 * 3.1416 for n in range(len(labels))] angles += angles[:1] fig, ax = plt.subplots(figsize=(4, 4), subplot_kw=dict(polar=True)) ax.plot(angles, values, color='blue', linewidth=2) ax.fill(angles, values, color='skyblue', alpha=0.4) ax.set_xticks(angles[:-1]) ax.set_xticklabels(labels) ax.set_yticklabels([]) ax.set_title("Cognitive Radar Chart") tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png") plt.savefig(tmp.name) plt.close(fig) return tmp.name # 2. Heatmap for EI matrix def create_ei_heatmap(data): fig, ax = plt.subplots(figsize=(4, 3)) cax = ax.imshow(data, cmap='YlOrRd', aspect='auto') ax.set_title("Emotional Intelligence Heatmap") ax.set_xticks(range(len(data[0]))) ax.set_yticks(range(len(data))) fig.colorbar(cax) tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png") plt.savefig(tmp.name) plt.close(fig) return tmp.name # 3. Main report function def generate_report(): st.subheader("📄 Export Your Weekly Report") name = st.text_input("Enter your name:") st.markdown("### Cognitive Task Scores (/10)") cog_scores = { "Symbol Match": st.slider("Symbol Match", 0, 10, 7), "Trail Making": st.slider("Trail Making", 0, 10, 6), "CPT Task": st.slider("CPT", 0, 10, 8) } st.markdown("### EI Drill Scores (1-5 Scale)") ei_matrix = [ [st.slider("Emoji Label", 1, 5, 3), st.slider("Body Scan", 1, 5, 2)], [st.slider("Gratitude Journal", 1, 5, 4), st.slider("Reflective Writing", 1, 5, 3)] ] big_five_score = st.slider("Big Five Personality Score (/25):", 0, 25, 18) if st.button("Download PDF Report"): pdf = FPDF() pdf.add_page() pdf.set_auto_page_break(auto=True, margin=15) # Title and intro pdf.set_font("Arial", 'B', 14) pdf.cell(0, 10, f"MindMentor Report - {name}", ln=True, align='C') pdf.set_font("Arial", '', 11) pdf.multi_cell(0, 8, "This weekly report summarizes your progress in cognitive brain training, " "emotional intelligence reflection, and personality profiling based on your " "recent interaction with the MindMentor App." ) pdf.ln(4) # Section 1: Personality pdf.set_font("Arial", 'B', 12) pdf.cell(0, 10, "Personality Test", ln=True) pdf.set_font("Arial", '', 11) pdf.cell(0, 8, f"Big Five Score: {big_five_score} / 25", ln=True) if big_five_score >= 20: pdf.cell(0, 8, "Interpretation: High emotional stability and openness to experience.", ln=True) elif big_five_score >= 15: pdf.cell(0, 8, "Interpretation: Balanced traits with moderate emotional awareness.", ln=True) else: pdf.cell(0, 8, "Interpretation: Consider focusing on self-awareness and adaptability.", ln=True) pdf.ln(5) # Section 2: Cognitive pdf.set_font("Arial", 'B', 12) pdf.cell(0, 10, "Cognitive Task Summary", ln=True) pdf.set_font("Arial", '', 11) for task, score in cog_scores.items(): interpretation = "Strong" if score >= 8 else "Moderate" if score >= 5 else "Needs Improvement" pdf.cell(0, 8, f"{task}: {score}/10 - {interpretation} performance", ln=True) radar_path = create_radar_chart(cog_scores) pdf.image(radar_path, w=100) os.unlink(radar_path) # Section 3: EI pdf.set_font("Arial", 'B', 12) pdf.cell(0, 10, "Emotional Intelligence Reflection", ln=True) ei_labels = ["Emoji Label", "Body Scan", "Gratitude Journal", "Reflective Writing"] ei_flat = [ei_matrix[0][0], ei_matrix[0][1], ei_matrix[1][0], ei_matrix[1][1]] for label, score in zip(ei_labels, ei_flat): note = "Good" if score >= 4 else "Average" if score >= 3 else "Low" pdf.cell(0, 8, f"{label}: {score}/5 - {note} emotional insight", ln=True) heatmap_path = create_ei_heatmap(ei_matrix) pdf.image(heatmap_path, w=100) os.unlink(heatmap_path) # Final Summary pdf.set_font("Arial", 'B', 12) pdf.cell(0, 10, "Overall Summary", ln=True) pdf.set_font("Arial", '', 11) pdf.multi_cell(0, 8, "Based on this week's session, you demonstrate strong potential in attention control " "and inhibitory response (CPT). Emotional awareness and reflection skills are developing steadily. " "Keep up your cognitive training and continue practicing daily EI drills for balanced growth." ) # Convert to BytesIO for download pdf_output = pdf.output(dest='S').encode('latin1') pdf_buffer = io.BytesIO(pdf_output) st.download_button( label="📄 Download Your MindMentor PDF Report", data=pdf_buffer, file_name=f"{name}_MindMentor_Report.pdf", mime="application/pdf" )