Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ 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")
|
@@ -70,6 +72,25 @@ def classic_mbti_weighted(responses):
|
|
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')
|
@@ -94,11 +115,16 @@ def show_mbti_quiz():
|
|
94 |
if st.button("Generate Personality Trait Information"):
|
95 |
st.subheader("Your MBTI Personality Type:")
|
96 |
mbti_type_classic = classic_mbti_weighted(responses)
|
97 |
-
|
|
|
|
|
|
|
98 |
|
99 |
-
#
|
|
|
|
|
|
|
100 |
if api_key:
|
101 |
-
# Run the LLM (GPT-4, for example) model to generate a personality type.
|
102 |
prompt = f"""
|
103 |
Determine a person's personality type based on their answers to the following Myers-Briggs Type Indicator (MBTI) questions:
|
104 |
The person has answered the following questions:
|
@@ -107,14 +133,33 @@ def show_mbti_quiz():
|
|
107 |
"""
|
108 |
try:
|
109 |
response = openai.ChatCompletion.create(
|
110 |
-
model="gpt-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
except Exception as e:
|
117 |
-
st.error(f"Error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
else:
|
119 |
st.warning("Please answer all the questions!")
|
120 |
|
|
|
2 |
import openai
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import io
|
7 |
|
8 |
# Load the OpenAI API Key
|
9 |
api_key = st.text_input('Enter your OpenAI API Key', type="password")
|
|
|
72 |
mbti_type += trait2
|
73 |
return mbti_type
|
74 |
|
75 |
+
# Function to create an image from text
|
76 |
+
def create_image_from_text(text):
|
77 |
+
# Create an empty image with white background
|
78 |
+
img = Image.new('RGB', (600, 400), color='white')
|
79 |
+
draw = ImageDraw.Draw(img)
|
80 |
+
|
81 |
+
# Load font (You can customize font and size)
|
82 |
+
font = ImageFont.load_default()
|
83 |
+
|
84 |
+
# Draw the text on the image
|
85 |
+
draw.text((10, 10), text, fill='black', font=font)
|
86 |
+
|
87 |
+
# Save to a buffer
|
88 |
+
buf = io.BytesIO()
|
89 |
+
img.save(buf, format="PNG")
|
90 |
+
buf.seek(0)
|
91 |
+
|
92 |
+
return buf
|
93 |
+
|
94 |
# Streamlit component to display the quiz and handle responses
|
95 |
def show_mbti_quiz():
|
96 |
st.title('FlexTemp Personality Test')
|
|
|
115 |
if st.button("Generate Personality Trait Information"):
|
116 |
st.subheader("Your MBTI Personality Type:")
|
117 |
mbti_type_classic = classic_mbti_weighted(responses)
|
118 |
+
result_text = f"Your MBTI type based on weighted answers: {mbti_type_classic}"
|
119 |
+
|
120 |
+
# Display result text
|
121 |
+
st.write(result_text)
|
122 |
|
123 |
+
# Create an image of the result
|
124 |
+
image_buf = create_image_from_text(result_text)
|
125 |
+
|
126 |
+
# LLM-based prediction
|
127 |
if api_key:
|
|
|
128 |
prompt = f"""
|
129 |
Determine a person's personality type based on their answers to the following Myers-Briggs Type Indicator (MBTI) questions:
|
130 |
The person has answered the following questions:
|
|
|
133 |
"""
|
134 |
try:
|
135 |
response = openai.ChatCompletion.create(
|
136 |
+
model="gpt-4",
|
137 |
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
138 |
{"role": "user", "content": prompt}]
|
139 |
)
|
140 |
mbti_type_llm = response['choices'][0]['message']['content']
|
141 |
+
llm_result_text = f"Your MBTI type according to AI: {mbti_type_llm}"
|
142 |
+
|
143 |
+
# Create an image of the LLM result
|
144 |
+
image_buf_llm = create_image_from_text(llm_result_text)
|
145 |
+
|
146 |
+
# Display the generated LLM image
|
147 |
+
st.image(image_buf_llm)
|
148 |
except Exception as e:
|
149 |
+
st.error(f"Error occurred while using the AI model: {e}")
|
150 |
+
|
151 |
+
# Save both the weighted result and LLM-based result as images
|
152 |
+
combined_text = result_text + "\n\n" + (llm_result_text if 'llm_result_text' in locals() else '')
|
153 |
+
combined_image_buf = create_image_from_text(combined_text)
|
154 |
+
|
155 |
+
# Display the combined result image
|
156 |
+
st.image(combined_image_buf)
|
157 |
+
|
158 |
+
# Optionally, save the image to disk
|
159 |
+
with open("personality_combined_result.png", "wb") as f:
|
160 |
+
f.write(combined_image_buf.read())
|
161 |
+
st.success("Your result image has been saved as 'personality_combined_result.png'.")
|
162 |
+
|
163 |
else:
|
164 |
st.warning("Please answer all the questions!")
|
165 |
|