johnpaulbin commited on
Commit
3a15783
·
verified ·
1 Parent(s): be2e7d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -101
app.py CHANGED
@@ -1,115 +1,170 @@
1
  import gradio as gr
2
 
3
- # Define the list of items with their questions and whether they are reverse-scored
4
- items = [
5
- {'BFI': 'BFI_1', 'question': 'Talks a lot', 'reverse': False},
6
- {'BFI': 'BFI_2', 'question': 'Notices other people\'s weak points', 'reverse': True},
7
- {'BFI': 'BFI_3', 'question': 'Does things carefully and completely', 'reverse': False},
8
- {'BFI': 'BFI_4', 'question': 'Is sad, depressed', 'reverse': False},
9
- {'BFI': 'BFI_5', 'question': 'Is original, comes up with new ideas', 'reverse': False},
10
- {'BFI': 'BFI_6', 'question': 'Keeps their thoughts to themselves', 'reverse': True},
11
- {'BFI': 'BFI_7', 'question': 'Is helpful and not selfish with others', 'reverse': False},
12
- {'BFI': 'BFI_8', 'question': 'Can be kind of careless', 'reverse': True},
13
- {'BFI': 'BFI_9', 'question': 'Is relaxed, handles stress well', 'reverse': True},
14
- {'BFI': 'BFI_10', 'question': 'Is curious about lots of different things', 'reverse': False},
15
- {'BFI': 'BFI_11', 'question': 'Has a lot of energy', 'reverse': False},
16
- {'BFI': 'BFI_12', 'question': 'Starts arguments with others', 'reverse': True},
17
- {'BFI': 'BFI_13', 'question': 'Is a good, hard worker', 'reverse': False},
18
- {'BFI': 'BFI_14', 'question': 'Can be tense; not always easy going', 'reverse': False},
19
- {'BFI': 'BFI_15', 'question': 'Clever; thinks a lot', 'reverse': False},
20
- {'BFI': 'BFI_16', 'question': 'Makes things exciting', 'reverse': False},
21
- {'BFI': 'BFI_17', 'question': 'Forgives others easily', 'reverse': False},
22
- {'BFI': 'BFI_18', 'question': 'Isn\'t very organized', 'reverse': True},
23
- {'BFI': 'BFI_19', 'question': 'Worries a lot', 'reverse': False},
24
- {'BFI': 'BFI_20', 'question': 'Has a good, active imagination', 'reverse': False},
25
- {'BFI': 'BFI_21', 'question': 'Tends to be quiet', 'reverse': True},
26
- {'BFI': 'BFI_22', 'question': 'Usually trusts people', 'reverse': False},
27
- {'BFI': 'BFI_23', 'question': 'Tends to be lazy', 'reverse': True},
28
- {'BFI': 'BFI_24', 'question': 'Doesn\'t get upset easily; steady', 'reverse': True},
29
- {'BFI': 'BFI_25', 'question': 'Is creative and inventive', 'reverse': False},
30
- {'BFI': 'BFI_26', 'question': 'Has a good, strong personality', 'reverse': False},
31
- {'BFI': 'BFI_27', 'question': 'Can be cold and distant with others', 'reverse': True},
32
- {'BFI': 'BFI_28', 'question': 'Keeps working until things are done', 'reverse': False},
33
- {'BFI': 'BFI_29', 'question': 'Can be moody', 'reverse': False},
34
- {'BFI': 'BFI_30', 'question': 'Likes artistic and creative experiences', 'reverse': False},
35
- {'BFI': 'BFI_31', 'question': 'Is kind of shy', 'reverse': True},
36
- {'BFI': 'BFI_32', 'question': 'Kind and considerate to almost everyone', 'reverse': False},
37
- {'BFI': 'BFI_33', 'question': 'Does things quickly and carefully', 'reverse': False},
38
- {'BFI': 'BFI_34', 'question': 'Stays calm in difficult situations', 'reverse': True},
39
- {'BFI': 'BFI_35', 'question': 'Likes work that is the same every time', 'reverse': True},
40
- {'BFI': 'BFI_36', 'question': 'Is outgoing; likes to be with people', 'reverse': False},
41
- {'BFI': 'BFI_37', 'question': 'Is sometimes rude to others', 'reverse': True},
42
- {'BFI': 'BFI_38', 'question': 'Makes plans and sticks to them', 'reverse': False},
43
- {'BFI': 'BFI_39', 'question': 'Gets nervous easily', 'reverse': False},
44
- {'BFI': 'BFI_40', 'question': 'Likes to think and play with ideas', 'reverse': False},
45
- {'BFI': 'BFI_41', 'question': 'Doesn\'t like artistic things (plays, music)', 'reverse': True},
46
- {'BFI': 'BFI_42', 'question': 'Likes to cooperate; goes along with others', 'reverse': False},
47
- {'BFI': 'BFI_43', 'question': 'Has trouble paying attention', 'reverse': True},
48
- {'BFI': 'BFI_44', 'question': 'Knows a lot about art, music, and books', 'reverse': False},
49
  ]
50
 
51
- # Define which items belong to which traits
52
- traits = {
53
- 'Extraversion': {
54
- 'positive': ['BFI_1', 'BFI_11', 'BFI_16', 'BFI_26', 'BFI_36'],
55
- 'negative': ['BFI_6', 'BFI_21', 'BFI_31'],
56
- },
57
- 'Agreeableness': {
58
- 'positive': ['BFI_7', 'BFI_17', 'BFI_22', 'BFI_32', 'BFI_42'],
59
- 'negative': ['BFI_2', 'BFI_12', 'BFI_27', 'BFI_37'],
60
- },
61
- 'Conscientiousness': {
62
- 'positive': ['BFI_3', 'BFI_13', 'BFI_28', 'BFI_33', 'BFI_38'],
63
- 'negative': ['BFI_8', 'BFI_18', 'BFI_23', 'BFI_43'],
64
- },
65
- 'Neuroticism': {
66
- 'positive': ['BFI_4', 'BFI_14', 'BFI_19', 'BFI_29', 'BFI_39'],
67
- 'negative': ['BFI_9', 'BFI_24', 'BFI_34'],
68
- },
69
- 'Openness': {
70
- 'positive': ['BFI_5', 'BFI_10', 'BFI_15', 'BFI_20', 'BFI_25', 'BFI_30', 'BFI_40', 'BFI_44'],
71
- 'negative': ['BFI_35', 'BFI_41'],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  }
73
- }
74
 
75
- # Define the scoring function
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
- trait_scores = {}
84
- for trait in traits:
85
- positive_items = traits[trait]['positive']
86
- negative_items = traits[trait]['negative']
87
 
88
- positive_sum = sum(responses[item] for item in positive_items)
89
- negative_sum = sum(6 - responses[item] for item in negative_items)
 
90
 
91
- trait_scores[trait] = positive_sum + negative_sum
 
 
 
 
 
92
 
93
- return trait_scores
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  # Create the Gradio interface
96
- inputs = []
97
- for item in items:
98
- question = item['question']
99
- input_component = gr.Radio(
100
- label=question,
101
- choices=['1 (Disagree a lot)', '2', '3', '4', '5 (Agree a lot)'],
102
- type='value',
103
- )
104
- inputs.append(input_component)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- iface = gr.Interface(
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
- iface.launch()
 
 
 
1
  import gradio as gr
2
 
3
+ # Define the 44 BFI questions
4
+ questions = [
5
+ "1. Talks a lot",
6
+ "2. Notices other peoples 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. Isnt 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. Doesnt 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. Doesnt 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()