|
import streamlit as st |
|
import openai |
|
from dotenv import load_dotenv |
|
import os |
|
from PIL import Image, ImageDraw, ImageFont |
|
import io |
|
|
|
|
|
api_key = st.text_input('Enter your OpenAI API Key', type="password") |
|
|
|
|
|
if api_key: |
|
openai.api_key = api_key |
|
|
|
|
|
questions = [ |
|
{"text": "Do you enjoy being spontaneous and keeping your options open?", "trait": "P"}, |
|
{"text": "Do you prefer spending weekends quietly at home rather than going out?", "trait": "I"}, |
|
{"text": "Do you feel more energized when you are around people?", "trait": "E"}, |
|
{"text": "Do you easily set and meet deadlines?", "trait": "J"}, |
|
{"text": "Are your decisions often influenced by how they will affect others emotionally?", "trait": "F"}, |
|
{"text": "Do you like discussing symbolic or metaphorical interpretations of a story?", "trait": "N"}, |
|
{"text": "Do you strive to maintain harmony in group settings, even if it means compromising?", "trait": "F"}, |
|
{"text": "When a friend is upset, is your first instinct to offer emotional support rather than solutions?", "trait": "F"}, |
|
{"text": "In arguments, do you focus more on being rational than on people's feelings?", "trait": "T"}, |
|
{"text": "When you learn something new, do you prefer hands-on experience over theory?", "trait": "S"}, |
|
{"text": "Do you often think about how today's actions will affect the future?", "trait": "N"}, |
|
{"text": "Are you comfortable adapting to new situations as they happen?", "trait": "P"}, |
|
{"text": "Do you prefer exploring different options before making a decision?", "trait": "P"}, |
|
{"text": "At parties, do you start conversations with new people?", "trait": "E"}, |
|
{"text": "When faced with a problem, do you prefer discussing it with others?", "trait": "E"}, |
|
{"text": "When making decisions, do you prioritize logic over personal considerations?", "trait": "T"}, |
|
{"text": "Do you find solitude more refreshing than social gatherings?", "trait": "I"}, |
|
{"text": "Do you prefer having a clear plan and dislike unexpected changes?", "trait": "J"}, |
|
{"text": "Do you find satisfaction in finishing tasks and making final decisions?", "trait": "J"}, |
|
{"text": "Do you tend to process your thoughts internally before speaking?", "trait": "I"}, |
|
{"text": "Are you more interested in exploring abstract theories and future possibilities?", "trait": "N"}, |
|
{"text": "When planning a vacation, do you prefer to have a detailed plan?", "trait": "S"}, |
|
{"text": "Do you often rely on objective criteria to assess situations?", "trait": "T"}, |
|
{"text": "Do you focus more on details and facts in your surroundings?", "trait": "S"} |
|
] |
|
|
|
|
|
def calculate_weighted_mbti_scores(responses): |
|
weights = { |
|
"Strongly Agree": 2, |
|
"Agree": 1, |
|
"Neutral": 0, |
|
"Disagree": -1, |
|
"Strongly Disagree": -2 |
|
} |
|
|
|
scores = {'E': 0, 'I': 0, 'S': 0, 'N': 0, 'T': 0, 'F': 0, 'J': 0, 'P': 0} |
|
|
|
for i, response in enumerate(responses): |
|
weight = weights.get(response, 0) |
|
trait = questions[i]["trait"] |
|
if trait in scores: |
|
scores[trait] += weight |
|
|
|
return scores |
|
|
|
|
|
def classic_mbti_weighted(responses): |
|
scores = calculate_weighted_mbti_scores(responses) |
|
mbti_type = "" |
|
for trait_pair in ['EI', 'SN', 'TF', 'JP']: |
|
trait1, trait2 = trait_pair |
|
if scores[trait1] >= scores[trait2]: |
|
mbti_type += trait1 |
|
else: |
|
mbti_type += trait2 |
|
return mbti_type |
|
|
|
|
|
def create_image_from_text(text): |
|
|
|
img = Image.new('RGB', (600, 400), color='white') |
|
draw = ImageDraw.Draw(img) |
|
|
|
|
|
font = ImageFont.load_default() |
|
|
|
|
|
draw.text((10, 10), text, fill='black', font=font) |
|
|
|
|
|
buf = io.BytesIO() |
|
img.save(buf, format="PNG") |
|
buf.seek(0) |
|
|
|
return buf |
|
|
|
|
|
def show_mbti_quiz(): |
|
st.title('FlexTemp Personality Test') |
|
|
|
|
|
participant_name = st.text_input("Enter your name") |
|
|
|
if participant_name: |
|
responses = [] |
|
st.subheader(f"Hello {participant_name}, let's start the quiz!") |
|
|
|
for i, question in enumerate(questions): |
|
response = st.radio( |
|
question["text"], |
|
["Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree"] |
|
) |
|
if response: |
|
responses.append(response) |
|
|
|
if len(responses) == len(questions): |
|
|
|
if st.button("Generate Personality Trait Information"): |
|
st.subheader("Your MBTI Personality Type:") |
|
mbti_type_classic = classic_mbti_weighted(responses) |
|
result_text = f"Your MBTI type based on weighted answers: {mbti_type_classic}" |
|
|
|
|
|
st.write(result_text) |
|
|
|
|
|
image_buf = create_image_from_text(result_text) |
|
|
|
|
|
st.image(image_buf) |
|
|
|
|
|
with open("personality_type_image.png", "wb") as f: |
|
f.write(image_buf.read()) |
|
st.success("Your result image has been saved as 'personality_type_image.png'.") |
|
|
|
else: |
|
st.warning("Please answer all the questions!") |
|
|
|
|
|
def main(): |
|
if api_key: |
|
show_mbti_quiz() |
|
else: |
|
st.info("Please enter your OpenAI API Key to begin the quiz.") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|