import gradio as gr def interpret_pred(pred): low_bond = -6.748472 high_bound = 6.7176056 result = "IA" if pred.argmax(dim=-1).item() == 1 else "Humain" pred_value = pred[0][1].item() interpreted_pred = (pred_value - low_bond) / (high_bound - low_bond) is_ai_percent = round(100 * interpreted_pred) return result, is_ai_percent def main(text_sentence): import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification from transformers import Trainer, TrainingArguments, EarlyStoppingCallback barthez_tokenizer = AutoTokenizer.from_pretrained("moussaKam/barthez") model = AutoModelForSequenceClassification.from_pretrained("Anvil-ML/detecteur-ia") input_ids = torch.tensor( [barthez_tokenizer.encode(text_sentence, add_special_tokens=True)] ) predict = model.forward(input_ids)[0] result = ( "Résultat : {}.\nCe texte a {}% de chances d'avoir été généré par de l'IA" .format(interpret_pred(predict)[0], interpret_pred(predict)[1]) ) return result iface = gr.Interface(fn=main, inputs="text", outputs="text") iface.launch()