johnpaulbin commited on
Commit
97181a1
·
verified ·
1 Parent(s): aace1e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -19
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
 
3
  # Define the 44 BFI questions
4
  questions = [
@@ -128,36 +130,101 @@ def compute_bfi_scores(*args):
128
  score = round(score, 2)
129
  scores[trait] = score
130
 
131
- # Prepare the output in Markdown format
 
 
 
 
 
 
 
 
132
  output = "## Your Big Five Personality Traits Scores\n\n"
 
 
 
 
133
  for trait, score in scores.items():
134
- output += f"**{trait}**: {score}\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  return output
137
 
138
  # Create the Gradio interface
139
  def create_interface():
140
  with gr.Blocks() as demo:
141
- gr.Markdown("## Big Five Inventory (BFI) Quiz")
142
- gr.Markdown("Please rate the following statements on a scale from **1 (Disagree a lot)** to **5 (Agree a lot)**. If you prefer not to respond to a particular statement, select **'No response'**.")
143
-
144
- # Create a grid layout for better organization
145
- with gr.Column():
146
- dropdowns = []
147
- for q in questions:
148
- dropdown = gr.Dropdown(
149
- choices=["No response", 1, 2, 3, 4, 5],
150
- label=q,
151
- value="No response"
152
- )
153
- dropdowns.append(dropdown)
154
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  # Submit button
156
- submit_btn = gr.Button("Submit")
157
-
158
  # Results display
159
  result = gr.Markdown()
160
-
161
  # Link the button to the function
162
  submit_btn.click(fn=compute_bfi_scores, inputs=dropdowns, outputs=result)
163
 
 
1
  import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
 
5
  # Define the 44 BFI questions
6
  questions = [
 
130
  score = round(score, 2)
131
  scores[trait] = score
132
 
133
+ # Prepare the output in Markdown format with explanations
134
+ explanations = {
135
+ 'Extraversion': "Extraversion reflects how outgoing and energetic you are. High scores indicate sociability and enthusiasm, while low scores suggest a more reserved and solitary nature.",
136
+ 'Agreeableness': "Agreeableness measures your tendency to be compassionate and cooperative. High scores signify kindness and trust, whereas low scores may indicate competitiveness or skepticism.",
137
+ 'Conscientiousness': "Conscientiousness assesses your level of self-discipline and organization. High scores denote reliability and thoroughness, while low scores might reflect a more spontaneous or disorganized approach.",
138
+ 'Neuroticism': "Neuroticism indicates emotional stability and susceptibility to stress. High scores suggest a tendency towards anxiety and moodiness, whereas low scores imply calmness and resilience.",
139
+ 'Openness': "Openness measures your openness to new experiences and creativity. High scores are associated with imagination and curiosity, while low scores may indicate practicality and preference for routine."
140
+ }
141
+
142
  output = "## Your Big Five Personality Traits Scores\n\n"
143
+
144
+ # Prepare data for visualization
145
+ trait_names = []
146
+ trait_scores = []
147
  for trait, score in scores.items():
148
+ output += f"### **{trait}**\n"
149
+ if score == "Incomplete":
150
+ output += "Insufficient responses to compute this trait.\n\n"
151
+ else:
152
+ output += f"**Score**: {score}\n\n"
153
+ output += f"{explanations[trait]}\n\n"
154
+ trait_names.append(trait)
155
+ trait_scores.append(score)
156
+
157
+ # Generate bar chart
158
+ if trait_scores:
159
+ fig, ax = plt.subplots(figsize=(8, 4))
160
+ bars = ax.bar(trait_names, trait_scores, color='skyblue')
161
+ ax.set_ylim(0, max(trait_scores) + 10)
162
+ ax.set_ylabel('Score')
163
+ ax.set_title('Big Five Traits Scores')
164
+
165
+ # Add score labels on top of bars
166
+ for bar in bars:
167
+ height = bar.get_height()
168
+ ax.annotate(f'{height}',
169
+ xy=(bar.get_x() + bar.get_width() / 2, height),
170
+ xytext=(0, 3), # 3 points vertical offset
171
+ textcoords="offset points",
172
+ ha='center', va='bottom')
173
+
174
+ plt.tight_layout()
175
+ # Save the plot to a PNG image in memory
176
+ from io import BytesIO
177
+ buf = BytesIO()
178
+ plt.savefig(buf, format='png')
179
+ buf.seek(0)
180
+ plt.close(fig)
181
+ output += "### **Trait Scores Visualization**\n\n"
182
+ output += f"![Trait Scores]({gr.Image.update(value=buf.getvalue())})\n\n"
183
 
184
  return output
185
 
186
  # Create the Gradio interface
187
  def create_interface():
188
  with gr.Blocks() as demo:
189
+ gr.Markdown("# Big Five Inventory (BFI) Quiz")
190
+ gr.Markdown(
191
+ """
192
+ Please rate the following statements on a scale from **1 (Disagree a lot)** to **5 (Agree a lot)**.
193
+ If you prefer not to respond to a particular statement, select **'No response'**.
194
+ """
195
+ )
196
+
197
+ # Organize questions into expandable sections by trait
198
+ trait_question_map = {
199
+ 'Extraversion': [1, 6, 11, 16, 21, 26, 31, 36],
200
+ 'Agreeableness': [2, 7, 12, 17, 22, 27, 32, 37, 42],
201
+ 'Conscientiousness': [3, 8, 13, 18, 23, 28, 33, 38, 43],
202
+ 'Neuroticism': [4, 9, 14, 19, 24, 29, 34, 39],
203
+ 'Openness': [5, 10, 15, 20, 25, 30, 35, 40, 41, 44]
204
+ }
205
+
206
+ dropdowns = []
207
+
208
+ with gr.Accordion("Answer the Questions", open=True):
209
+ for trait, q_indices in trait_question_map.items():
210
+ with gr.Group():
211
+ with gr.Accordion(trait, open=False):
212
+ for i in q_indices:
213
+ q = questions[i-1]
214
+ radio = gr.Radio(
215
+ choices=["No response", 1, 2, 3, 4, 5],
216
+ label=q,
217
+ value="No response",
218
+ interactive=True
219
+ )
220
+ dropdowns.append(radio)
221
+
222
  # Submit button
223
+ submit_btn = gr.Button("Submit", variant="primary")
224
+
225
  # Results display
226
  result = gr.Markdown()
227
+
228
  # Link the button to the function
229
  submit_btn.click(fn=compute_bfi_scores, inputs=dropdowns, outputs=result)
230