Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load the OpenAI API Key
|
7 |
+
api_key = st.text_input('Enter your OpenAI API Key', type="password")
|
8 |
+
|
9 |
+
# Set the OpenAI API key
|
10 |
+
if api_key:
|
11 |
+
openai.api_key = api_key
|
12 |
+
|
13 |
+
# English-translated version of the questions (MBTI-related questions)
|
14 |
+
questions = [
|
15 |
+
{"text": "Do you enjoy being spontaneous and keeping your options open?", "trait": "P"},
|
16 |
+
{"text": "Do you prefer spending weekends quietly at home rather than going out?", "trait": "I"},
|
17 |
+
{"text": "Do you feel more energized when you are around people?", "trait": "E"},
|
18 |
+
{"text": "Do you easily set and meet deadlines?", "trait": "J"},
|
19 |
+
{"text": "Are your decisions often influenced by how they will affect others emotionally?", "trait": "F"},
|
20 |
+
{"text": "Do you like discussing symbolic or metaphorical interpretations of a story?", "trait": "N"},
|
21 |
+
{"text": "Do you strive to maintain harmony in group settings, even if it means compromising?", "trait": "F"},
|
22 |
+
{"text": "When a friend is upset, is your first instinct to offer emotional support rather than solutions?", "trait": "F"},
|
23 |
+
{"text": "In arguments, do you focus more on being rational than on people's feelings?", "trait": "T"},
|
24 |
+
{"text": "When you learn something new, do you prefer hands-on experience over theory?", "trait": "S"},
|
25 |
+
{"text": "Do you often think about how today's actions will affect the future?", "trait": "N"},
|
26 |
+
{"text": "Are you comfortable adapting to new situations as they happen?", "trait": "P"},
|
27 |
+
{"text": "Do you prefer exploring different options before making a decision?", "trait": "P"},
|
28 |
+
{"text": "At parties, do you start conversations with new people?", "trait": "E"},
|
29 |
+
{"text": "When faced with a problem, do you prefer discussing it with others?", "trait": "E"},
|
30 |
+
{"text": "When making decisions, do you prioritize logic over personal considerations?", "trait": "T"},
|
31 |
+
{"text": "Do you find solitude more refreshing than social gatherings?", "trait": "I"},
|
32 |
+
{"text": "Do you prefer having a clear plan and dislike unexpected changes?", "trait": "J"},
|
33 |
+
{"text": "Do you find satisfaction in finishing tasks and making final decisions?", "trait": "J"},
|
34 |
+
{"text": "Do you tend to process your thoughts internally before speaking?", "trait": "I"},
|
35 |
+
{"text": "Are you more interested in exploring abstract theories and future possibilities?", "trait": "N"},
|
36 |
+
{"text": "When planning a vacation, do you prefer to have a detailed plan?", "trait": "S"},
|
37 |
+
{"text": "Do you often rely on objective criteria to assess situations?", "trait": "T"},
|
38 |
+
{"text": "Do you focus more on details and facts in your surroundings?", "trait": "S"}
|
39 |
+
]
|
40 |
+
|
41 |
+
# Function to calculate MBTI scores based on responses
|
42 |
+
def calculate_weighted_mbti_scores(responses):
|
43 |
+
weights = {
|
44 |
+
"Strongly Agree": 2,
|
45 |
+
"Agree": 1,
|
46 |
+
"Neutral": 0,
|
47 |
+
"Disagree": -1,
|
48 |
+
"Strongly Disagree": -2
|
49 |
+
}
|
50 |
+
|
51 |
+
scores = {'E': 0, 'I': 0, 'S': 0, 'N': 0, 'T': 0, 'F': 0, 'J': 0, 'P': 0}
|
52 |
+
|
53 |
+
for i, response in enumerate(responses):
|
54 |
+
weight = weights.get(response, 0)
|
55 |
+
trait = questions[i]["trait"]
|
56 |
+
if trait in scores:
|
57 |
+
scores[trait] += weight
|
58 |
+
|
59 |
+
return scores
|
60 |
+
|
61 |
+
# Function to determine MBTI type based on weighted scores
|
62 |
+
def classic_mbti_weighted(responses):
|
63 |
+
scores = calculate_weighted_mbti_scores(responses)
|
64 |
+
mbti_type = ""
|
65 |
+
for trait_pair in ['EI', 'SN', 'TF', 'JP']:
|
66 |
+
trait1, trait2 = trait_pair
|
67 |
+
if scores[trait1] >= scores[trait2]:
|
68 |
+
mbti_type += trait1
|
69 |
+
else:
|
70 |
+
mbti_type += trait2
|
71 |
+
return mbti_type
|
72 |
+
|
73 |
+
# Streamlit component to display the quiz and handle responses
|
74 |
+
def show_mbti_quiz():
|
75 |
+
st.title('FlexTemp Personality Test')
|
76 |
+
|
77 |
+
# Step 1: Input name
|
78 |
+
participant_name = st.text_input("Enter your name")
|
79 |
+
|
80 |
+
if participant_name:
|
81 |
+
responses = []
|
82 |
+
st.subheader(f"Hello {participant_name}, let's start the quiz!")
|
83 |
+
|
84 |
+
for i, question in enumerate(questions):
|
85 |
+
response = st.radio(
|
86 |
+
question["text"],
|
87 |
+
["Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree"]
|
88 |
+
)
|
89 |
+
if response:
|
90 |
+
responses.append(response)
|
91 |
+
|
92 |
+
if len(responses) == len(questions):
|
93 |
+
st.subheader("Your MBTI Personality Type:")
|
94 |
+
mbti_type_classic = classic_mbti_weighted(responses)
|
95 |
+
st.write(f"Your MBTI type based on weighted answers: {mbti_type_classic}")
|
96 |
+
|
97 |
+
# You can add LLM-based prediction if needed here (example OpenAI-based model)
|
98 |
+
if api_key:
|
99 |
+
# Run the LLM (GPT-4, for example) model to generate a personality type.
|
100 |
+
prompt = f"""
|
101 |
+
Determine a person's personality type based on their answers to the following Myers-Briggs Type Indicator (MBTI) questions:
|
102 |
+
The person has answered the following questions:
|
103 |
+
|
104 |
+
{', '.join([f"{question['text']} {response}" for question, response in zip(questions, responses)])}
|
105 |
+
|
106 |
+
What is the MBTI personality type based on these answers?
|
107 |
+
"""
|
108 |
+
try:
|
109 |
+
response = openai.ChatCompletion.create(
|
110 |
+
model="gpt-4o",
|
111 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
112 |
+
{"role": "user", "content": prompt}]
|
113 |
+
)
|
114 |
+
mbti_type_llm = response['choices'][0]['message']['content']
|
115 |
+
st.write(f"Your MBTI type according to AI: {mbti_type_llm}")
|
116 |
+
except Exception as e:
|
117 |
+
st.error(f"Error occurred: {e}")
|
118 |
+
|
119 |
+
else:
|
120 |
+
st.warning("Please answer all the questions!")
|
121 |
+
|
122 |
+
# Main function to display the app
|
123 |
+
def main():
|
124 |
+
if api_key:
|
125 |
+
show_mbti_quiz()
|
126 |
+
else:
|
127 |
+
st.info("Please enter your OpenAI API Key to begin the quiz.")
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
main()
|