Upload 3 files
Browse files- New Text Document.txt +4 -0
- app_Text.py +53 -0
- best_model (3).pth +3 -0
New Text Document.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
nltk
|
| 4 |
+
gradio
|
app_Text.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import re
|
| 5 |
+
import nltk
|
| 6 |
+
from nltk.tokenize import word_tokenize
|
| 7 |
+
from nltk.corpus import stopwords
|
| 8 |
+
from nltk.stem import WordNetLemmatizer
|
| 9 |
+
|
| 10 |
+
# Download necessary NLTK data
|
| 11 |
+
nltk.download('punkt')
|
| 12 |
+
nltk.download('stopwords')
|
| 13 |
+
nltk.download('wordnet')
|
| 14 |
+
|
| 15 |
+
# Preprocessing function
|
| 16 |
+
def preprocess(text):
|
| 17 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text).lower()
|
| 18 |
+
tokens = word_tokenize(text)
|
| 19 |
+
stop_words = set(stopwords.words('english'))
|
| 20 |
+
tokens = [word for word in tokens if word not in stop_words]
|
| 21 |
+
lemmatizer = WordNetLemmatizer()
|
| 22 |
+
tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 23 |
+
return ' '.join(tokens)
|
| 24 |
+
|
| 25 |
+
# Load tokenizer and model
|
| 26 |
+
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
|
| 27 |
+
model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
|
| 28 |
+
model.load_state_dict(torch.load('best_model (3).pth', map_location=torch.device('cpu')))
|
| 29 |
+
model.eval()
|
| 30 |
+
|
| 31 |
+
# Prediction function
|
| 32 |
+
def classify_essay(text):
|
| 33 |
+
cleaned_text = preprocess(text)
|
| 34 |
+
inputs = tokenizer(cleaned_text, return_tensors='pt', truncation=True, padding=True, max_length=100)
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
outputs = model(**inputs)
|
| 37 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 38 |
+
predicted_class = torch.argmax(probs, dim=1).item()
|
| 39 |
+
labels = ["Human-Written", "AI-Generated"]
|
| 40 |
+
return {labels[0]: float(probs[0][0]), labels[1]: float(probs[0][1])}
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=classify_essay,
|
| 45 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste your essay here..."),
|
| 46 |
+
outputs=gr.Label(num_top_classes=2),
|
| 47 |
+
title="Essay Authorship Classifier",
|
| 48 |
+
description="Detect whether an essay is AI-generated or human-written using a fine-tuned DistilBERT model."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the app
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
iface.launch()
|
best_model (3).pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:26f322627d12fd41a5c7f11adc4534c70b45196ff791d425323b17c242433669
|
| 3 |
+
size 267861862
|