Spaces:
Sleeping
Sleeping
qiankun commited on
Commit ·
5e1197b
1
Parent(s): b2766e7
Refactor app to implement multilingual sentiment analysis using Hugging Face's pipeline, replacing the previous prompt enhancement functionality. Update Gradio interface and adjust requirements for dependencies.
Browse files- app.py +31 -18
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,29 +1,42 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
|
|
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
except Exception as e:
|
| 17 |
return f"Error: {str(e)}"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
prompt_input = gr.Textbox(label="Enter your
|
| 23 |
-
output = gr.Textbox(label="
|
| 24 |
-
|
| 25 |
-
btn
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
-
app.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# 直接引用 Hugging Face Hub 上的模型
|
| 5 |
+
MODEL_REPO = "tabularisai/multilingual-sentiment-analysis"
|
| 6 |
|
| 7 |
+
# 初始化情感分析 pipeline
|
| 8 |
+
classifier = pipeline("sentiment-analysis", model=MODEL_REPO)
|
| 9 |
|
| 10 |
+
def analyze_sentiment(text):
|
| 11 |
try:
|
| 12 |
+
results = classifier(text)
|
| 13 |
+
# 处理单条和多条输入
|
| 14 |
+
if isinstance(results, list):
|
| 15 |
+
output_lines = []
|
| 16 |
+
for result in results:
|
| 17 |
+
label = result.get('label', '')
|
| 18 |
+
score = result.get('score', 0)
|
| 19 |
+
output_lines.append(f"Label: {label}, Confidence: {score:.2%}")
|
| 20 |
+
return "\n".join(output_lines)
|
| 21 |
+
else:
|
| 22 |
+
label = results.get('label', '')
|
| 23 |
+
score = results.get('score', 0)
|
| 24 |
+
return f"Label: {label}, Confidence: {score:.2%}"
|
| 25 |
except Exception as e:
|
| 26 |
return f"Error: {str(e)}"
|
| 27 |
|
| 28 |
+
# Gradio Web 界面
|
| 29 |
+
with gr.Blocks(title="Multilingual Sentiment Analysis") as app:
|
| 30 |
+
gr.Markdown("## 🌍 Multilingual Sentiment Analysis\nAnalyze sentiment for texts in multiple languages using [tabularisai/multilingual-sentiment-analysis](https://huggingface.co/tabularisai/multilingual-sentiment-analysis).")
|
| 31 |
|
| 32 |
+
prompt_input = gr.Textbox(label="Enter your text here", lines=3, placeholder="Type or paste your sentence...")
|
| 33 |
+
output = gr.Textbox(label="Sentiment Result", lines=2)
|
| 34 |
+
btn = gr.Button("Analyze Sentiment")
|
| 35 |
+
btn.click(
|
| 36 |
+
fn=analyze_sentiment,
|
| 37 |
+
inputs=prompt_input,
|
| 38 |
+
outputs=output
|
| 39 |
+
)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
+
app.launch()
|
requirements.txt
CHANGED
|
@@ -2,5 +2,5 @@ torch
|
|
| 2 |
transformers
|
| 3 |
Pillow
|
| 4 |
|
| 5 |
-
gradio
|
| 6 |
-
huggingface_hub
|
|
|
|
| 2 |
transformers
|
| 3 |
Pillow
|
| 4 |
|
| 5 |
+
gradio
|
| 6 |
+
huggingface_hub
|