File size: 1,594 Bytes
fe11590
5e1197b
fe11590
5e1197b
 
fe11590
5e1197b
 
d5556df
5e1197b
d5556df
5e1197b
 
 
 
 
 
 
 
 
 
 
 
 
d5556df
 
 
5e1197b
 
 
e382d3a
5e1197b
 
 
 
 
 
 
 
fe11590
 
5e1197b
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
import gradio as gr
from transformers import pipeline

# η›΄ζŽ₯引用 Hugging Face Hub δΈŠηš„ζ¨‘εž‹
MODEL_REPO = "tabularisai/multilingual-sentiment-analysis"

# εˆε§‹εŒ–ζƒ…ζ„Ÿεˆ†ζž pipeline
classifier = pipeline("sentiment-analysis", model=MODEL_REPO)

def analyze_sentiment(text):
    try:
        results = classifier(text)
        # ε€„η†ε•ζ‘ε’Œε€šζ‘θΎ“ε…₯
        if isinstance(results, list):
            output_lines = []
            for result in results:
                label = result.get('label', '')
                score = result.get('score', 0)
                output_lines.append(f"Label: {label}, Confidence: {score:.2%}")
            return "\n".join(output_lines)
        else:
            label = results.get('label', '')
            score = results.get('score', 0)
            return f"Label: {label}, Confidence: {score:.2%}"
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Web η•Œι’
with gr.Blocks(title="Multilingual Sentiment Analysis") as app:
    gr.Markdown("## 🌍 Multilingual Sentiment Analysis\nAnalyze sentiment for texts in multiple languages using [tabularisai/multilingual-sentiment-analysis](https://huggingface.co/tabularisai/multilingual-sentiment-analysis).")

    prompt_input = gr.Textbox(label="Enter your text here", lines=3, placeholder="Type or paste your sentence...")
    output = gr.Textbox(label="Sentiment Result", lines=2)
    btn = gr.Button("Analyze Sentiment")
    btn.click(
        fn=analyze_sentiment,
        inputs=prompt_input,
        outputs=output
    )

if __name__ == "__main__":
    app.launch()