Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,170 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
# Define the
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
]
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
}
|
73 |
-
}
|
74 |
|
75 |
-
|
76 |
-
def compute_trait_scores(*args):
|
77 |
-
responses = {}
|
78 |
-
for i, item in enumerate(items):
|
79 |
-
BFI = item['BFI']
|
80 |
-
response = int(args[i])
|
81 |
-
responses[BFI] = response
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
negative_items = traits[trait]['negative']
|
87 |
|
88 |
-
|
89 |
-
|
|
|
90 |
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
# Create the Gradio interface
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
fn=compute_trait_scores,
|
108 |
-
inputs=inputs,
|
109 |
-
outputs=gr.outputs.JSON(label="Your Big Five Personality Scores"),
|
110 |
-
title='Big Five Personality Test',
|
111 |
-
description='Answer the following questions to see your Big Five personality trait scores.',
|
112 |
-
allow_flagging='never'
|
113 |
-
)
|
114 |
|
115 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# Define the 44 BFI questions
|
4 |
+
questions = [
|
5 |
+
"1. Talks a lot",
|
6 |
+
"2. Notices other people’s weak points",
|
7 |
+
"3. Does things carefully and completely",
|
8 |
+
"4. Is sad, depressed",
|
9 |
+
"5. Is original, comes up with new ideas",
|
10 |
+
"6. Keeps their thoughts to themselves",
|
11 |
+
"7. Is helpful and not selfish with others",
|
12 |
+
"8. Can be kind of careless",
|
13 |
+
"9. Is relaxed, handles stress well",
|
14 |
+
"10. Is curious about lots of different things",
|
15 |
+
"11. Has a lot of energy",
|
16 |
+
"12. Starts arguments with others",
|
17 |
+
"13. Is a good, hard worker",
|
18 |
+
"14. Can be tense; not always easy going",
|
19 |
+
"15. Clever; thinks a lot",
|
20 |
+
"16. Makes things exciting",
|
21 |
+
"17. Forgives others easily",
|
22 |
+
"18. Isn’t very organized",
|
23 |
+
"19. Worries a lot",
|
24 |
+
"20. Has a good, active imagination",
|
25 |
+
"21. Tends to be quiet",
|
26 |
+
"22. Usually trusts people",
|
27 |
+
"23. Tends to be lazy",
|
28 |
+
"24. Doesn’t get upset easily; steady",
|
29 |
+
"25. Is creative and inventive",
|
30 |
+
"26. Has a good, strong personality",
|
31 |
+
"27. Can be cold and distant with others",
|
32 |
+
"28. Keeps working until things are done",
|
33 |
+
"29. Can be moody",
|
34 |
+
"30. Likes artistic and creative experiences",
|
35 |
+
"31. Is kind of shy",
|
36 |
+
"32. Kind and considerate to almost everyone",
|
37 |
+
"33. Does things quickly and carefully",
|
38 |
+
"34. Stays calm in difficult situations",
|
39 |
+
"35. Likes work that is the same every time",
|
40 |
+
"36. Is outgoing; likes to be with people",
|
41 |
+
"37. Is sometimes rude to others",
|
42 |
+
"38. Makes plans and sticks to them",
|
43 |
+
"39. Get nervous easily",
|
44 |
+
"40. Likes to think and play with ideas",
|
45 |
+
"41. Doesn’t like artistic things (plays, music)",
|
46 |
+
"42. Likes to cooperate; goes along with others",
|
47 |
+
"43. Has trouble paying attention",
|
48 |
+
"44. Knows a lot about art, music and books"
|
49 |
]
|
50 |
|
51 |
+
# Scoring function based on the provided SPSS syntax
|
52 |
+
def compute_bfi_scores(*args):
|
53 |
+
responses = list(args)
|
54 |
+
# Convert 'No response' to None, else to int
|
55 |
+
processed = []
|
56 |
+
for r in responses:
|
57 |
+
if r == "No response":
|
58 |
+
processed.append(None)
|
59 |
+
else:
|
60 |
+
processed.append(int(r))
|
61 |
+
|
62 |
+
# Define traits with their respective items and scoring parameters
|
63 |
+
traits = {
|
64 |
+
'Extraversion': {
|
65 |
+
'positive': [1, 11, 16, 26, 36],
|
66 |
+
'reverse': [6, 21, 31],
|
67 |
+
'threshold': 1,
|
68 |
+
'formula_pos_mult': 5,
|
69 |
+
'formula_reverse_mult': 3,
|
70 |
+
'formula_reverse_const': 12
|
71 |
+
},
|
72 |
+
'Agreeableness': {
|
73 |
+
'positive': [7, 17, 22, 32, 42],
|
74 |
+
'reverse': [2, 12, 27, 37],
|
75 |
+
'threshold': 1,
|
76 |
+
'formula_pos_mult': 5,
|
77 |
+
'formula_reverse_mult':4,
|
78 |
+
'formula_reverse_const':16
|
79 |
+
},
|
80 |
+
'Conscientiousness': {
|
81 |
+
'positive': [3, 13, 28, 33, 38],
|
82 |
+
'reverse': [8, 18, 23, 43],
|
83 |
+
'threshold': 1,
|
84 |
+
'formula_pos_mult':5,
|
85 |
+
'formula_reverse_mult':4,
|
86 |
+
'formula_reverse_const':16
|
87 |
+
},
|
88 |
+
'Neuroticism':{
|
89 |
+
'positive':[4, 14, 19, 29, 39],
|
90 |
+
'reverse':[9, 24, 34],
|
91 |
+
'threshold':1,
|
92 |
+
'formula_pos_mult':5,
|
93 |
+
'formula_reverse_mult':3,
|
94 |
+
'formula_reverse_const':12
|
95 |
+
},
|
96 |
+
'Openness':{
|
97 |
+
'positive':[5, 10, 15, 20, 25, 30, 40, 44],
|
98 |
+
'reverse':[35, 41],
|
99 |
+
'threshold':2,
|
100 |
+
'formula_pos_mult':8,
|
101 |
+
'formula_reverse_mult':2,
|
102 |
+
'formula_reverse_const':8
|
103 |
+
}
|
104 |
}
|
|
|
105 |
|
106 |
+
scores = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
for trait, info in traits.items():
|
109 |
+
pos_items = [processed[i-1] for i in info['positive']]
|
110 |
+
rev_items = [processed[i-1] for i in info['reverse']]
|
|
|
111 |
|
112 |
+
missing_pos = pos_items.count(None)
|
113 |
+
missing_rev = rev_items.count(None)
|
114 |
+
total_missing = missing_pos + missing_rev
|
115 |
|
116 |
+
if total_missing > info['threshold']:
|
117 |
+
scores[trait] = "Incomplete"
|
118 |
+
else:
|
119 |
+
# Compute means, ignoring None
|
120 |
+
pos_values = [x for x in pos_items if x is not None]
|
121 |
+
rev_values = [x for x in rev_items if x is not None]
|
122 |
|
123 |
+
mean_pos = sum(pos_values) / len(pos_values) if pos_values else 0
|
124 |
+
mean_rev = sum(rev_values) / len(rev_values) if rev_values else 0
|
125 |
+
|
126 |
+
# Apply the scoring formula
|
127 |
+
score = (mean_pos * info['formula_pos_mult']) + (info['formula_reverse_const'] - (mean_rev * info['formula_reverse_mult']))
|
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.Row():
|
146 |
+
with gr.Column(scale=1):
|
147 |
+
pass # Empty column for spacing
|
148 |
+
with gr.Column(scale=10):
|
149 |
+
dropdowns = []
|
150 |
+
for q in questions:
|
151 |
+
dropdown = gr.Dropdown(
|
152 |
+
choices=["No response", 1, 2, 3, 4, 5],
|
153 |
+
label=q,
|
154 |
+
value="No response"
|
155 |
+
)
|
156 |
+
dropdowns.append(dropdown)
|
157 |
+
|
158 |
+
# Submit button
|
159 |
+
submit_btn = gr.Button("Submit")
|
160 |
+
submit_btn.click(compute_bfi_scores, inputs=dropdowns, outputs="result")
|
161 |
+
|
162 |
+
# Results display
|
163 |
+
result = gr.Markdown()
|
164 |
+
demo.add_component(result, "result")
|
165 |
|
166 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
# Launch the interface
|
169 |
+
demo = create_interface()
|
170 |
+
demo.launch()
|