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,27 +115,22 @@ 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 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
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 |
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 |
+
# Display the generated image in Streamlit
|
127 |
+
st.image(image_buf)
|
128 |
+
|
129 |
+
# Optionally, save the image to disk
|
130 |
+
with open("personality_type_image.png", "wb") as f:
|
131 |
+
f.write(image_buf.read())
|
132 |
+
st.success("Your result image has been saved as 'personality_type_image.png'.")
|
133 |
+
|
|
|
|
|
|
|
|
|
|
|
134 |
else:
|
135 |
st.warning("Please answer all the questions!")
|
136 |
|