ma4389 commited on
Commit
9b56bd1
·
verified ·
1 Parent(s): cf04285

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -53
app.py CHANGED
@@ -1,53 +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()
 
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_tab')
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()