CoolFace
Apppublic

GwendalTsang/Emotyc_visualisation

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
app.cpython-312.pyc279 linesDownload Raw Back to __pycache__
1�

2e�j�O����UdZddlmZddlZddlmZmZddlmZ	ddl3mZddlm
Z
ddlZddlZddlZee�j+�j,ZedzZgd	�Zd4Zddd
dddddd�Zddddd�Zdddddd�Zded <dRd!�ZeGd"�d#��Z eGd$�d%��Z!dSd&�Z"dTd'�Z#dUd(�Z$dVd)�Z%d*d+gZ&dWd,�Z'								dXd-�Z(e)d.�e)d/�e)d.�e#�Z*e)d0e+e*��d1��e*re,e*j[��dndZ.dYd2�Z/						dZd3�Z0d4Z1ejdd5ejfjiejfjjjlejfjjjnejfjjjnejfjqd6�d7d8g�9�d:�;�5Z9ejtd<�=�5ejve1�ejx�5ejze,e*j[��e.d>d?d@�A�Z>ddd�ejvdB�Z?ej�dC�D�ZAej�dBdEdF�G�ZCejtdHd?�I�5ejvdB�ZDej�ej��dJdEd?dK�L�ZGddd�ddd�e>j�e/e>geAe?eGg�M�eCj�e0eCe>geDeGeCg�M�e9j�e/e>geAe?eGg�M�ddd�eJdNk(re9j�dOdP�Q�yy#1swY��xYw#1swY��xYw#1swY��xYw#1swY�HxYw)[u5EMOTYC — Visualisation interactive des performances.6 7Space Gradio avec :8- Dropdown pour sélectionner une configuration (5 XLSX pré-chargés)9- Tableau HTML de performances (F1, Précision, Rappel, FN/FP/TN/TP)10- Cellules FN/FP/TN/TP cliquables → panneau d'instances concrétes11�)�annotationsN)�	dataclass�field)�escape)�Path)�Any�data)�Emo�Comportementale�Designee�Montree�Suggeree�Base�Complexe�12Admiration�Autre�Colere�Culpabilite�Degout�Embarras�Fierte�Jalousie�Joie�Peur�Surprise�	Tristesse�_pred_emotycuColèreuCulpabilitéuDégoûtuFiertéu13DésignéeuMontréeu14SuggéréeuÉmo)rrrrrr
rr15u✅ Vrais Positifs (TP)u⚠️ Faux Positifs (FP)u❌ Faux Négatifs (FN)u✓ Vrais Négatifs (TN)��tp�fp�fn�tnzCyberAggAdo200.parquetz!CyberAggAdoGlobal_Context.parquetz&CyberAggAdoGlobal_SansContexte.parquetzTextToKids_Context.parquetzTextToKids_SansContexte.parquet)zCyberAggAdo 200uCyberAggAdo Global — Contexteu$CyberAggAdo Global — Sans ContexteuTextToKids — ContexteuTextToKids — Sans Contextezdict[str, str]�CONFIGSc�.�tj||�S)N)�
DISPLAY_NAMES�get��labels �7/workspaces/codespaces-blank/EMOTYCVisualisation/app.py�display_namer*Bs�����U�E�*�*�c�^�eZdZUded<ded<ded<ded<ded<ded	<ded16<ded<y)
�LabelMetrics�strr(�float�f1�	precision�recall�intrr r!r"N)�__name__�17__module__�__qualname__�__annotations__�r+r)r-r-Js+���J�
�I����M��G��G��G��Gr+r-c�\�eZdZUded<ded<ded<ded<d	ed18<ee��Zded
<y)�19ConfigDatar.�namezpd.DataFrame�dfz	list[str]�labelszlist[LabelMetrics]�metricsr/�macro_f1)�default_factoryzdict[str, dict[str, list[int]]]�20case_indexN)r4r5r6r7r�dictrAr8r+r)r:r:Vs.��21
�I�����
���O�27��2M�J�/�Mr+r:c
���tj|�}g}tD];}|�t��}||jvs�||jvs�+|j|��=|st
d|j����g}i}|D�]}|�t��}||jd�jt�j}||jd�jt�j}	|dk(|	dk(z}22|dk(|	dk(z}|dk(|	dk(z}|dk(|	dk(z}
t|23j��}t|j��}t|j��}t|
j��}||zdkDr|||zznd}||zdkDr|||zznd}||zdkDrd|z|z||zznd}|jt|t|d�t|d�t|d�||||���tj |24�dj#�tj |�dj#�tj |�dj#�tj |
�dj#�d�||<��tt%tj&|D�cgc]}|j(��c}��d�}t+||||||�	�Scc}w)25zDLoad a single config parquet and compute all metrics + case indices.zNo valid label pairs found in r�g��)r(r0r1r2rr r!r"r)r;r<r=r>r?rA)�pd�read_parquet�26ALL_LABELS�PRED_SUFFIX�columns�append�27ValueErrorr;�fillna�astyper3�values�sumr-�round�np�where�tolistr/�meanr0r:)r;�	xlsx_pathr<�	availabler(�pred_col�metrics_listrA�gold�pred�tp_mask�fp_mask�fn_mask�tn_maskrr r!r"�prec�recr0�mr?s                       r)�load_configrdes���	����	#�B��I��$���W�[�M�*���B�J�J��8�r�z�z�#9����U�#�$�28��9�)�.�.�9I�J�K�K�(*�L�24�J��29���W�[�M�*���%�y����"�)�)�#�.�5�5���(�|�"�"�1�%�,�,�S�1�8�8���1�9����+���1�9����+���1�9����+���1�9����+��
�����
��
�����
��
�����
��
�����
��"$�r�'�Q��r�R�"�W�~�C��!#�b��A�
�b�B��G�n�3��15���q�0@�a�$�h��n��s�30�+�c�����L��E�"�a�L�E�$��N���a�=�R�B�2�"�31�	��(�(�7�#�A�&�-�-�/��(�(�7�#�A�&�-�-�/��(�(�7�#�A�&�-�-�/��(�(�7�#�A�&�-�-�/�	32�33�5��134�>�U�2�7�7�,�#?�Q�A�D�D�#?�@�A�1�E�H��
�b��L��j����$@s�2K&c35�P�i}tj�D]�\}}t|z}|j�r`t	d|�d|�d��t||�||<t	dt
||j��dt
||j��d���t	d|�����|S)z#Load all configurations at startup.z
Chargement : z (�)u  → z	 lignes, z labelsu⚠️ Fichier manquant : )	r#�items�DATA_DIR�exists�printrd�lenr<r=)�configsr;�filename�paths    r)�load_all_configsro�s���%'�G�!�-�-�/�7���h��(�"���;�;�=��M�$��r�(��1�5�6�'��d�3�G�D�M��F�3�w�t�}�/�/�0�1��3�w�t�}�?S�?S�;T�:U�U\�]�^��.�t�f�5�6�7��Nr+c��|dk\ry|dk\ryy)Ng�������?z#15803dg�?z#b45309z#be123cr8)�values r)�
_metric_colorrr�s����|����|��r+c��g}|jD�]-}tt|j��}t|j�}t	|j36�}t	|j�}t	|j�}djgd�|��d�|��d�|j37d��d�|��d�|jd��d�|��d�|jd��d�|��d�|j��d�|��d�|j��d�|��d	�|j��d�|��d38�|j��d��}|j|���0t	|j�}	|jd|	�d|jd�d
��dj|�}39d|40�d�S)zDGenerate an interactive HTML performance table with clickable cells.�z<tr>41  <td class="cell-label">z-</td>42  <td class="cell-metric" style="color:z">�.3fz:</td>43  <td class="cell-count cell-clickable" data-label="z." data-outcome="fn" onclick="cellClick(this)">z." data-outcome="fp" onclick="cellClick(this)">z." data-outcome="tn" onclick="cellClick(this)">z." data-outcome="tp" onclick="cellClick(this)">z</td>44</tr>z�<tr class="row-macro">45  <td class="cell-label" style="font-style:italic;">Macro avg</td>46  <td class="cell-metric" style="color:z�</td>47  <td class="cell-metric"></td>48  <td class="cell-metric"></td>49  <td class="cell-count"></td>50  <td class="cell-count"></td>51  <td class="cell-count"></td>52  <td class="cell-count"></td>53</tr>�54uP55<style>56  .perf-table-wrap {57    overflow-x: auto;58    margin: 0 auto;59    max-width: 900px;60  }61  .perf-table {62    border-collapse: collapse;63    font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;64    font-size: 13.5px;65    width: 100%;66    min-width: 700px;67  }68  .perf-table thead th {69    background: linear-gradient(135deg, #334155 0%, #475569 100%);70    color: #f1f5f9;71    font-weight: 600;72    padding: 10px 14px;73    border: 1px solid #475569;74    text-align: center;75    letter-spacing: 0.02em;76    position: sticky;77    top: 0;78    z-index: 2;79  }80  .perf-table thead th:first-child {81    text-align: left;82    border-top-left-radius: 8px;83  }84  .perf-table thead th:last-child {85    border-top-right-radius: 8px;86  }87  .perf-table tbody tr {88    transition: background 0.15s ease;89  }90  .perf-table tbody tr:nth-child(even) {91    background: #f8fafc;92  }93  .perf-table tbody tr:nth-child(odd) {94    background: #ffffff;95  }96  .perf-table tbody tr:hover {97    background: #e0e7ff !important;98  }99  .perf-table tbody tr.row-macro {100    background: #f1f5f9 !important;101    border-top: 2px solid #94a3b8;102  }103  .cell-label {104    font-weight: 600;105    color: #0f172a;106    padding: 9px 14px;107    border: 1px solid #e2e8f0;108    text-align: left;109    white-space: nowrap;110  }111  .cell-metric {112    font-weight: 600;113    padding: 9px 14px;114    border: 1px solid #e2e8f0;115    text-align: center;116    font-variant-numeric: tabular-nums;117  }118  .cell-count {119    color: #64748b;120    padding: 9px 14px;121    border: 1px solid #e2e8f0;122    text-align: center;123    font-variant-numeric: tabular-nums;124  }125  .cell-clickable {126    cursor: pointer;127    position: relative;128    transition: all 0.15s ease;129  }130  .cell-clickable:hover {131    background: #c7d2fe !important;132    color: #1e1b4b;133    font-weight: 700;134    box-shadow: inset 0 0 0 2px #6366f1;135  }136  .cell-clickable.selected {137    background: #6366f1 !important;138    color: #fff !important;139    font-weight: 700;140    box-shadow: inset 0 0 0 2px #4338ca;141  }142 143  /* Dark mode */144  @media (prefers-color-scheme: dark) {145    .perf-table thead th {146      background: linear-gradient(135deg, #1e293b 0%, #334155 100%);147      border-color: #475569;148    }149    .perf-table tbody tr:nth-child(even) { background: #1e293b; }150    .perf-table tbody tr:nth-child(odd) { background: #0f172a; }151    .perf-table tbody tr:hover { background: #312e81 !important; }152    .perf-table tbody tr.row-macro { background: #1e293b !important; border-top-color: #475569; }153    .cell-label { color: #e2e8f0; border-color: #334155; }154    .cell-metric { border-color: #334155; }155    .cell-count { color: #94a3b8; border-color: #334155; }156    .cell-clickable:hover {157      background: #3730a3 !important;158      color: #e0e7ff;159      box-shadow: inset 0 0 0 2px #818cf8;160    }161    .cell-clickable.selected {162      background: #4f46e5 !important;163      color: #fff !important;164      box-shadow: inset 0 0 0 2px #6366f1;165    }166  }167 168  /* Gradio dark mode override */169  .dark .perf-table thead th {170    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);171    border-color: #475569;172  }173  .dark .perf-table tbody tr:nth-child(even) { background: #1e293b; }174  .dark .perf-table tbody tr:nth-child(odd) { background: #0f172a; }175  .dark .perf-table tbody tr:hover { background: #312e81 !important; }176  .dark .perf-table tbody tr.row-macro { background: #1e293b !important; border-top-color: #475569; }177  .dark .cell-label { color: #e2e8f0; border-color: #334155; }178  .dark .cell-metric { border-color: #334155; }179  .dark .cell-count { color: #94a3b8; border-color: #334155; }180  .dark .cell-clickable:hover {181    background: #3730a3 !important;182    color: #e0e7ff;183    box-shadow: inset 0 0 0 2px #818cf8;184  }185  .dark .cell-clickable.selected {186    background: #4f46e5 !important;187    color: #fff !important;188    box-shadow: inset 0 0 0 2px #6366f1;189  }190</style>191 192<div class="perf-table-wrap">193<table class="perf-table">194  <thead>195    <tr>196      <th>Label</th>197      <th>F1</th>198      <th>Précision</th>199      <th>Rappel</th>200      <th>FN</th>201      <th>FP</th>202      <th>TN</th>203      <th>TP</th>204    </tr>205  </thead>206  <tbody>207    a�208  </tbody>209</table>210</div>211 212<script>213function cellClick(el) {214  // Remove previous selection215  document.querySelectorAll('.cell-clickable.selected').forEach(c => c.classList.remove('selected'));216  el.classList.add('selected');217 218  const label = el.getAttribute('data-label');219  const outcome = el.getAttribute('data-outcome');220  const value = label + '|' + outcome;221 222  // Find the hidden textbox and update it223  const hiddenInput = document.querySelector('#cell_click_input textarea');224  if (hiddenInput) {225    hiddenInput.value = value;226    hiddenInput.dispatchEvent(new Event('input', { bubbles: true }));227    // Trigger Gradio's change228    setTimeout(() => {229      hiddenInput.dispatchEvent(new Event('change', { bubbles: true }));230    }, 50);231  }232}233</script>234)r>�html_escaper*r(rrr0r1r2�joinr!r r"rrLr?)�config�rowsrc�dname�canon�f1_color�235prec_color�	rec_color�row�macro_color�bodys           r)�generate_performance_htmlr��sy��
�D�
�^�^����L����1�2���A�G�G�$�� ����&��"�1�;�;�/�236�!�!�(�(�+�	�		�		��		���		�!(�		�(0�z�		�13�		�45�D�D��:�		�>(�		�)3�|�		�46�		�78�k�k�#�5F�		�G(�		�)2�{�			�35�			�67�X�X�c�N�			�C5�			�2376;�G�		�238<j�		�239kl�jn�jn�io�		�240p5�		�6;�G�
		�<j�
		�kl�jn�jn�io�
		�p5�
		�6;�G�		�<j�		�kl�jn�jn�io�		�p5�		�6;�G�		�<j�		�kl�jn�jn�io�		�p�		��	
���C��%�* ����0�K��K�K�(�(3�}�B�v���s�6K�L�		�	241��9�9�T�?�D�Y�r242�F��st�tr+�_run1�_run2c�&�tD]}||vs�yy)z9Check if a column should be shown in the instance viewer.FT)�EXCLUDE_COLS_PATTERNS)�col�patterns  r)�_should_include_colr��s ��(����c�>���r+c243��||jvrdtj�fS|j|j|g�}|s:dt	|��dt244j||��d�tj�fS|jj|j�}|jD�cgc]}t|�s�|��}}||}g}d|jvr|jd�||jvr|j|�|�t��}||jvr|j|�|jD�cgc]	}||vs�|��}	}|||	z}dt245j||��dt	|��dt|��d	t|�d246kDrdnd��}247|248|jd
��fScc}wcc}w)z1Retrieve instances for a given label and outcome.zLabel introuvable.zAucune instance pour u — �.�TEXTz### u — **u	** — **z** instancerD�srtT)�drop)rArG�	DataFramer&r*�OUTCOME_DISPLAYr<�iloc�copyrKr�rLrJrk�reset_index)ryr(�outcome�indices�subset�c�cols_to_show�
priority_colsrY�	remaining�titles           r)�
get_instancesr��s���
�F�%�%�%�#�R�\�\�^�3�3�����&�*�*�7�B�7�G��&�|�E�':�&;�5��AT�AT�U\�^e�Af�@g�gh�i�km�kw�kw�ky�y�y��Y�Y�^�^�G�
$�
)�
)�
+�F� &�~�~�H�!�1D�Q�1G�A�H�L�H�
�L�
!�F��M�
��������V�$���������U�#����
�&�H��6�>�>�!����X�&�"�N�N�E�q�a�}�.D��E�I�E�
�M�I�-�
.�F���"�"�7�G�4�5�6
�
�%�
 �!�"
�
��\�N�+�S��\�A�-=�c�2�%F�	H�249��&�$�$�$�$�/�/�/��-I��Fs�5F8�F8�	F=�F=z<============================================================u+EMOTYC — Chargement des configurations...u250✅ u configuration(s) chargée(s).251c	��|tvrddtj�fSt|}t|�}t	|j252�}t	|j�}d|�d|�d|�d|jd�d�	}||tj�fS)	zEWhen user selects a new config, update the table and clear instances.u"<p>Configuration non trouvée.</p>rtu📊 **u** — z instances, z labels, Macro F1 = **ruz**)�ALL_CONFIGSrGr�r�rkr<r=r?)�config_namery�html�n_rows�n_labels�infos      r)�on_config_changer��s����+�%�3�R�����G�G�
��
%�F�$�V�,�D�
����^�F��6�=�=�!�H�
�[�M�����X�J�F\�]c�]l�]l�mp�\q�qs�t�D���r�|�|�~�%�%r+c���|rd|vrdtj�dfS|jdd�\}}|tvrdtj�dfSt|}t	|||�\}}||dfS)z$When user clicks a TP/FP/TN/FN cell.�|rtrDzConfiguration introuvable.)rGr��splitr�r�)�253cell_valuer�r(r�ryr��instances_dfs       r)�
on_cell_clickr��s|����J�.��2�<�<�>�2�%�%��%�%�c�1�-�N�E�7��+�%�+�R�\�\�^�R�?�?�
��
%�F�'���w�?��E�<��,��"�"r+u�254# 📊 EMOTYC — Visualisation des Performances255 256Sélectionnez une configuration pour afficher le tableau de performances du modèle de détection des émotions.257**Cliquez sur les cellules FN, FP, TN ou TP** pour explorer les instances concrètes.258uEMOTYC — Performances�Interz	system-uiz259sans-serif)�primary_hue�
secondary_hue�neutral_hue�fontz�260    .main-container { max-width: 1100px; margin: 0 auto; }261    #cell_click_input { display: none !important; }262    .instance-panel { margin-top: 8px; }263    footer { display: none !important; }264    )r��theme�csszmain-container)�elem_classes�
ConfigurationTrF)�choicesrqr(�interactive�scalertzTableau de performancesr'F�cell_click_input)rq�visible�elem_idzinstance-panel)r�r��	Instancesi�)rqr(r��wrap�265max_height)r!�inputs�outputs�__main__z0.0.0.0i�)�server_name�server_port)r(r.�returnr.)r;r.rWrr�r:)r�zdict[str, ConfigData])rqr/r�r.)ryr:r�r.)r�r.r��bool)ryr:r(r.r�r.r�ztuple[str, pd.DataFrame])r�r.r�ztuple[str, str, pd.DataFrame])r�r.r�r.r�ztuple[str, pd.DataFrame, str])L�__doc__�266__future__r�json�dataclassesrrr�rrw�pathlibr�typingr�gradio�gr�numpyrS�pandasrG�__file__�resolve�parent�BASE_DIRrhrIrJr%r�r#r7r*r-r:rdrorrr�r�r�r�rjr�rk�list�keys�DEFAULT_CONFIGr�r��	HEADER_MD�Blocks�themes�Soft�colors�indigo�slate�267GoogleFont�demo�Column�Markdown�Row�Dropdown�config_dropdown�config_info�HTML�	perf_html�Textboxr��instance_title�	Dataframer��instance_table�change�loadr4�launchr8r+r)�<module>r�s
���#��(�&��������>�!�!�#�*�*���f����268����!�������	�
�$�269%�270#�271$�	��0�'J�,T�;�$E�����+�������N�N��N�6�r�$�[�H�W���272�%0��%0�"�%0�-0�%0��%0�X�h���3�4��h��� ����s�;�� � @�A�B�0;��k�&�&�(�)�!�,���273&�#��#�"%�#�"�#�(
�	��R�Y�Y�274#�275�)�)�.�.��I�I�$�$�+�+��i�i�&�&�,�,��I�I�$�$�*�*��i�i�"�"�7�+�[�,�
G�	��	��F�276�	���� 0�	1�!�����I��
�R�V�V�X�	�)�b�k�k��[�-�-�/�0�$�%� ���O�	�"�b�k�k�"�o���B�G�G�";�<�	�&�2�:�:���&�277���R�Y�Y�$4�d�
C�	�(�R�[�[��_�N�)�R�\�\�"�b�l�l�n�!�!����N�	�3!�J����� ��K��8������� �/�2���1A�B���	�I�I��� ��K��8���EF�R�z���K�K�I�4�K�8��m	�	��,	�	��3!�!��!F�F�sV�278M�$M�,L8�.AM�9M�=M�AM�8M�=M�M�279M�M	�M�M&