File size: 962 Bytes
83d0a05
5dc0980
 
 
 
 
d900c5f
5dc0980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a967e1a
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
import gradio as gr
#gr.Interface.load("models/hipnologo/gpt2-imdb-finetune").launch()
from transformers import AutoTokenizer, AutoModelForSequenceClassification

def predict_review(text):
    # Specify the model name or path
    model_name = "hipnologo/gpt2-imdb-finetune"  # Replace with your model name on the Hugging Face model hub

    # Load your model and tokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(model_name)

    # encoding the input text
    input_ids = tokenizer.encode(text, return_tensors="pt")

    # getting the logits 
    output = model(input_ids)
    logits = output.logits

    # getting the predicted class
    predicted_class = logits.argmax(-1).item()

    return f"The sentiment predicted by the model is: {'Positive' if predicted_class == 1 else 'Negative'}"

iface = gr.Interface(fn=predict_review, inputs="textbox", outputs="text")
iface.launch()