Spaces:
Sleeping
Sleeping
Chang Che Wei
commited on
Commit
·
9143c0c
1
Parent(s):
a00fca0
add gradio entry file
Browse files
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)
|