Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit_option_menu import option_menu
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import datasets
|
6 |
+
import argparse
|
7 |
+
from typing import Tuple
|
8 |
+
import transformers
|
9 |
+
import torch
|
10 |
+
from torch.utils.data import Dataset
|
11 |
+
import matplotlib as plt
|
12 |
+
import random
|
13 |
+
from tqdm import tqdm
|
14 |
+
import pandas as pd
|
15 |
+
from huggingface_hub import login
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from typing import Callable, Dict, List, Tuple, Union
|
18 |
+
import csv
|
19 |
+
from timeit import default_timer as timer
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
def load_tokenizer(tokenizer_name:str)->object:
|
26 |
+
"""
|
27 |
+
Function to load the tokenizer by the model's name
|
28 |
+
Args:
|
29 |
+
- tokenizer_name -> the name of the tokenizerto download
|
30 |
+
Returns:
|
31 |
+
- tokenizer -> returns respectively the model and the tokenizer
|
32 |
+
"""
|
33 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained("Salesforce/codet5p-770m")
|
34 |
+
|
35 |
+
|
36 |
+
return tokenizer
|
37 |
+
|
38 |
+
|
39 |
+
def load_model(model_name:str)->object:
|
40 |
+
"""
|
41 |
+
Function for model loading
|
42 |
+
Args:
|
43 |
+
- model_name -> the name of the model
|
44 |
+
Returns:
|
45 |
+
- model,tokenizer -> returns respectively the model and the tokenizer
|
46 |
+
"""
|
47 |
+
|
48 |
+
print(f'Loading model {model_name}...')
|
49 |
+
|
50 |
+
|
51 |
+
model_kwargs = {}
|
52 |
+
|
53 |
+
model_kwargs.update(dict( torch_dtype=torch.bfloat16))
|
54 |
+
transformers.T5EncoderModel._keys_to_ignore_on_load_unexpected = ["decoder.*"]
|
55 |
+
model_encoder = transformers.T5EncoderModel.from_pretrained("Salesforce/codet5p-770m", **model_kwargs)
|
56 |
+
|
57 |
+
print("---MODEL LOADED---")
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
return model_encoder
|
62 |
+
|
63 |
+
class stylometer_classifier(torch.nn.Module):
|
64 |
+
def __init__(self,pretrained_encoder,dimensionality):
|
65 |
+
super(stylometer_classifier, self).__init__()
|
66 |
+
self.modelBase = pretrained_encoder
|
67 |
+
self.pre_classifier = torch.nn.Linear(dimensionality, 768, dtype=torch.bfloat16)
|
68 |
+
self.activation = torch.nn.ReLU()
|
69 |
+
self.dropout = torch.nn.Dropout(0.2)
|
70 |
+
self.classifier = torch.nn.Linear(768, 1, dtype=torch.bfloat16)
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
def forward(self, input_ids, padding_mask):
|
76 |
+
output_1 = self.modelBase(input_ids=input_ids, attention_mask=padding_mask)
|
77 |
+
hidden_state = output_1[0]
|
78 |
+
#Here i take only the cls token representation for further classification
|
79 |
+
cls_output = hidden_state[:, 0]
|
80 |
+
pooler = self.pre_classifier(cls_output)
|
81 |
+
afterActivation = self.activation(pooler)
|
82 |
+
pooler_after_act = self.dropout(afterActivation)
|
83 |
+
output = torch.sigmoid(self.classifier(pooler_after_act))
|
84 |
+
|
85 |
+
if output>=0.07:
|
86 |
+
return {"my_class":"It's a Human!",
|
87 |
+
"prob":output}
|
88 |
+
else:
|
89 |
+
return {"my_class":"It's an LLM!",
|
90 |
+
"prob":output}
|
91 |
+
|
92 |
+
|
93 |
+
return output
|
94 |
+
|
95 |
+
def adapt_model(model:object, dim:int=1024) -> object:
|
96 |
+
"""
|
97 |
+
This function returns the model with a classification head
|
98 |
+
"""
|
99 |
+
newModel = stylometer_classifier(model,dimensionality=dim)
|
100 |
+
|
101 |
+
return newModel
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
def main():
|
108 |
+
parser = argparse.ArgumentParser()
|
109 |
+
parser.add_argument('--model_name', type=str, default="Salesforce/codet5p-770m")
|
110 |
+
parser.add_argument('--path_checkpoint1', type=str, default="checkpoint.bin")
|
111 |
+
|
112 |
+
args = parser.parse_args()
|
113 |
+
|
114 |
+
|
115 |
+
model_name = args.model_name
|
116 |
+
checkpoint1 = args.path_checkpoint1
|
117 |
+
|
118 |
+
|
119 |
+
DEVICE = "cpu"
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
#load tokenizer
|
125 |
+
tokenizer = load_tokenizer(model_name)
|
126 |
+
print("tokenizer loaded!")
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
#loading model and tokenizer for functional translation
|
133 |
+
model = load_model(model_name)
|
134 |
+
|
135 |
+
#adding classification head to the model
|
136 |
+
model = adapt_model(model, dim=model.shared.embedding_dim)
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
model.load_state_dict(torch.load(checkpoint1,map_location='cpu'))
|
142 |
+
model = model.eval()
|
143 |
+
st.title("Human-AI stylometer - Multilingual_multiprovenance")
|
144 |
+
text = st.text_area("insert your code here")
|
145 |
+
button = st.button("send")
|
146 |
+
if button or text:
|
147 |
+
input = tokenizer([text])
|
148 |
+
out= model(torch.tensor(input.input_ids),torch.tensor(input.attention_mask))
|
149 |
+
#st.json(out)
|
150 |
+
st.write(out["my_class"])
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
if __name__ == '__main__':
|
156 |
+
main()
|