shukdevdatta123 commited on
Commit
89a6749
·
verified ·
1 Parent(s): 0d83e25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -11
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import streamlit as st
2
  import openai
3
  import json
4
- import os
5
- from dotenv import load_dotenv
6
 
7
  # Load the OpenAI API Key
8
  api_key = st.text_input('Enter your OpenAI API Key', type="password")
@@ -78,7 +77,6 @@ def save_responses_to_json(username, responses):
78
  "responses": [{"text": question["text"], "answer": response} for question, response in zip(questions, responses)]
79
  }
80
 
81
- # Save to UserChoices.json
82
  with open("UserChoices.json", "w") as json_file:
83
  json.dump(user_data, json_file, indent=4)
84
 
@@ -90,10 +88,34 @@ def save_personality_to_output_json(username, mbti_type_classic, mbti_type_llm):
90
  "mbti_type_llm": mbti_type_llm
91
  }
92
 
93
- # Save to Output.json
94
  with open("Output.json", "w") as json_file:
95
  json.dump(output_data, json_file, indent=4)
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  # Streamlit component to display the quiz and handle responses
98
  def show_mbti_quiz():
99
  st.title('FlexTemp Personality Test')
@@ -120,9 +142,9 @@ def show_mbti_quiz():
120
  mbti_type_classic = classic_mbti_weighted(responses)
121
  st.write(f"Your MBTI type based on weighted answers: {mbti_type_classic}")
122
 
123
- # You can add LLM-based prediction if needed here (example OpenAI-based model)
 
124
  if api_key:
125
- # Run the LLM (GPT-4, for example) model to generate a personality type.
126
  prompt = f"""
127
  Determine a person's personality type based on their answers to the following Myers-Briggs Type Indicator (MBTI) questions:
128
  The person has answered the following questions:
@@ -131,7 +153,7 @@ def show_mbti_quiz():
131
  """
132
  try:
133
  response = openai.ChatCompletion.create(
134
- model="gpt-4o",
135
  messages=[{"role": "system", "content": "You are a helpful assistant."},
136
  {"role": "user", "content": prompt}]
137
  )
@@ -144,6 +166,17 @@ def show_mbti_quiz():
144
  save_responses_to_json(participant_name, responses)
145
  save_personality_to_output_json(participant_name, mbti_type_classic, mbti_type_llm)
146
 
 
 
 
 
 
 
 
 
 
 
 
147
  with open("Output.json", "r") as json_file:
148
  json_data = json_file.read()
149
 
@@ -171,8 +204,7 @@ def show_mbti_quiz():
171
  def main():
172
  # Add instructions to the sidebar
173
  with st.sidebar.expander("How This App Works", expanded=False):
174
- st.write("""
175
- ### FlexTemp Personality Test
176
  This app is designed to help you determine your MBTI personality type based on your answers to a series of questions. The process works as follows:
177
  1. **Weighted MBTI Scoring**:
178
  - Each question corresponds to a trait in the MBTI system.
@@ -182,11 +214,11 @@ def main():
182
  - Optionally, you can also get your MBTI type based on the answers using a language model (LLM) like GPT-4. This provides an additional prediction that may offer insights into your personality.
183
  - The LLM is trained on vast amounts of data and can generate responses based on patterns from psychological research and real-world interactions.
184
  """)
185
-
186
  if api_key:
187
  show_mbti_quiz()
188
  else:
189
  st.info("Please enter your OpenAI API Key to begin the quiz.")
190
 
191
  if __name__ == "__main__":
192
- main()
 
1
  import streamlit as st
2
  import openai
3
  import json
4
+ from fpdf import FPDF
 
5
 
6
  # Load the OpenAI API Key
7
  api_key = st.text_input('Enter your OpenAI API Key', type="password")
 
77
  "responses": [{"text": question["text"], "answer": response} for question, response in zip(questions, responses)]
78
  }
79
 
 
80
  with open("UserChoices.json", "w") as json_file:
81
  json.dump(user_data, json_file, indent=4)
82
 
 
88
  "mbti_type_llm": mbti_type_llm
89
  }
90
 
 
91
  with open("Output.json", "w") as json_file:
92
  json.dump(output_data, json_file, indent=4)
93
 
94
+ # Function to generate PDF report
95
+ def generate_pdf_report(username, mbti_type_classic, mbti_type_llm):
96
+ pdf = FPDF()
97
+ pdf.set_auto_page_break(auto=True, margin=15)
98
+ pdf.add_page()
99
+
100
+ # Set title
101
+ pdf.set_font('Arial', 'B', 16)
102
+ pdf.cell(200, 10, txt="MBTI Personality Report", ln=True, align='C')
103
+
104
+ # Add participant information
105
+ pdf.ln(10)
106
+ pdf.set_font('Arial', '', 12)
107
+ pdf.cell(200, 10, txt=f"Participant Name: {username}", ln=True)
108
+
109
+ # Add MBTI types
110
+ pdf.ln(10)
111
+ pdf.cell(200, 10, txt=f"Your MBTI type based on weighted answers: {mbti_type_classic}", ln=True)
112
+ pdf.cell(200, 10, txt=f"Your MBTI type according to AI: {mbti_type_llm}", ln=True)
113
+
114
+ # Output the PDF
115
+ pdf_output = "MBTI_Personality_Report.pdf"
116
+ pdf.output(pdf_output)
117
+ return pdf_output
118
+
119
  # Streamlit component to display the quiz and handle responses
120
  def show_mbti_quiz():
121
  st.title('FlexTemp Personality Test')
 
142
  mbti_type_classic = classic_mbti_weighted(responses)
143
  st.write(f"Your MBTI type based on weighted answers: {mbti_type_classic}")
144
 
145
+ # LLM-based prediction
146
+ mbti_type_llm = ""
147
  if api_key:
 
148
  prompt = f"""
149
  Determine a person's personality type based on their answers to the following Myers-Briggs Type Indicator (MBTI) questions:
150
  The person has answered the following questions:
 
153
  """
154
  try:
155
  response = openai.ChatCompletion.create(
156
+ model="gpt-4",
157
  messages=[{"role": "system", "content": "You are a helpful assistant."},
158
  {"role": "user", "content": prompt}]
159
  )
 
166
  save_responses_to_json(participant_name, responses)
167
  save_personality_to_output_json(participant_name, mbti_type_classic, mbti_type_llm)
168
 
169
+ # Generate PDF report and offer it as a download
170
+ pdf_report = generate_pdf_report(participant_name, mbti_type_classic, mbti_type_llm)
171
+ with open(pdf_report, "rb") as file:
172
+ st.download_button(
173
+ label="Download Generated Report (PDF)",
174
+ data=file,
175
+ file_name="MBTI_Personality_Report.pdf",
176
+ mime="application/pdf"
177
+ )
178
+
179
+ # Provide the other download buttons
180
  with open("Output.json", "r") as json_file:
181
  json_data = json_file.read()
182
 
 
204
  def main():
205
  # Add instructions to the sidebar
206
  with st.sidebar.expander("How This App Works", expanded=False):
207
+ st.write("""### FlexTemp Personality Test
 
208
  This app is designed to help you determine your MBTI personality type based on your answers to a series of questions. The process works as follows:
209
  1. **Weighted MBTI Scoring**:
210
  - Each question corresponds to a trait in the MBTI system.
 
214
  - Optionally, you can also get your MBTI type based on the answers using a language model (LLM) like GPT-4. This provides an additional prediction that may offer insights into your personality.
215
  - The LLM is trained on vast amounts of data and can generate responses based on patterns from psychological research and real-world interactions.
216
  """)
217
+
218
  if api_key:
219
  show_mbti_quiz()
220
  else:
221
  st.info("Please enter your OpenAI API Key to begin the quiz.")
222
 
223
  if __name__ == "__main__":
224
+ main()