Wintersmith commited on
Commit
2e96147
1 Parent(s): fcccd9b

Upload LLM detector app.py

Browse files
Files changed (1) hide show
  1. LLM detector app.py +51 -0
LLM detector app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # -- Sheet --
4
+
5
+ %pip install gradio
6
+
7
+ import numpy as np
8
+ import tensorflow as tf
9
+ from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
10
+ import gradio as gr
11
+ import re
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained("Wintersmith/LLM_generated_text_detector")
14
+ model = TFAutoModelForSequenceClassification.from_pretrained("Wintersmith/LLM_generated_text_detector")
15
+
16
+ def clean_text(text):
17
+ text = re.sub(r"[^A-Za-z0-9\s]", "", text)
18
+ text = re.sub(r"\s+", " ", text)
19
+ text = text.lower()
20
+ return text
21
+
22
+ def get_probabilities(input_text):
23
+ cleaned_text = clean_text(input_text)
24
+ model_input = tokenizer(cleaned_text, max_length=512, padding=True, truncation=True, return_tensors='tf')
25
+ model_input = dict(model_input)
26
+ logits_pred = model.predict(model_input)['logits']
27
+ probs = tf.nn.sigmoid(logits_pred)
28
+ probs_list = list(probs[0].numpy())
29
+ class_names = ["Written by student", "AI generated"]
30
+ return {class_names[i]: probs_list[i] for i in range(2)}
31
+
32
+ student_written_eassay = "Most schools allow students to have cell phones for safety, which seems unlikely to change as long as school shootings remain a common occurrence. But phones aren't just tools for emergencies; they can also be valuable tools in the classroom. If there's a word or concept a student doesn't understand, the student can find information instantly. Phones have calculators as well as spelling and grammar checks. Most importantly, phones allow students to communicate with one another and with experts in fields of interest. The question remains, however, whether the use of cell phones in school outweighs the distraction they cause."
33
+
34
+ ai_generated_essay = "In today’s digital age, the presence of smartphones in schools has sparked heated debates among educators, parents, and policymakers. While some argue that banning phones is essential for maintaining classroom focus, others emphasize the need to consider students’ sense of connection and adapt to the changing educational landscape."
35
+
36
+ description = """This is a Distilbert model fine-tuned on thousands of essays of various themes like 'Phones in school', 'Car-free cities' etc. <img src="https://image.khaleejtimes.com/?uuid=99d0d917-5420-4344-84d8-4fabd2578882&function=cropresize&type=preview&source=false&q=75&crop_w=0.99999&crop_h=0.75&x=0&y=0&width=1500&height=844" width=300px>"""
37
+
38
+ article = "This app is based on a Kaggle competition, learn more about it [here](https://www.kaggle.com/competitions/llm-detect-ai-generated-text)."
39
+
40
+ iface = gr.Interface(
41
+ fn=get_probabilities,
42
+ inputs="text",
43
+ outputs=gr.Label(),
44
+ title="Detect AI Generated Text - Identify which essay was written by a LLM",
45
+ examples = [student_written_eassay, ai_generated_essay],
46
+ description=description,
47
+ article=article,
48
+ )
49
+
50
+ iface.launch(share=True)
51
+