Spaces:
Sleeping
Sleeping
File size: 1,686 Bytes
856d4f7 2cc4dc9 1b8dd75 2cc4dc9 856d4f7 2cc4dc9 856d4f7 2cc4dc9 856d4f7 1b8dd75 856d4f7 1b8dd75 856d4f7 1b8dd75 |
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 46 47 48 49 50 |
import pandas as pd
from transformers import pipeline
import gradio as gr
# 載入 Hugging Face 的中文三分類情緒模型
classifier = pipeline("text-classification", model="uer/roberta-base-finetuned-dianping-chinese", tokenizer="uer/roberta-base-finetuned-dianping-chinese")
# 分析函式(接收 CSV)
def analyze_csv(file):
df = pd.read_csv(file)
# 檢查是否有 "text" 欄位
if "text" not in df.columns:
return "錯誤:CSV 檔案中必須包含 'text' 欄位。"
# 對每一列文字做情緒分析
results = []
for text in df["text"]:
result = classifier(str(text))[0]
label = result["label"]
score = round(result["score"], 4)
if label == "LABEL_0":
sentiment = "負向"
elif label == "LABEL_1":
sentiment = "中立"
elif label == "LABEL_2":
sentiment = "正向"
else:
sentiment = "未知"
results.append({"label": sentiment, "score": score})
# 加回原始 dataframe
df["情緒判斷"] = [r["label"] for r in results]
df["信心分數"] = [r["score"] for r in results]
# 輸出為新的 csv 檔案
output_file = "/tmp/output.csv"
df.to_csv(output_file, index=False)
return output_file
# Gradio 介面(接收檔案,回傳檔案)
gr.Interface(
fn=analyze_csv,
inputs=gr.File(label="上傳包含 'text' 欄位的 CSV 檔案", file_types=[".csv"]),
outputs=gr.File(label="下載標註後的 CSV 檔案"),
title="中文情緒分析系統(批次處理)",
description="上傳一份 CSV,系統會針對 'text' 欄做情緒分析,並下載結果。"
).launch() |