Spaces:
Runtime error
Runtime error
File size: 2,386 Bytes
56fcd5f 6ba8908 56fcd5f f09d8e2 56fcd5f 5885156 56fcd5f |
1 2 3 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 |
#os.system('pip install openpyxl') #works for huggingface-spaces
#score of each latent variable ranges in [10, 50]
def correct_score(score, big_5_indicator):
big_5_sign = big_5_indicator[0]
big_5_num = int(big_5_indicator[1])
dict1 = {
1 : 'Extraversion',
2 : 'Agreeableness',
3 : 'Conscientiousness',
4 : 'Neuroticism',
5 : 'Openness'
}
if big_5_sign=='+':
return [dict1[big_5_num], score]
elif big_5_sign =='-':
return [dict1[big_5_num], 6-score]
# correct_score(3, big_5_indicator='-2')
def make_question(q):
q = gr.Radio(choices=[1, 2, 3, 4, 5], label=q) #, value=5
return q
def calculate_personality_score(questions, list_questions):
personality = {
'Extraversion': 0,
'Agreeableness': 0,
'Conscientiousness': 0,
'Neuroticism': 0,
'Openness': 0
}
for question in list_questions:
big_5_indicator = questions[question[0]]
score = correct_score(question[1], big_5_indicator=big_5_indicator)
personality[score[0]] += score[1]
return personality
import gradio as gr
import pandas as pd
import numpy as np
import os
#
df_big5_test = pd.read_csv('Big5.csv', sep=';', header=None)
df_big5_test = df_big5_test[[0, 6]]
df_big5_test.columns = ['question', 'score']
df_big5_test['score'] = df_big5_test['score'].apply(lambda x : x.replace('(', '').replace(')', '')[::-1])
# df.index = df.pop('question')
questions = dict()
for el in df_big5_test.values.tolist():
questions[el[0]] = el[1]
questions
#the first module becomes text1, the second module file1
def greet(*args):
args_list = [item for item in args]
#extract big5
q_list = list(questions)
a_list = args_list[:]
list_total = [[q_list[x], a_list[x]] for x in range(len(a_list))]
personality = calculate_personality_score(questions, list_total)
personality = { x : personality[x]/50 for x in personality}
# personality = [personality['Openness'], personality['Conscientiousness'], personality['Extraversion'], personality['Agreeableness'], personality['Neuroticism']]
# personality = [x/50 for x in personality]
#to return a file:
#return 'new.xlsx'
return personality
iface = gr.Interface(
fn=greet,
inputs=[make_question(q) for q in list(questions)],
outputs=["text"]
)
iface.launch() |