Team Ai
Apppublic

aravagarwal/CodeCloakPII

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
gradiodemo.cpython-310.pyc49 linesDownload Raw Back to __pycache__
1o

2��eS�@s�ddlmZddlZddlmZddlmZddlm	Zddl3mZmZm
Z
mZddlmZmZddlmZmZmZdd	lmZmZd4d�ZdZe�����Ze�d
�e�d�e�d�e���ejedd�Z Wd�n1sswYe�d�e���ej!e"e�#��gd�dd�Z$Wd�n1s�wYe�d�e�%d���e�&d�Z'e���ej(ddd�Z)Wd�n1s�wYe�d�e�&d�Z*gZ+gZ,e-e�D]/Z.ejdd��Z/ejdd�Z0e+�1e0�ej(dd�Z2e,�1e2�Wd�n	1�swYq�e3de4e,��e3d e4e+��d!d"�Z5d#d$�Z6e'j7e6e e$ge)gd%�e'j7e5e e$ge+d%�d&d'�Z8e*j7e8e+e,d%�Wd�n	1�sNwYe�%d(��ke�d)�d*d+�Z9e���9e�:��e�d,�ejdd�Z;e�d-�ejdd�Z<Wd�n	1�s�wYej(dd�Z=Wd�n	1�s�wYej&d.dd�Z>e>j7e9e$e ge<e=e;gd/�Wd�n	1�s�wYWd�n	1�s�wYej?dd0�dS)1�)�deepcopyN)�OperatorConfig)�Differ)�5difference)�6MAX_LENGTH�MAX_STRINGS_IN_SAMPLE�analyzer�7anonymizer)�generate_constant_id�generate_hash_id)� syntax_aware_replace_with_lambda�visualize_tree_sitter_matches�TREE_SITTER_QUERY_NAME_TO_QUERY)�anonymize_string�analyze_stringcCsBddg}|D]	}t||tt�}qdg}|D]	}t||tt�}q|S)N�(comment) @comment� (string_content) @string_content�(identifier) @identifier)rr8rr)�source_text�queries�query�text_to_redact�queries2�r�[/home/arava/update_feedback_data/text_pii_removal/anoninterface/anoninterface/gradiodemo.py�visualize_redactsrz�9# My name is Inigo Montoya10Luke = 111Luke += 112Jane = 113Jane += 214"""15Monty16Python17Holy18Grail19"""20real_user_string = "My name is John Smith"21 22z9## Welcome to CodeCloak, an Interactive PII Removal Tool.zjWe will first pick out the sections of code we want to anonymize using Tree-Sitter, an incremental parser.z6Please place the Python code you want to analyze here:zSource Text)�value�labelz0Please select the elements you want to identify:)�comment�string�nameT)�choicesr�multiselectz}You can either **analyze** text to see what the tool detects as PII, or **anonymize** to get a diff of what the tool changes.�AnalyzezLocate useful sectionsz23Simple Vis)r�combine_adjacentz7Here are the strings we shall run our NER tool against:zIdentify PII��visibleF�inputs�outputscCs`t||�\}}tttdd�|D���dd�d�}t|�add�|D�tjdd�gtt|�S)	NcSsg|]24}|dj�d��qS)r�utf8)�text�decode��.0�elemrrr�25<listcomp>bsz6wrap_visualize_tree_sitter_matches.<locals>.<listcomp>cSst|�S�N)�len)�xrrr�<lambda>bsz4wrap_visualize_tree_sitter_matches.<locals>.<lambda>)�keycSsg|]	}tj|dd��qS)Tr%)�gr�Coder,rrrr/esFr%)	r
�sorted�list�setr1�TOTAL_ITEM_COUNTr5r6r)�inputv�26query_list�
labelled_text�capture_list�elems_to_capturerrr�"wrap_visualize_tree_sitter_matches`s"(r@cCst||�\}}|Sr0)r
)r;r<r=r>rrr�#wrap_visualize_tree_sitter_matches2gsrA)�fnr'r(cgs��tdd�|D��}tjdgdd�g|tjdd�gt|}tt|��D](}t||�dkrCt||�}tj|dd�||<|Vq&tjdd�||<|Vq&|dS)NcSsg|]}|dkr|�qS)�rr,rrrr/rsz(wrap_model_inference.<locals>.<listcomp>)zNo PIINTr%Fr)r1r5�HighlightedTextr�ranger)�input_textboxes�num_good_boxes�iterative_boxesr.�analysisrrr�wrap_model_inferenceqs�,rJ�	AnonymizezQNow, we will apply the redactions to these matches, and return the redacted text:cCsvddg}dg}dd�}|}|D]}t|}t||||vrtntt�}qtj|dd�tj|||�ddd�tj|dd�fS)	NrrrcSst�}dd�|�||�D�S)NcSs0g|]}|dd�|ddkr|dndf�qS)�Nr� r)r-�tokenrrrr/�s"��z9wrap_final_output.<locals>.diff_texts.<locals>.<listcomp>)r�compare)�text1�text2�drrr�27diff_texts�s28�z%wrap_final_output.<locals>.diff_textsTr%)r&r$)rrr29rrr5r6rD)r<�inp�queries_constant�queries_hashrSrrrrr�wrap_final_output�s0rWzBefore:zAfter:zAnonymize and Compare)r'r()�share)@�copyr�gradior5�presidio_anonymizerr�difflibr�	code_diffr�
anoninterfacerrrr	�anoninterface.redactionr30r�anoninterface.treeutilsrr
r�anoninterface.identificationrrr�simple_example�Blocks�demo�Markdown�Rowr6rT�Dropdownr8�keysr<�Tab�Button�btnrD�stageoneoutput�btn2rF�output_highlightsrE�i�lookatme�t�append�output_t�printr1r@rA�clickrJrW�Column�initial_text�31final_text�32final_diff�btn3�launchrrrr�<module>s�
33343536�3738�394041�424344���=4546474849�����r