|
|
|
|
|
"""
|
|
|
Created on Mon Aug 25 14:10:33 2025
|
|
|
|
|
|
@author: shuangshuang
|
|
|
"""
|
|
|
|
|
|
from transformers import pipeline
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
classifier = pipeline(
|
|
|
"sentiment-analysis",
|
|
|
model="nlptown/bert-base-multilingual-uncased-sentiment",
|
|
|
device=-1,
|
|
|
)
|
|
|
|
|
|
|
|
|
label_map = {
|
|
|
"1 star": "very bad",
|
|
|
"2 stars": "bad",
|
|
|
"3 stars": "neutral",
|
|
|
"4 stars": "good",
|
|
|
"5 stars": "very good",
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def predict(text):
|
|
|
text = text.strip()
|
|
|
if not text:
|
|
|
return "Please enter some text."
|
|
|
result = classifier(text)[0]
|
|
|
label = result["label"]
|
|
|
score = result["score"]
|
|
|
mapped = label_map[label]
|
|
|
return f"Prediction: {mapped}\nConfidence: {score:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
interface = gr.Interface(
|
|
|
fn=predict,
|
|
|
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
|
|
|
outputs="text",
|
|
|
title="Sentiment Analysis",
|
|
|
description="Enter a sentence to analyze its sentiment.",
|
|
|
examples=["I love this product!", "This is terrible."],
|
|
|
)
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
interface.launch()
|
|
|
|