File size: 1,576 Bytes
64cf0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4194aec
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

import gradio as gr
from transformers import pipeline, DistilBertForSequenceClassification, DistilBertTokenizer

model     = DistilBertForSequenceClassification.from_pretrained("NigelTaruvinga/distilbert-imdb-sentiment")
tokenizer = DistilBertTokenizer.from_pretrained("NigelTaruvinga/distilbert-imdb-sentiment")

sentiment = pipeline(
    "text-classification",
    model=model,
    tokenizer=tokenizer,
    device=-1
)

def predict(text):
    if not text.strip():
        return "Please enter a review.", 0.0
    
    result     = sentiment(text)[0]
    label      = "Positive" if result["label"] == "LABEL_1" else "Negative"
    confidence = round(result["score"] * 100, 2)
    
    return label, confidence

app = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(
        lines=5,
        placeholder="Type or paste a movie review here...",
        label="Movie Review"
    ),
    outputs=[
        gr.Label(label="Sentiment"),
        gr.Number(label="Confidence (%)")
    ],
    title="IMDb Sentiment Classifier",
    description="Fine-tuned DistilBERT model trained on IMDb movie reviews. Enter any review to predict whether it is positive or negative.",
    examples=[
        ["This movie was absolutely fantastic. One of the best films I have ever seen."],
        ["Terrible film. Boring, slow, and a complete waste of time."],
        ["The visuals were stunning but the plot was confusing and hard to follow."]
    ],
    theme=gr.themes.Soft()
)

app.launch(server_name="0.0.0.0", server_port=7860, share=False)