File size: 1,232 Bytes
36e5180
ca12785
 
36e5180
ca12785
 
 
 
bb5b784
ca12785
 
398fb47
 
ca12785
398fb47
 
 
ca12785
398fb47
 
ca12785
398fb47
 
0a036bb
d3359ce
398fb47
 
 
 
 
 
5c87e0a
398fb47
 
ca12785
 
 
398fb47
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
from transformers import RobertaTokenizer, RobertaForSequenceClassification
import torch

# Load the model and tokenizer from the specified directory
model_path = './finetuned_roberta'
tokenizer = RobertaTokenizer.from_pretrained(model_path)
model = RobertaForSequenceClassification.from_pretrained(model_path)

# Define the prediction function
def classify_text(text):
    # Tokenize the input text
    inputs = tokenizer(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..."),
    outputs="json",
    title="GoalZero Endpoint",
    description="Enter some text and get the probability of the text being written by AI.",
)

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