CoolFace
Apppublic

pranavKHF/code_vulnerability_detection

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
inference.cpython-312.pyc85 linesDownload Raw Back to __pycache__
12<�i�����dZddlZddlmZddlmZddlZejje	ee3�jjj��ddlm
Z
Gd�d�Zd�Zed	k(re�yy)4zUInference module for vulnerability detection5Load trained models and make predictions�N)�RobertaTokenizer)�Path)�VulnerabilityCodeT5c�$�eZdZ		dd�Zd�Zd�Zy)�VulnerabilityDetectorc��tjtjj�rdnd�|_||_tj|�|_t|d��|_	tj||j��}|jj|d�|jj|j�|jj�td�dd	d6�|_y)N�cuda�cpu�)�7model_name�8num_labels)�map_location�model_state_dictzModel Loaded Successfullyz	Safe CodezVulnerable Code)r�)�torch�devicer	�is_available�9max_lengthr�from_pretrained�	tokenizerr�model�load�load_state_dict�to�eval�print�labels)�self�10model_pathrr�11checkpoints     �5D:\Code Vulnerability Detector CLEAN\src\inference.py�__init__zVulnerabilityDetector.__init__
s����l�l�U�Z�Z�-D�-D�-F�6�E�R���$���)�9�9�*�E���(�J�1�M��12��Z�Z�13����E�14��1516�"�"�:�.@�#A�B��1718�
�
�d�k�k�"��1920����	�)�*�� �21���c��|j|dddd��}|dj|j�}|dj|j�}tj�5|j22j
||�\}}ddd�d	j�}d	|j�}||j|||d	d	j�|d	d23j�d�d�S#1swY�qxYw)
z�Predict Vulnerability of Code Snippet24        25        Args : 26        code_snippet: String Containing source code27        28        Returns:29          dict with predictions, confidence and label30        31        �rT�pt)r�padding�32truncation�return_tensors�	input_ids�attention_maskNrr)�safe�33vulnerable)�34prediction�label�35confidence�
probabilities)	rrrr�no_gradr�predict�itemr)	r�code_snippet�inputsr*r+�predictions�probs�36pred_labelr0s	         r!r3zVulnerabilityDetector.predict#s������� ��� �37���;�'�*�*�4�;�;�7�	�� 0�1�4�4�T�[�[�A��
�]�]�_�	O�!%���!3�!3�I�~�!N��K��	O�!��^�(�(�*�38��1�X�j�)�.�.�0�39�%��[�[��,�$��a����(�(�*�#�A�h�q�k�.�.�0��	40�	41�	O�	O�s�' C7�7Dc�J�|D�cgc]}|j|���c}Scc}w)z&Analyze multiple code snippets at once)r3)r�
code_snippets�codes   r!�
analyze_batchz#VulnerabilityDetector.analyze_batchIs��/<�=�t����T�"�=�=��=s� N)zmodels/best_model.ptzSalesforce/codet5-baser%)�__name__�42__module__�__qualname__r"r3r=�r#r!rrs��"8�AD�43�,$44�L>r#rc��t�}ddd�ddd�ddd�dd	d�d45dd�dd
d�g}td�td�td�|D]�}td|d���td|ddd�d��|j|d�}td|d���td|dd���td|ddd���td|dd d�����y)!NzSafe Bounded Copyz�void copy_input(const char *input) {46        char buffer[32];47        strncpy(buffer, input, sizeof(buffer) - 1);48        buffer[sizeof(buffer) - 1] = '\0';49    })�namer<zSafe fgets Inputz�void read_input() {50        char buffer[64];51        if (fgets(buffer, sizeof(buffer), stdin) != NULL) {52            printf("%s", buffer);53        }54    }zSafe malloc usagez�void allocate() {55        char *buf = (char *)malloc(128);56        if (buf == NULL) {57            return;58        }59        strcpy(buf, "safe");60        free(buf);61    }zStack Buffer Overflowz[void copy_input(char *input) {62        char buffer[8];63        strcpy(buffer, input);64    }zInteger Overflowz�void allocate(int size) {65        char *buf = (char *)malloc(size * sizeof(char));66        if (buf == NULL) return;67        memset(buf, 'A', size + 10);68    }zUse After Freezivoid uaf() {69        char *buf = (char *)malloc(16);70        free(buf);71        strcpy(buf, "UAF");72    }z=73============================================================zTesting Vulnerability Detectionz<============================================================z74Test: rCzCode: r<�<z...zPrediction: r/zConfidence: r0z.2%z   - Safe: r1r,z   - Vulnerable: r-)rrr3)�detector�75test_cases�test�results    r!�test_inferencerIMs@��$�&�H�(�	�	76�'�	�	77�(�	�78	79�,�	�	80�'�	�	81�%�	�	82�Y4�J�n83�-��	�84+�,�	�&�M��	O��
���f���'�(�
��t�F�|�C�R�(�)��-�.��!�!�$�v�,�/��
��V�G�_�-�.�/�
��V�L�1�#�6�7�8�
��F�?�3�F�;�C�@�A�B�
�!�&��"9�,�"G��!L�M�N�	Or#�__main__)�__doc__r�transformersr�pathlibr�sys�path�append�str�__file__�parent�	src.modelrrrIr>rAr#r!�<module>rUsn��,�
�)��85�������D��N�)�)�0�0�7�7�8�9�)�?>�?>�BJO�X�z����r#