Spaces:
Sleeping
Sleeping
File size: 1,472 Bytes
27ff273 7b74064 27ff273 7b74064 27ff273 7b74064 27ff273 |
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 |
import gradio as gr
from transformers import pipeline
def classifier(sentence):
classifier = pipeline(
"text-classification",
model="AirrStorm/DistilBERT-SST2",
tokenizer="AirrStorm/DistilBERT-SST2",
device=0
)
label_mapping = {"LABEL_0": "negative", "LABEL_1": "positive"}
result = classifier(sentence)
predicted_label = label_mapping[result[0]['label']]
return predicted_label # Should print "negative" or "positive"
examples = [
["This movie is amazing!"],
["I dislike this product."],
["The food was bland. The texture was fine but the taste was lacking."],
["The book was enjoyable. The story was good but predictable."],
["The movie was boring. The plot was dull and unoriginal."]
]
# Define the Gradio Interface
demo = gr.Interface(
fn=classifier,
inputs=gr.Textbox(
lines=4,
placeholder="Enter a sentence to analyze sentiment (e.g., 'I really liked this product.')",
label="Input Text"
),
outputs=gr.Textbox(
label="Predicted Sentiment"
),
title="Sentiment Analysis",
description="Classify the sentiment of the input text as positive or negative.",
theme="hugging-face", # Optional, you can experiment with other themes like 'huggingface'
allow_flagging="never", # Disable flagging if not needed
examples=examples # Add examples to make it easier for users
)
# Launch the interface
demo.launch()
|