Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
|
| 4 |
|
| 5 |
# Define the 44 BFI questions
|
| 6 |
questions = [
|
|
@@ -139,22 +139,23 @@ def compute_bfi_scores(*args):
|
|
| 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 |
-
|
| 143 |
|
| 144 |
# Prepare data for visualization
|
| 145 |
trait_names = []
|
| 146 |
trait_scores = []
|
| 147 |
for trait, score in scores.items():
|
| 148 |
-
|
| 149 |
if score == "Incomplete":
|
| 150 |
-
|
| 151 |
else:
|
| 152 |
-
|
| 153 |
-
|
| 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')
|
|
@@ -173,15 +174,15 @@ def compute_bfi_scores(*args):
|
|
| 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 |
-
|
| 182 |
-
output += f")})\n\n"
|
| 183 |
|
| 184 |
-
|
|
|
|
|
|
|
| 185 |
|
| 186 |
# Create the Gradio interface
|
| 187 |
def create_interface():
|
|
@@ -203,30 +204,35 @@ def create_interface():
|
|
| 203 |
'Openness': [5, 10, 15, 20, 25, 30, 35, 40, 41, 44]
|
| 204 |
}
|
| 205 |
|
| 206 |
-
|
| 207 |
|
| 208 |
with gr.Accordion("Answer the Questions", open=True):
|
| 209 |
for trait, q_indices in trait_question_map.items():
|
| 210 |
-
with gr.
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
dropdowns.append(radio)
|
| 221 |
|
| 222 |
# Submit button
|
| 223 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 224 |
|
| 225 |
# Results display
|
| 226 |
-
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# Link the button to the function
|
| 229 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
return demo
|
| 232 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
from io import BytesIO
|
| 4 |
|
| 5 |
# Define the 44 BFI questions
|
| 6 |
questions = [
|
|
|
|
| 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 |
+
markdown_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 |
+
markdown_output += f"### **{trait}**\n"
|
| 149 |
if score == "Incomplete":
|
| 150 |
+
markdown_output += "Insufficient responses to compute this trait.\n\n"
|
| 151 |
else:
|
| 152 |
+
markdown_output += f"**Score**: {score}\n\n"
|
| 153 |
+
markdown_output += f"{explanations[trait]}\n\n"
|
| 154 |
trait_names.append(trait)
|
| 155 |
trait_scores.append(score)
|
| 156 |
|
| 157 |
# Generate bar chart
|
| 158 |
+
image = None
|
| 159 |
if trait_scores:
|
| 160 |
fig, ax = plt.subplots(figsize=(8, 4))
|
| 161 |
bars = ax.bar(trait_names, trait_scores, color='skyblue')
|
|
|
|
| 174 |
|
| 175 |
plt.tight_layout()
|
| 176 |
# Save the plot to a PNG image in memory
|
|
|
|
| 177 |
buf = BytesIO()
|
| 178 |
plt.savefig(buf, format='png')
|
| 179 |
buf.seek(0)
|
| 180 |
plt.close(fig)
|
| 181 |
+
image = buf.getvalue()
|
|
|
|
| 182 |
|
| 183 |
+
markdown_output += "### **Trait Scores Visualization**\n\n"
|
| 184 |
+
|
| 185 |
+
return markdown_output, image
|
| 186 |
|
| 187 |
# Create the Gradio interface
|
| 188 |
def create_interface():
|
|
|
|
| 204 |
'Openness': [5, 10, 15, 20, 25, 30, 35, 40, 41, 44]
|
| 205 |
}
|
| 206 |
|
| 207 |
+
inputs = []
|
| 208 |
|
| 209 |
with gr.Accordion("Answer the Questions", open=True):
|
| 210 |
for trait, q_indices in trait_question_map.items():
|
| 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 |
+
inputs.append(radio)
|
|
|
|
| 221 |
|
| 222 |
# Submit button
|
| 223 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 224 |
|
| 225 |
# Results display
|
| 226 |
+
with gr.Row():
|
| 227 |
+
markdown_result = gr.Markdown(label="Textual Results")
|
| 228 |
+
image_result = gr.Image(label="Trait Scores Visualization")
|
| 229 |
|
| 230 |
# Link the button to the function
|
| 231 |
+
submit_btn.click(
|
| 232 |
+
fn=compute_bfi_scores,
|
| 233 |
+
inputs=inputs,
|
| 234 |
+
outputs=[markdown_result, image_result]
|
| 235 |
+
)
|
| 236 |
|
| 237 |
return demo
|
| 238 |
|