Spaces:
Running
Running
File size: 2,929 Bytes
7e16596 602ea6e 0aaced8 6cd6e80 c9e6f14 6cd6e80 c9e6f14 6cd6e80 ad81ace 7e16596 6cd6e80 0aaced8 602ea6e ad81ace adcba70 0aaced8 7e16596 602ea6e 593d24f ad81ace 0aaced8 9df415d 5b248ae 593d24f 9df415d 593d24f 9df415d 593d24f ad81ace a23057c 593d24f 602ea6e adcba70 a23057c 7e16596 6cd6e80 7e16596 602ea6e |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import gradio as gr
from sentiment import analyze_sentiment
from config import MODEL_OPTIONS
# 📌 Gradio 介面說明
intro_text = """
# 🎯 多語言情緒分析 AI
本應用使用 Hugging Face 的 `XLM-RoBERTa`、`BERT`、`DistilBERT` 模型來進行**多語言情緒分析**。
輸入任何語言的文本,AI 會自動判斷其**情緒分類(正向 / 中立 / 負向)**,並提供**信心度(%)**。
## 📌 **使用方式**
1️⃣ **輸入一句話或一段文本**(可輸入中文、英文、日文等)
2️⃣ **選擇 AI 模型(預設為 `XLM-RoBERTa`)**
3️⃣ **點擊「分析情緒」**
4️⃣ **查看結果,包括情緒分類 & 信心度**
"""
developer_info = """
## 👨💻 開發資訊
- **開發者**: 余彦志 (大宇 / ian)
- **模型來源**: [Hugging Face](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment)
- **技術棧**: `Gradio`、`FastAPI`、`Hugging Face API`
- **聯絡方式**: [[email protected]]
"""
# 📌 建立 Gradio 介面
def create_ui():
with gr.Blocks(theme=gr.themes.Soft()) as iface:
gr.Markdown(intro_text)
text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本", max_lines=5)
model_selector = gr.Dropdown(
choices=list(MODEL_OPTIONS.keys()),
value="🌎 多語言推特情緒分析 (XLM-RoBERTa)",
label="選擇 AI 模型"
)
analyze_button = gr.Button("分析情緒")
clear_button = gr.Button("清除")
result_display = gr.Markdown("等待輸入...", label="AI 回應") # 🚀 讓 `gr.Markdown()` 成為輸出區
# 📌 綁定按鈕功能
def process_analysis(text, model_name):
print("📢 [Debug] 按鈕被點擊")
print(f"📢 [Debug] 模型: {model_name}")
print(f"📢 [Debug] 輸入文本: {text}")
processing_message = "🔄 **AI 正在分析,請稍後...**"
return processing_message # 🚀 先回傳「分析中」的訊息
def analyze_and_return(text, model_name):
result, _ = analyze_sentiment(text, model_name)
print(f"📢 [Debug] 回傳結果: {result}")
return result # 🚀 直接回傳結果
analyze_button.click(
fn=process_analysis,
inputs=[text_input, model_selector],
outputs=[result_display]
).then( # 🚀 **等第一步執行完後再執行分析**
fn=analyze_and_return,
inputs=[text_input, model_selector],
outputs=[result_display]
)
clear_button.click(
lambda: "等待輸入...", # 🚀 讓狀態列回到初始值
inputs=[],
outputs=[result_display]
)
gr.Markdown(developer_info)
return iface
if __name__ == "__main__":
ui = create_ui()
ui.launch()
|