Spaces:
Running
Running
import gradio as gr | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
import torch | |
model_path = "modernbert.bin" | |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
tokenizer = AutoTokenizer.from_pretrained("answerdotai/ModernBERT-base") | |
model = AutoModelForSequenceClassification.from_pretrained("answerdotai/ModernBERT-base", num_labels=41) | |
model.load_state_dict(torch.load(model_path, map_location=device)) | |
model.to(device) | |
model.eval() | |
label_mapping = { | |
0: '13B', 1: '30B', 2: '65B', 3: '7B', 4: 'GLM130B', 5: 'bloom_7b', | |
6: 'bloomz', 7: 'cohere', 8: 'davinci', 9: 'dolly', 10: 'dolly-v2-12b', | |
11: 'flan_t5_base', 12: 'flan_t5_large', 13: 'flan_t5_small', | |
14: 'flan_t5_xl', 15: 'flan_t5_xxl', 16: 'gemma-7b-it', 17: 'gemma2-9b-it', | |
18: 'gpt-3.5-turbo', 19: 'gpt-35', 20: 'gpt4', 21: 'gpt4o', | |
22: 'gpt_j', 23: 'gpt_neox', 24: 'human', 25: 'llama3-70b', 26: 'llama3-8b', | |
27: 'mixtral-8x7b', 28: 'opt_1.3b', 29: 'opt_125m', 30: 'opt_13b', | |
31: 'opt_2.7b', 32: 'opt_30b', 33: 'opt_350m', 34: 'opt_6.7b', | |
35: 'opt_iml_30b', 36: 'opt_iml_max_1.3b', 37: 't0_11b', 38: 't0_3b', | |
39: 'text-davinci-002', 40: 'text-davinci-003' | |
} | |
def classify_text(text): | |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True) | |
inputs = {key: value.to(device) for key, value in inputs.items()} | |
with torch.no_grad(): | |
outputs = model(**inputs) | |
probabilities = torch.softmax(outputs.logits, dim=1)[0] | |
predicted_class = torch.argmax(probabilities).item() | |
confidence = probabilities[predicted_class].item() | |
if predicted_class == 24: | |
prediction_label = "β **Human Written**" | |
confidence_message = f"π **Confidence:** {confidence:.2f}" | |
if confidence > 0.8: | |
confidence_message += " (Highly Likely Human)" | |
else: | |
prediction_label = f"π€ **AI Generated by {label_mapping[predicted_class]}**" | |
confidence_message = f"π **Confidence:** {confidence:.2f}" | |
if confidence > 0.8: | |
confidence_message += " (Highly Likely AI)" | |
return f"**Result:**\n\n{prediction_label}\n\n{confidence_message}" | |
title = "π§ SzegedAI ModernBERT Text Detector" | |
description = ( | |
""" | |
**AI Detection Tool by SzegedAI** | |
**Detect AI-generated texts with precision.** This tool uses the new **ModernBERT** model, fine-tuned for machine-generated text detection, and able to detect 40 different models. | |
- **π€ Identify AI Models**: If detected as AI-generated, the system will reveal which LLM was responsible for the text generation. | |
- **β Human Verification**: If confidently human, the result will be marked with a **green checkmark**. | |
**Press the button below to classify your text!** | |
""" | |
) | |
iface = gr.Interface( | |
fn=classify_text, | |
inputs=gr.Textbox( | |
label="βοΈ Enter Text for Analysis", | |
placeholder="Type or paste your content here...", | |
lines=5, | |
elem_id="text_input_box" | |
), | |
outputs=gr.Textbox( | |
label="Detection Results", | |
lines=4, | |
elem_id="result_output_box" | |
), | |
title=title, | |
description=description, | |
theme="dark", | |
allow_flagging="never", | |
live=False, | |
submit_button="π― Analyze Now", | |
css=""" | |
#text_input_box, #result_output_box { | |
border-radius: 10px; | |
border: 2px solid #4CAF50; | |
font-size: 18px; | |
} | |
body { | |
background: #1E1E2F; | |
color: #E1E1E6; | |
font-family: 'Aptos', sans-serif; | |
padding: 20px; | |
} | |
.gradio-container { | |
border: 2px solid #4CAF50; | |
border-radius: 15px; | |
padding: 20px; | |
box-shadow: 0px 0px 20px rgba(0,255,0,0.6); | |
} | |
h1, h2 { | |
text-align: center; | |
font-size: 32px; | |
font-weight: bold; | |
} | |
""" | |
) | |
if __name__ == "__main__": | |
iface.launch(share=True) | |