File size: 1,368 Bytes
3eccb5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ui
from transformers import pipeline

# 1. Load the sentiment analysis pipeline
# (Hugging Face Spaces will cache this so it only loads once on startup)
pipe = pipeline(
    "text-classification", model="tabularisai/multilingual-sentiment-analysis"
)


# 2. Define the prediction function
def analyze_sentiment(text):
    if not text.strip():
        return "Please enter some text to analyze."

    # Run the pipeline
    result = pipe(text)[0]

    # Extract label and score
    label = result["label"]
    score = result["score"]

    # Return a cleanly formatted string
    return f"Prediction: {label} | Confidence: {score:.2%}"


# 3. Create the Gradio Interface
demo = ui.Interface(
    fn=analyze_sentiment,
    inputs=ui.Textbox(
        lines=3, placeholder="Enter text here...", label="Input Text"
    ),
    outputs=ui.Textbox(label="Sentiment Analysis Result"),
    title="Multilingual Sentiment Analysis",
    description="Enter text in various languages to detect the underlying sentiment using the `tabularisai/multilingual-sentiment-analysis` model.",
    examples=[
        ["I love this product! It's amazing and works perfectly."],
        ["Ce produit est terrible, je déteste ça."],
        ["Este producto es increíble y funciona a la perfección."],
    ],
)

# 4. Launch the app
if __name__ == "__main__":
    demo.launch()