Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -248,5 +248,88 @@ def classify_email(json_input):
|
|
| 248 |
return f"is_spam:{result.get("is_spam")}\nspam_reason:{result.get("spam_reason")}"
|
| 249 |
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
return f"is_spam:{result.get("is_spam")}\nspam_reason:{result.get("spam_reason")}"
|
| 249 |
|
| 250 |
|
| 251 |
+
# ===================== 核心处理函数(模拟邮件分析) =====================
|
| 252 |
+
def classify_email(sender: str, subject: str, body: str) -> tuple[str, str, str]:
|
| 253 |
+
email = {
|
| 254 |
+
"sender": sender,
|
| 255 |
+
"subject": subject,
|
| 256 |
+
"body": body
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
result = compiled_graph.invoke({
|
| 260 |
+
"email": email,
|
| 261 |
+
"is_spam": None,
|
| 262 |
+
"spam_reason": None,
|
| 263 |
+
"email_category": None,
|
| 264 |
+
"draft_response": None,
|
| 265 |
+
"messages": []
|
| 266 |
+
})
|
| 267 |
+
return result.get("is_spam"), result.get("spam_reason"), result.get("email_category")
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# ===================== Gradio 表单界面构建 =====================
|
| 271 |
+
with gr.Blocks(title="邮件分析表单", theme=gr.themes.Soft()) as demo:
|
| 272 |
+
# 标题说明
|
| 273 |
+
gr.Markdown("### 📧 邮件垃圾信息分析表单")
|
| 274 |
+
gr.Markdown("请输入以下3项信息,提交后自动分析邮件是否为垃圾邮件")
|
| 275 |
+
|
| 276 |
+
# 表单区域:3个文本输入框
|
| 277 |
+
with gr.Row(): # 行布局,可选:改为 gr.Column() 垂直布局
|
| 278 |
+
with gr.Column(scale=1):
|
| 279 |
+
sender_input = gr.Textbox(
|
| 280 |
+
label="1. 发件人邮箱地址",
|
| 281 |
+
placeholder="例如:john.smith@gmail.com",
|
| 282 |
+
lines=1,
|
| 283 |
+
max_lines=1
|
| 284 |
+
)
|
| 285 |
+
with gr.Column(scale=2):
|
| 286 |
+
subject_input = gr.Textbox(
|
| 287 |
+
label="2. 邮件主题",
|
| 288 |
+
placeholder="例如:预约会议 / 新年祝福",
|
| 289 |
+
lines=1,
|
| 290 |
+
max_lines=1
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
# 第三个文本框:多行输入(邮件内容)
|
| 294 |
+
content_input = gr.Textbox(
|
| 295 |
+
label="3. 邮件内容",
|
| 296 |
+
placeholder="例如:我想预约下周二的咨询会议 / 感谢2025年的辛苦付出",
|
| 297 |
+
lines=5,
|
| 298 |
+
max_lines=10
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# 提交按钮
|
| 302 |
+
submit_btn = gr.Button("📤 提交分析", variant="primary", size="lg")
|
| 303 |
+
|
| 304 |
+
# 分割线
|
| 305 |
+
# gr.Divider()
|
| 306 |
+
|
| 307 |
+
# 结果展示区域(结构化输出)
|
| 308 |
+
gr.Markdown("### 📝 分析结果")
|
| 309 |
+
with gr.Row():
|
| 310 |
+
is_spam_output = gr.Textbox(label="是否垃圾邮件", interactive=False)
|
| 311 |
+
category_output = gr.Textbox(label="邮件分类", interactive=False)
|
| 312 |
+
reason_output = gr.Textbox(label="判定原因", lines=3, interactive=False)
|
| 313 |
+
|
| 314 |
+
# 绑定按钮与处理函数
|
| 315 |
+
submit_btn.click(
|
| 316 |
+
fn=classify_email,
|
| 317 |
+
inputs=[sender_input, subject_input, content_input], # 3个输入框
|
| 318 |
+
outputs=[is_spam_output, reason_output, category_output] # 3个输出框
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# 重置按钮(可选)
|
| 322 |
+
reset_btn = gr.Button("🔄 清空输入")
|
| 323 |
+
reset_btn.click(
|
| 324 |
+
fn=lambda: ("", "", "", "", "", ""), # 重置所有输入输出
|
| 325 |
+
inputs=[],
|
| 326 |
+
outputs=[sender_input, subject_input, content_input, is_spam_output, reason_output, category_output]
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
# ===================== 启动界面 =====================
|
| 330 |
+
if __name__ == "__main__":
|
| 331 |
+
# server_name="0.0.0.0" 允许局域网访问,server_port 自定义端口
|
| 332 |
+
demo.launch()
|
| 333 |
+
|
| 334 |
+
# demo = gr.Interface(fn=classify_email, inputs="text", outputs="text")
|
| 335 |
+
# demo.launch()
|