shukdevdatta123 commited on
Commit
0ac7181
·
verified ·
1 Parent(s): f7d229c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -52
app.py CHANGED
@@ -2,8 +2,6 @@ import streamlit as st
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,25 +70,6 @@ def classic_mbti_weighted(responses):
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,16 +94,11 @@ def show_mbti_quiz():
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,33 +107,14 @@ def show_mbti_quiz():
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
 
@@ -171,4 +126,4 @@ def main():
171
  st.info("Please enter your OpenAI API Key to begin the quiz.")
172
 
173
  if __name__ == "__main__":
174
- main()
 
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
  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
  if st.button("Generate Personality Trait Information"):
95
  st.subheader("Your MBTI Personality Type:")
96
  mbti_type_classic = classic_mbti_weighted(responses)
97
+ st.write(f"Your MBTI type based on weighted answers: {mbti_type_classic}")
 
 
 
98
 
99
+ # You can add LLM-based prediction if needed here (example OpenAI-based model)
 
 
 
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
  """
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
  else:
119
  st.warning("Please answer all the questions!")
120
 
 
126
  st.info("Please enter your OpenAI API Key to begin the quiz.")
127
 
128
  if __name__ == "__main__":
129
+ main()