Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
from transformers import pipeline
|
| 4 |
-
|
| 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 |
-
|
| 20 |
-
|
| 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()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
+
classifier = pipeline("sentiment-analysis", model="bert-base-uncased")
|
| 4 |
+
def classify_sentiment(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
result = classifier(text)[0]
|
| 6 |
+
return f"{result['label']} ({round(result['score'] * 100, 2)}%)"
|
| 7 |
+
gr.Interface(fn=classify_sentiment, inputs="text", outputs="text").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|