File size: 1,199 Bytes
5e20430
 
 
 
 
 
 
 
 
 
 
 
485d99a
5e20430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import pickle
import torch
import numpy as np
from transformers import BertTokenizer, BertModel
from sklearn.linear_model import LogisticRegression

# Load BERT tokenizer and model
tokenizer = BertTokenizer.from_pretrained('dslim/bert-large-NER')
bert_model = BertModel.from_pretrained('dslim/bert-large-NER')

# Load the trained Logistic Regression classifier
with open('bert_large_ner.pkl', 'rb') as model_file:
    classifier = pickle.load(model_file)

# Define function to preprocess and classify text
def classify_text(text):
    # Preprocess text and get BERT embeddings
    inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
    with torch.no_grad():
        outputs = bert_model(**inputs)
        embeddings = outputs.last_hidden_state[:, 0, :].numpy()

    # Predict using the classifier
    label = classifier.predict(embeddings)
    return label[0]

# Create the Gradio interface
iface = gr.Interface(
    fn=classify_text,
    inputs="text",
    outputs="text",
    title="Text Classification: Human or AI?",
    description="Enter a text to classify whether it's generated by a human or AI.",
)

# Launch the Gradio interface
iface.launch()