import gradio as gr import matplotlib.pyplot as plt from sentiment import analyze_sentiment # 📌 生成簡單的信心度條狀圖 def plot_confidence(score): fig, ax = plt.subplots(figsize=(4, 1)) # 調整圖表尺寸 ax.barh(["信心度"], [score], color="blue") # 橫向條狀圖 ax.set_xlim([0, 1]) # 設定範圍為 0-1 ax.set_xticks([0, 0.25, 0.5, 0.75, 1]) # 設定刻度 ax.set_xlabel("信心度(百分比)") # 標題 return fig # 📌 建立 Gradio 介面 def create_ui(): with gr.Blocks(theme=gr.themes.Soft()) as iface: gr.Markdown("# 🎯 多語言情緒分析 AI") gr.Markdown("請輸入一段文字,AI 會分析其情緒,並提供信心度。") text_input = gr.Textbox(lines=3, placeholder="請輸入文本...", label="輸入文本") analyze_button = gr.Button("分析情緒") result_output = gr.Markdown(label="分析結果") plot_output = gr.Plot(label="信心度") # 📌 綁定按鈕功能 def process_analysis(text): result, confidence_score = analyze_sentiment(text) # 調用 API 進行分析 plot = plot_confidence(confidence_score) # 生成條狀圖 return result, plot analyze_button.click(process_analysis, inputs=text_input, outputs=[result_output, plot_output]) return iface