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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py 09-11-2024, 19:00 CET
|
| 2 |
+
# C:\Users\jfhmb\BIG 5 OCEAN PSYCHOLOGICAL PERSON PROFILE FROM ANSWERS TO QUESTION SET
|
| 3 |
+
# EVERNOTE :
|
| 4 |
+
# SYNTHETIC RANDOM BIG 5 PSYCHOLOGICAL GROUNDTRUTH PROFILE
|
| 5 |
+
# —> BIOGRAPHICAL PERSON DESCRIPTION
|
| 6 |
+
# —> ANTWOORDEN OP SET VAN VRAGEN OM PROFIEL UIT TE KUNNEN AFLEIDEN
|
| 7 |
+
# —> PSYCHOLOGISCH BIG 5 OCEAN PROFIEL
|
| 8 |
+
# -> VERGELIJKING BIG 5 OCEAN GROUNDTRUTH EN AFGELEID PROFIEL
|
| 9 |
+
#
|
| 10 |
+
# https://share.evernote.com/note/ba75c830-9a90-e368-b8f7-0c2a6f83b3c8
|
| 11 |
+
#
|
| 12 |
+
# EN
|
| 13 |
+
#
|
| 14 |
+
# KOPIE - SYNTHETIC RANDOM BIG 5 PSYCHOLOGICAL GROUNDTRUTH PROFILE —> BIOGRAPHICAL PERSON DESCRIPTION —> ANTWOORDEN OP SET VAN VRAGEN OM PROFIEL UIT TE KUNNEN AFLEIDEN —> PSYCHOLOGISCH BIG 5 OCEAN PROFIEL - VERGELIJKING BIG 5 OCEAN GROUNDTRUTH EN AFGELEID P
|
| 15 |
+
#
|
| 16 |
+
# https://share.evernote.com/note/04bd5628-aeb5-642f-0b69-c4d615aedf5e
|
| 17 |
+
#
|
| 18 |
+
# 03-11-2024
|
| 19 |
+
|
| 20 |
+
# CHANGES:
|
| 21 |
+
# IN enereer_big5_profiel(antwoorden):
|
| 22 |
+
#
|
| 23 |
+
# """AAN HET EINDE:
|
| 24 |
+
# GEEF EEN WEERGAVE VAN HET BIG 5 OCEAN MODEL IN 5 CIJFERS TUSSEN 1 EN 5,
|
| 25 |
+
# EN EEN SCHATTING VAN DE ONNAUWKEURIGHEID IN DIE CIJFERS
|
| 26 |
+
# IN DE VORM VAN EEN RANGE VAN MINIMUM TO MAXIMUM WAARDE DIE UIT DE ANALYSE KAN WORDEN AFGELEID.
|
| 27 |
+
# GEEF DIT DEEL VAN JE ANWOORD IN DE VORM VAN EEN KEURIGE TABEL IN MARKDOWN FORMAAT.\n"""
|
| 28 |
+
#
|
| 29 |
+
# IN ...
|
| 30 |
+
#
|
| 31 |
+
# PDF generation and downloading working
|
| 32 |
+
|
| 33 |
+
# Deze PDF download werkt in deze versie !!!!!
|
| 34 |
+
import streamlit as st
|
| 35 |
+
import os
|
| 36 |
+
from datetime import datetime
|
| 37 |
+
from groq import Groq
|
| 38 |
+
from io import BytesIO
|
| 39 |
+
from reportlab.lib.pagesizes import A4
|
| 40 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle
|
| 41 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 42 |
+
from reportlab.lib import colors
|
| 43 |
+
import re
|
| 44 |
+
|
| 45 |
+
# Initialize the GROQ client
|
| 46 |
+
client = Groq(
|
| 47 |
+
api_key=os.environ.get("GROQ_API_KEY"), # Ensure the API key is set in the environment variables
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Define the questions to ask the user
|
| 51 |
+
vragenlijst = [
|
| 52 |
+
"Hoe zou je jezelf in drie woorden beschrijven?",
|
| 53 |
+
"Wat beschouw je als je grootste sterkte en je grootste zwakte? Waarom?",
|
| 54 |
+
"Welke drie waarden vind je het belangrijkste in het leven?",
|
| 55 |
+
"Wat beschouw je als de belangrijkste motivatie achter je handelen?",
|
| 56 |
+
"Hoe vaak probeer je nieuwe dingen, zoals een nieuw gerecht, hobby of reismogelijkheid?",
|
| 57 |
+
"Kun je een voorbeeld geven van een situatie waarin je buiten je comfortzone bent gegaan?",
|
| 58 |
+
"Wat voor soort boeken of films trekt je aan?",
|
| 59 |
+
"Hoe belangrijk is het voor jou om plannen en doelen te hebben? Waarom?",
|
| 60 |
+
"Hoe ga je om met deadlines? Wat doe je als een deadline lastig te halen is?",
|
| 61 |
+
"Hoe voel je je over grote sociale bijeenkomsten en feestjes? Vind je ze leuk of vermoeiend?",
|
| 62 |
+
"Hoe belangrijk zijn vriendschappen voor je, en hoeveel tijd besteed je eraan?",
|
| 63 |
+
"Wat geeft je energie: tijd doorbrengen met anderen of alleen zijn?",
|
| 64 |
+
"Hoe ga je om met conflicten? Probeer je deze te vermijden of los je ze graag op?",
|
| 65 |
+
"Hoe belangrijk is het voor je om anderen te helpen?",
|
| 66 |
+
"Hoe vaak ervaar je stress, en hoe reageer je daarop?",
|
| 67 |
+
"Hoe reageer je op kritiek? Wat voor gevoelens roept dit bij je op?",
|
| 68 |
+
"Hoe belangrijk is het voor jou om emoties uit te drukken? Hoe doe je dat?",
|
| 69 |
+
"Hoe ga je om met teleurstellingen of tegenslagen?",
|
| 70 |
+
"Hoe reageer je wanneer iemand je pijn doet of teleurstelt?",
|
| 71 |
+
"Vind je het makkelijk om fouten van anderen te vergeven? Waarom wel of niet?",
|
| 72 |
+
"Hoe besteed je het liefst je vrije tijd?",
|
| 73 |
+
"Wat zijn activiteiten die je echt vreugde geven?",
|
| 74 |
+
"Wat zijn de drie grootste doelen die je op dit moment hebt?",
|
| 75 |
+
"Wat zou je willen dat mensen zich over jou herinneren?",
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
# Define function to generate biography using the GROQ LLM model
|
| 79 |
+
def genereer_biografie(antwoorden):
|
| 80 |
+
prompt = "Hier zijn de antwoorden van de gebruiker:\n" + "\n".join([f"{vraag}: {antwoord}" for vraag, antwoord in antwoorden.items()])
|
| 81 |
+
prompt += "BELANGRIJK: Doe je in response geen aannamen over het geslacht van de persoon, houd de beschrijving in die zin algemeen !\n"
|
| 82 |
+
|
| 83 |
+
chat_completion = client.chat.completions.create(
|
| 84 |
+
messages=[{"role": "user", "content": prompt}],
|
| 85 |
+
model="llama-3.1-70b-versatile",
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return chat_completion.choices[0].message.content
|
| 89 |
+
|
| 90 |
+
# Define function to generate Big 5 profile using the GROQ LLM model
|
| 91 |
+
def genereer_big5_profiel(antwoorden):
|
| 92 |
+
prompt = (
|
| 93 |
+
"Geef je antwoorden uitsluitend en alleen in het Nederlands!\n"
|
| 94 |
+
"Dit is je opdracht:\n"
|
| 95 |
+
"You will analyze a provided text to identify the Big Five personality traits—Openness, Conscientiousness, Extraversion, Agreeableness, and Neuroticism (OCEAN model)—and produce a detailed psychological profile based on this analysis.\n"
|
| 96 |
+
"Output your analysis in nice markdown format, with each of the Big Five personality traits on a seperate alinea.\n"
|
| 97 |
+
"IMPORTANT: At the end of your report output a table in markdown format, showing all the Big 5 scores on a scale from 1 to 5 and the range of minimum to maximum scores because of the possible limited accuracy of the analysis. Use column headings: Big 5 eigenschap, Score (1-5), Range (minimale en maximale score)\n"
|
| 98 |
+
"Use the following naming conventions for the Big 5 personality traits and no others: Openheid, Nauwgezetheid, Extraversie, Vriendelijkheid, Neuroticisme"
|
| 99 |
+
"INPUT:\n" + "\n".join([f"{vraag}: {antwoord}" for vraag, antwoord in antwoorden.items()])
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
chat_completion = client.chat.completions.create(
|
| 103 |
+
messages=[{"role": "user", "content": prompt}],
|
| 104 |
+
model="llama-3.1-70b-versatile",
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
return chat_completion.choices[0].message.content
|
| 108 |
+
|
| 109 |
+
# Function to separate markdown table from descriptive text
|
| 110 |
+
def separate_text_and_table(big5_profiel):
|
| 111 |
+
text_part = []
|
| 112 |
+
table_part = ""
|
| 113 |
+
lines = big5_profiel.strip().split("\n")
|
| 114 |
+
|
| 115 |
+
for line in lines:
|
| 116 |
+
if "|" in line:
|
| 117 |
+
table_part += line + "\n"
|
| 118 |
+
else:
|
| 119 |
+
text_part.append(line)
|
| 120 |
+
|
| 121 |
+
return "\n".join(text_part), table_part
|
| 122 |
+
|
| 123 |
+
# Function to parse markdown table to ReportLab Table
|
| 124 |
+
def parse_markdown_table(markdown_table):
|
| 125 |
+
rows = []
|
| 126 |
+
lines = markdown_table.strip().split("\n")
|
| 127 |
+
for line in lines:
|
| 128 |
+
if "|" in line:
|
| 129 |
+
row = [cell.strip() for cell in line.split("|") if cell]
|
| 130 |
+
rows.append(row)
|
| 131 |
+
return rows
|
| 132 |
+
|
| 133 |
+
# Function to generate a PDF report in BytesIO
|
| 134 |
+
def generate_pdf_report(antwoorden, biografie, big5_profiel):
|
| 135 |
+
buffer = BytesIO()
|
| 136 |
+
doc = SimpleDocTemplate(buffer, pagesize=A4)
|
| 137 |
+
styles = getSampleStyleSheet()
|
| 138 |
+
|
| 139 |
+
elements = []
|
| 140 |
+
|
| 141 |
+
# Title
|
| 142 |
+
elements.append(Paragraph("Persoonlijkheidstest Resultaten", styles["Title"]))
|
| 143 |
+
|
| 144 |
+
# Add the questions and answers
|
| 145 |
+
elements.append(Paragraph("Antwoorden:", styles["Heading2"]))
|
| 146 |
+
for vraag, antwoord in antwoorden.items():
|
| 147 |
+
elements.append(Paragraph(f"{vraag}: {antwoord}", styles["Normal"]))
|
| 148 |
+
|
| 149 |
+
# Add the biography
|
| 150 |
+
elements.append(Paragraph("Biografie:", styles["Heading2"]))
|
| 151 |
+
elements.append(Paragraph(biografie, styles["Normal"]))
|
| 152 |
+
|
| 153 |
+
# Separate Big 5 Profile descriptive text and table
|
| 154 |
+
big5_text, big5_table_md = separate_text_and_table(big5_profiel)
|
| 155 |
+
|
| 156 |
+
# Add the descriptive text of Big 5 Profile
|
| 157 |
+
elements.append(Paragraph("Big 5 OCEAN Profiel:", styles["Heading2"]))
|
| 158 |
+
elements.append(Paragraph(big5_text, styles["Normal"]))
|
| 159 |
+
|
| 160 |
+
# Add the Big 5 Profile table
|
| 161 |
+
table_data = parse_markdown_table(big5_table_md)
|
| 162 |
+
big5_table = Table(table_data)
|
| 163 |
+
|
| 164 |
+
# Set table style for better readability
|
| 165 |
+
big5_table.setStyle(TableStyle([
|
| 166 |
+
#('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
|
| 167 |
+
#('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
| 168 |
+
#('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
| 169 |
+
#('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
| 170 |
+
#('FONTSIZE', (0, 0), (-1, -1), 10),
|
| 171 |
+
#('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
| 172 |
+
#('GRID', (0, 0), (-1, -1), 1, colors.black),
|
| 173 |
+
]))
|
| 174 |
+
|
| 175 |
+
elements.append(big5_table)
|
| 176 |
+
|
| 177 |
+
# Build PDF
|
| 178 |
+
doc.build(elements)
|
| 179 |
+
buffer.seek(0)
|
| 180 |
+
return buffer
|
| 181 |
+
|
| 182 |
+
# Main application
|
| 183 |
+
def main():
|
| 184 |
+
st.title("Persoonlijkheidstest")
|
| 185 |
+
|
| 186 |
+
# Collect user answers
|
| 187 |
+
antwoorden = {}
|
| 188 |
+
for vraag in vragenlijst:
|
| 189 |
+
antwoord = st.text_input(vraag)
|
| 190 |
+
if antwoord:
|
| 191 |
+
antwoorden[vraag] = antwoord
|
| 192 |
+
|
| 193 |
+
# Generate biography and Big 5 profile if all questions are answered
|
| 194 |
+
if len(antwoorden) == len(vragenlijst):
|
| 195 |
+
if st.button("Genereer Biografie en Big 5 Profiel"):
|
| 196 |
+
# Generate biography
|
| 197 |
+
biografie = genereer_biografie(antwoorden)
|
| 198 |
+
|
| 199 |
+
# Generate Big 5 profile
|
| 200 |
+
big5_profiel = genereer_big5_profiel(antwoorden)
|
| 201 |
+
|
| 202 |
+
# Display results
|
| 203 |
+
st.subheader("Biografie")
|
| 204 |
+
st.markdown(biografie)
|
| 205 |
+
|
| 206 |
+
st.subheader("Big 5 OCEAN Profiel")
|
| 207 |
+
st.markdown(big5_profiel)
|
| 208 |
+
|
| 209 |
+
# Generate PDF and provide a download button
|
| 210 |
+
pdf_buffer = generate_pdf_report(antwoorden, biografie, big5_profiel)
|
| 211 |
+
st.download_button(
|
| 212 |
+
label="Download PDF Rapport",
|
| 213 |
+
data=pdf_buffer,
|
| 214 |
+
file_name="Persoonlijkheidstest_Rapport.pdf",
|
| 215 |
+
mime="application/pdf"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
if __name__ == "__main__":
|
| 219 |
+
main()
|