Spaces:
Sleeping
Sleeping
| 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) | |