themanas021 commited on
Commit
0fcd6bb
1 Parent(s): 200f627

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import torch
4
+ import numpy as np
5
+ from transformers import BertTokenizer, BertModel
6
+ from sklearn.linear_model import LogisticRegression
7
+
8
+ # Load BERT tokenizer and model
9
+ tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
10
+ bert_model = BertModel.from_pretrained('bert-base-cased')
11
+
12
+ # Load the trained Logistic Regression classifier
13
+ with open('bert_cased.pkl', 'rb') as model_file:
14
+ classifier = pickle.load(model_file)
15
+
16
+ # Define function to preprocess and classify text
17
+ def classify_text(text):
18
+ # Preprocess text and get BERT embeddings
19
+ inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
20
+ with torch.no_grad():
21
+ outputs = bert_model(**inputs)
22
+ embeddings = outputs.last_hidden_state[:, 0, :].numpy()
23
+
24
+ # Predict using the classifier
25
+ label = classifier.predict(embeddings)
26
+ return label[0]
27
+
28
+ # Create the Gradio interface
29
+ iface = gr.Interface(
30
+ fn=classify_text,
31
+ inputs="text",
32
+ outputs="text",
33
+ title="Text Classification: Human or AI?",
34
+ description="Enter a text to classify whether it's generated by a human or AI.",
35
+ )
36
+
37
+ # Launch the Gradio interface
38
+ iface.launch()