{ "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": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(, '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 }