File size: 2,064 Bytes
36e5180
ca12785
 
36e5180
34531ec
 
 
 
 
 
 
 
 
 
bb5b784
ca12785
34531ec
 
 
 
 
 
 
 
24955d5
 
 
 
 
ca12785
398fb47
 
 
ca12785
398fb47
 
ca12785
398fb47
 
0a036bb
d3359ce
398fb47
 
 
 
34531ec
 
 
 
398fb47
34531ec
 
398fb47
ca12785
 
 
34531ec
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
from transformers import RobertaTokenizer, RobertaForSequenceClassification
import torch

# Define available models
model_options = {
    "GoalZero/aidetection-ada-v0.2": "GoalZero/aidetection-ada-v0.2",
    "GoalZero/aidetection-ada-v0.1": "GoalZero/aidetection-ada-v0.1"
}

# Initialize tokenizer and model with the default model
default_model = model_options["GoalZero/aidetection-ada-v0.2"]
tokenizer = RobertaTokenizer.from_pretrained(default_model)
model = RobertaForSequenceClassification.from_pretrained(default_model)

# Define the prediction function
def classify_text(text, model_choice):
    global model, tokenizer  # Access the global model and tokenizer variables
    
    # Check if the model needs to be changed
    if model_choice != model.name_or_path:
        model = RobertaForSequenceClassification.from_pretrained(model_choice)
        tokenizer = RobertaTokenizer.from_pretrained(model_choice)
    
    # Remove periods and new lines from the input text
    cleaned_text = text.replace('.', '').replace('\n', ' ')
    
    # Tokenize the cleaned input text
    inputs = tokenizer(cleaned_text, return_tensors='pt', padding=True, truncation=True, max_length=128)
    
    # Get the model's prediction
    with torch.no_grad():
        outputs = model(**inputs)
    
    # Apply softmax to get probabilities
    probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
    
    # Get the probability of the class '1'
    prob_1 = probabilities[0][1].item()
    
    return {"Probability of being AI": prob_1}

# Create the Gradio interface
iface = gr.Interface(
    fn=classify_text,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter text here..."),
        gr.Dropdown(choices=list(model_options.keys()), value=default_model, label="Select Model")
    ],
    outputs="json",
    title="GoalZero Ada Model Selector",
    description="Enter text to get the probability of it being AI-written. Select a model version to use.",
)

# Launch the app
if __name__ == "__main__":
    iface.launch(share=True)