import gradio as gr # Define the list of items with their questions and whether they are reverse-scored items = [ {'key': 'BFI_1', 'question': 'Talks a lot', 'reverse': False}, {'key': 'BFI_2', 'question': 'Notices other people’s weak points', 'reverse': True}, {'key': 'BFI_3', 'question': 'Does things carefully and completely', 'reverse': False}, {'key': 'BFI_4', 'question': 'Is sad, depressed', 'reverse': False}, {'key': 'BFI_5', 'question': 'Is original, comes up with new ideas', 'reverse': False}, {'key': 'BFI_6', 'question': 'Keeps their thoughts to themselves', 'reverse': True}, {'key': 'BFI_7', 'question': 'Is helpful and not selfish with others', 'reverse': False}, {'key': 'BFI_8', 'question': 'Can be kind of careless', 'reverse': True}, {'key': 'BFI_9', 'question': 'Is relaxed, handles stress well', 'reverse': True}, {'key': 'BFI_10', 'question': 'Is curious about lots of different things', 'reverse': False}, {'key': 'BFI_11', 'question': 'Has a lot of energy', 'reverse': False}, {'key': 'BFI_12', 'question': 'Starts arguments with others', 'reverse': True}, {'key': 'BFI_13', 'question': 'Is a good, hard worker', 'reverse': False}, {'key': 'BFI_14', 'question': 'Can be tense; not always easy going', 'reverse': False}, {'key': 'BFI_15', 'question': 'Clever; thinks a lot', 'reverse': False}, {'key': 'BFI_16', 'question': 'Makes things exciting', 'reverse': False}, {'key': 'BFI_17', 'question': 'Forgives others easily', 'reverse': False}, {'key': 'BFI_18', 'question': 'Isn\'t very organized', 'reverse': True}, {'key': 'BFI_19', 'question': 'Worries a lot', 'reverse': False}, {'key': 'BFI_20', 'question': 'Has a good, active imagination', 'reverse': False}, {'key': 'BFI_21', 'question': 'Tends to be quiet', 'reverse': True}, {'key': 'BFI_22', 'question': 'Usually trusts people', 'reverse': False}, {'key': 'BFI_23', 'question': 'Tends to be lazy', 'reverse': True}, {'key': 'BFI_24', 'question': 'Doesn\'t get upset easily; steady', 'reverse': True}, {'key': 'BFI_25', 'question': 'Is creative and inventive', 'reverse': False}, {'key': 'BFI_26', 'question': 'Has a good, strong personality', 'reverse': False}, {'key': 'BFI_27', 'question': 'Can be cold and distant with others', 'reverse': True}, {'key': 'BFI_28', 'question': 'Keeps working until things are done', 'reverse': False}, {'key': 'BFI_29', 'question': 'Can be moody', 'reverse': False}, {'key': 'BFI_30', 'question': 'Likes artistic and creative experiences', 'reverse': False}, {'key': 'BFI_31', 'question': 'Is kind of shy', 'reverse': True}, {'key': 'BFI_32', 'question': 'Kind and considerate to almost everyone', 'reverse': False}, {'key': 'BFI_33', 'question': 'Does things quickly and carefully', 'reverse': False}, {'key': 'BFI_34', 'question': 'Stays calm in difficult situations', 'reverse': True}, {'key': 'BFI_35', 'question': 'Likes work that is the same every time', 'reverse': True}, {'key': 'BFI_36', 'question': 'Is outgoing; likes to be with people', 'reverse': False}, {'key': 'BFI_37', 'question': 'Is sometimes rude to others', 'reverse': True}, {'key': 'BFI_38', 'question': 'Makes plans and sticks to them', 'reverse': False}, {'key': 'BFI_39', 'question': 'Gets nervous easily', 'reverse': False}, {'key': 'BFI_40', 'question': 'Likes to think and play with ideas', 'reverse': False}, {'key': 'BFI_41', 'question': 'Doesn\'t like artistic things (plays, music)', 'reverse': True}, {'key': 'BFI_42', 'question': 'Likes to cooperate; goes along with others', 'reverse': False}, {'key': 'BFI_43', 'question': 'Has trouble paying attention', 'reverse': True}, {'key': 'BFI_44', 'question': 'Knows a lot about art, music, and books', 'reverse': False}, ] # Define which items belong to which traits traits = { 'Extraversion': { 'positive': ['BFI_1', 'BFI_11', 'BFI_16', 'BFI_26', 'BFI_36'], 'negative': ['BFI_6', 'BFI_21', 'BFI_31'], }, 'Agreeableness': { 'positive': ['BFI_7', 'BFI_17', 'BFI_22', 'BFI_32', 'BFI_42'], 'negative': ['BFI_2', 'BFI_12', 'BFI_27', 'BFI_37'], }, 'Conscientiousness': { 'positive': ['BFI_3', 'BFI_13', 'BFI_28', 'BFI_33', 'BFI_38'], 'negative': ['BFI_8', 'BFI_18', 'BFI_23', 'BFI_43'], }, 'Neuroticism': { 'positive': ['BFI_4', 'BFI_14', 'BFI_19', 'BFI_29', 'BFI_39'], 'negative': ['BFI_9', 'BFI_24', 'BFI_34'], }, 'Openness': { 'positive': ['BFI_5', 'BFI_10', 'BFI_15', 'BFI_20', 'BFI_25', 'BFI_30', 'BFI_40', 'BFI_44'], 'negative': ['BFI_35', 'BFI_41'], } } # Define the scoring function def compute_trait_scores(*args): responses = {} for i, item in enumerate(items): key = item['key'] response = int(args[i]) responses[key] = response trait_scores = {} for trait, trait_data in traits.items(): positive_sum = 0 negative_sum = 0 for item_key in trait_data['positive']: try: positive_sum += responses[item_key] except KeyError: print(f"Warning: Response for {item_key} not found.") #Handle missing keys gracefully. for item_key in trait_data['negative']: try: negative_sum += 6 - responses[item_key] except KeyError: print(f"Warning: Response for {item_key} not found.") #Handle missing keys gracefully. trait_scores[trait] = positive_sum + negative_sum return trait_scores # Create the Gradio interface inputs = [] for item in items: question = item['question'] input_component = gr.Radio( label=question, choices=['1 (Disagree a lot)', '2', '3', '4', '5 (Agree a lot)'], type='value', required=True ) inputs.append(input_component) iface = gr.Interface( fn=compute_trait_scores, inputs=inputs, outputs=gr.JSON(label="Your Big Five Personality Scores"), title='Big Five Personality Test', description='Answer the following questions to see your Big Five personality trait scores.', allow_flagging='never' ) iface.launch()