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()