Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Define pipeline
|
| 12 |
+
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 13 |
+
|
| 14 |
+
# Prediction function
|
| 15 |
+
def predict_sentiment(text):
|
| 16 |
+
if not text.strip():
|
| 17 |
+
return "Please enter some text."
|
| 18 |
+
result = classifier(text)[0]
|
| 19 |
+
label = result['label']
|
| 20 |
+
score = round(result['score'], 4)
|
| 21 |
+
return f"Sentiment: {label} (Confidence: {score})"
|
| 22 |
+
|
| 23 |
+
# Gradio UI
|
| 24 |
+
interface = gr.Interface(
|
| 25 |
+
fn=predict_sentiment,
|
| 26 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter movie review..."),
|
| 27 |
+
outputs="text",
|
| 28 |
+
title="IMDB Movie Review Sentiment Analysis with BERT",
|
| 29 |
+
description="This demo uses BERT to predict sentiment on IMDB-like reviews. Model: nlptown/bert-base-multilingual-uncased-sentiment"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
interface.launch()
|