File size: 1,220 Bytes
5dc0980
ce9a470
5dc0980
 
 
 
6065638
5dc0980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae0430f
 
 
ce9a470
ae0430f
5dc0980
6065638
ae0430f
6065638
 
ae0430f
6065638
ae0430f
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
28
29
30
31
32
33
34
35
36
37
#gr.Interface.load("models/hipnologo/gpt2-imdb-finetune").launch()
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification

def predict_review(text):
    # Specify the model name or path
    model_name = "hipnologo/gpt2-imdb-finetune"  

    # 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()

    sentiment = 'Positive' if predicted_class == 1 else 'Negative'

    # Create a Markdown string for the output
    result_md = f"Sentiment: {sentiment}"
    return result_md

iface = gr.Interface(
    fn=predict_review, 
    inputs=gr.components.Textbox(lines=7, placeholder="Enter text here..."), 
    outputs=gr.components.Textbox(),
    title="Sentiment Analysis",
    description="This application predicts the sentiment (Positive/Negative) of the input text using a fine-tuned GPT-2 model."
)
iface.launch()