a0ms1n/AI-Code-Detector_for-Competitive-Programming
7
1from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, AutoConfig, AutoModel2from datasets import Dataset, DatasetDict, Features, Sequence, ClassLabel, Value3import pandas as pd4import re5import torch6from Preprocess import *7 8model_path = "Model-V1.2"9tokenizer = AutoTokenizer.from_pretrained(model_path)10config = AutoConfig.from_pretrained(model_path)11model = AutoModelForSequenceClassification.from_pretrained(model_path, config=config)12labels = model.config.id2label13label2id = model.config.label2id14 15def preprocess(code):16 code = format_cpp(code)17 code = remove_comments(code)18 code = replace_preprocessor(code)19 # code = normalize_braces(code)20 code = strip_lines(code)21 return code22 23 24def eval(source):25 source = preprocess(source)26 inputs = tokenizer(27 source,28 truncation=True,29 padding='max_length',30 max_length=512,31 return_tensors='pt'32 )33 34 model.cpu()35 model.eval()36 inputs = {k: v.cpu() for k, v in inputs.items()}37 38 with torch.no_grad():39 outputs = model(**inputs)40 41 probs = torch.softmax(outputs.logits, dim=-1).detach().cpu().numpy()[0]42 pred_id = probs.argmax()43 # print("Label:", labels[pred_id], " | Score:", probs[pred_id])44 return labels[pred_id], f"{probs[label2id['AI']]*100:.2f} %"45 