Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import gradio as grfrom bert_explainer import analyze_text, analyze_image# ⛳ 分析文字def predict_text(text, mode):result = analyze_text(text=text, explain_mode=mode)return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])# ⛳ 分析圖片def predict_image(file_path, mode):with open(file_path, "rb") as f:result = analyze_image(f.read(), explain_mode=mode)return result['status'], f"{result['confidence']}%", ', '.join(result['suspicious_keywords'])# 🧩 文字分析介面text_api = gr.Interface(fn=predict_text,inputs=[gr.Textbox(lines=3, placeholder="請輸入訊息", label="輸入文字"),gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")],outputs=[gr.Textbox(label="判斷結果"),gr.Textbox(label="置信度"),gr.Textbox(label="可疑詞彙")],title="詐騙文字分析",flagging_mode="never")# 🧩 圖片分析介面image_api = gr.Interface(fn=predict_image,inputs=[gr.Image(type="filepath", label="上傳圖片"),gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")],outputs=[gr.Textbox(label="判斷結果"),gr.Textbox(label="置信度"),gr.Textbox(label="可疑詞彙")],title="詐騙圖片分析",flagging_mode="never")# ✅ 這是 Hugging Face Space 預設會尋找的變數,會觸發自動啟動 + 文件頁demo = gr.TabbedInterface(interface_list=[text_api, image_api],tab_names=["文字模式", "圖片模式"])# ✅ 加上這段,Hugging Face 才能啟動 Spaceif __name__ == "__main__":demo.queue().launch()123456789101112131415161718192021222324252627282930313233343536import gradio as grfrom bert_explainer import analyze_text, analyze_image# 🧠 預測函式:供 UI 和 API 共用def predict_text(text, mode):result = analyze_text(text=text, explain_mode=mode)return result["status"], f"{result['confidence']}%", ", ".join(result["suspicious_keywords"])def predict_image(file_path, mode):with open(file_path, "rb") as f:result = analyze_image(f.read(), explain_mode=mode)return result["status"], f"{result['confidence']}%", ", ".join(result["suspicious_keywords"])# 🧩 Gradio UI:兩個 Tab(文字 & 圖片)with gr.Blocks() as demo:with gr.Tab("文字模式"):text_input = gr.Textbox(lines=3, label="輸入文字")text_mode = gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")text_btn = gr.Button("提交")text_output1 = gr.Textbox(label="判斷結果")text_output2 = gr.Textbox(label="置信度")text_output3 = gr.Textbox(label="可疑詞彙")text_btn.click(fn=predict_text, inputs=[text_input, text_mode], outputs=[text_output1, text_output2, text_output3])with gr.Tab("圖片模式"):image_input = gr.Image(type="filepath", label="上傳圖片")image_mode = gr.Radio(["cnn", "bert", "both"], value="cnn", label="分析模式")image_btn = gr.Button("提交")image_output1 = gr.Textbox(label="判斷結果")image_output2 = gr.Textbox(label="置信度")image_output3 = gr.Textbox(label="可疑詞彙")image_btn.click(fn=predict_image, inputs=[image_input, image_mode], outputs=[image_output1, image_output2, image_output3])# ✅ 讓 Gradio 支援 API 路由 + 互動功能if __name__ == "__main__":demo.queue().launch()
app.py+23-43