Spaces:
Sleeping
Sleeping
Chang Che Wei
commited on
Commit
·
e29b14f
1
Parent(s):
5f3b31f
initial gradio demo
Browse files- gradio_app.py +29 -0
- requirements.txt +3 -0
- runtime.txt +1 -0
gradio_app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 1. 建立情緒分析管線
|
| 6 |
+
sent_cls = pipeline(
|
| 7 |
+
task="sentiment-analysis",
|
| 8 |
+
model="uer/roberta-base-finetuned-jd-binary-chinese" # 雙分類
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# 2. 推論函式
|
| 12 |
+
def classify(text: str):
|
| 13 |
+
if not text.strip():
|
| 14 |
+
return {"error": "請輸入文字"}
|
| 15 |
+
res = sent_cls(text)[0]
|
| 16 |
+
return {"label": res["label"], "score": round(res["score"], 4)}
|
| 17 |
+
|
| 18 |
+
# 3. Gradio 介面
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=classify,
|
| 21 |
+
inputs=gr.Textbox(lines=4, placeholder="輸入評論…"),
|
| 22 |
+
outputs="json",
|
| 23 |
+
title="中文情緒分析 Demo",
|
| 24 |
+
description="RoBERTa 中文二分類情緒分析(positive/negative)"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
# localhost:7860 預覽
|
| 29 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.43.0
|
| 2 |
+
torch>=2.2.0
|
| 3 |
+
gradio>=4.25.0
|
runtime.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
python-3.11
|