File size: 4,941 Bytes
56fcd5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8a3ce1
56fcd5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
001e37c
56fcd5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8a3ce1
56fcd5f
 
 
 
 
 
a8a3ce1
56fcd5f
 
 
 
 
 
 
a8a3ce1
56fcd5f
 
 
 
 
 
 
 
 
 
 
a8a3ce1
56fcd5f
 
a8a3ce1
56fcd5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8a3ce1
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://ipip.ori.org/New_IPIP-50-item-scale.htm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#os.system('pip install openpyxl') #works for huggingface-spaces\n",
    "#score of each latent variable ranges in [10, 50]\n",
    "def correct_score(score, big_5_indicator):\n",
    "    big_5_sign = big_5_indicator[0]\n",
    "    big_5_num = int(big_5_indicator[1])\n",
    "    dict1 = {\n",
    "        1 : 'Extraversion',\n",
    "        2 : 'Agreeableness',\n",
    "        3 : 'Conscientiousness',\n",
    "        4 : 'Neuroticism',\n",
    "        5 : 'Openness'\n",
    "    }\n",
    "    if big_5_sign=='+':\n",
    "        return [dict1[big_5_num], score]\n",
    "    elif big_5_sign =='-':\n",
    "        return [dict1[big_5_num], 6-score]\n",
    "# correct_score(3, big_5_indicator='-2')\n",
    "\n",
    "def make_question(q):\n",
    "    q = gr.Radio(choices=[1, 2, 3, 4, 5], label=q, value=1) #, value=5\n",
    "    return q\n",
    "\n",
    "def calculate_personality_score(questions, list_questions):\n",
    "    personality = {\n",
    "        'Extraversion': 0,\n",
    "        'Agreeableness': 0,\n",
    "        'Conscientiousness': 0,\n",
    "        'Neuroticism': 0,\n",
    "        'Openness': 0\n",
    "    }\n",
    "    for question in list_questions:\n",
    "        big_5_indicator = questions[question[0]]\n",
    "        score = correct_score(question[1], big_5_indicator=big_5_indicator)\n",
    "        personality[score[0]] += score[1]\n",
    "    return personality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7862\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7862/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(<gradio.routes.App at 0x27427d923a0>, 'http://127.0.0.1:7862/', None)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "#\n",
    "df_big5_test = pd.read_csv('Big5.csv', sep=';', header=None)\n",
    "df_big5_test = df_big5_test[[0, 6]]\n",
    "df_big5_test.columns = ['question', 'score']\n",
    "df_big5_test['score'] = df_big5_test['score'].apply(lambda x : x.replace('(', '').replace(')', '')[::-1])\n",
    "# df.index = df.pop('question')\n",
    "questions = dict()\n",
    "for el in df_big5_test.values.tolist():\n",
    "    questions[el[0]] = el[1]\n",
    "questions\n",
    "\n",
    "#the first module becomes text1, the second module file1\n",
    "def greet(*args): \n",
    "    args_list = [item for item in args]\n",
    "\n",
    "    #extract big5\n",
    "    q_list = list(questions)\n",
    "    a_list = args_list[:]\n",
    "    list_total = [[q_list[x], a_list[x]] for x in range(len(a_list))]\n",
    "    personality = calculate_personality_score(questions, list_total)\n",
    "    personality = { x : personality[x]/50 for x in personality}\n",
    "    # personality = [personality['Openness'], personality['Conscientiousness'], personality['Extraversion'], personality['Agreeableness'], personality['Neuroticism']]\n",
    "    # personality = [x/50 for x in personality]\n",
    "\n",
    "    #to return a file:\n",
    "    #return 'new.xlsx'\n",
    "    return personality\n",
    "\n",
    "iface = gr.Interface(\n",
    "    fn=greet, \n",
    "    inputs=[make_question(q) for q in list(questions)], \n",
    "        outputs=[\"text\"]\n",
    "    )\n",
    "iface.launch(share=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.0 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "fdf377d643bc1cb065454f0ad2ceac75d834452ecf289e7ba92c6b3f59a7cee1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}