Download app.py from pengyuyan1222/instock_warning_GUI_cloud: direct link, hf CLI and curl.
- Browser
- Download file 8.09 kB
-
https://huggingface.co/spaces/pengyuyan1222/instock_warning_GUI_cloud/resolve/main/app.py
- Command line
-
hf download hf://spaces/pengyuyan1222/instock_warning_GUI_cloud/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pengyuyan1222/instock_warning_GUI_cloud/resolve/main/app.py
8.09 kB
| from order_sql_code import sql_query_uk_fr,sql_query_au_jp | |
| from instock_automate_warning_function import connect_database,fetch_data,update_origin_data,lack_of_stock_SKU_count,send_email_qq,email_body_write | |
| from settings import file_path_model_UK_FR,file_path_model_AU_JP,user,password,host,database,port,model_url_dict,email_content_dict,custom_css | |
| import numpy as np | |
| import time | |
| import gradio as gr | |
| import datetime | |
| import re | |
| selected_cn_list = [] | |
| def process_code(progress_text:str="", progress_state: gr.State = None,selected_options:list=[],to_recipients:list=[], cc_recipients:list=[]): | |
| progress_text = "" | |
| if not selected_options or not to_recipients: | |
| error_message = "请至少选择一个国家!\n" | |
| yield gr.update(value=""), gr.update(value=""), gr.update(value=error_message,visible=True) | |
| elif not to_recipients: | |
| error_message = "请填写收件人邮箱!" | |
| yield gr.update(value=""), gr.update(value=""), gr.update(value=error_message, visible=True) | |
| else: | |
| selected_cn_list.clear() | |
| selected_cn_list.extend(selected_options) | |
| warning_cn_value = selected_cn_list | |
| progress_text += "1.开始执行代码...\n" | |
| time.sleep(0.5) | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| # 一.获取每日最新订单数据 | |
| order_data_UK_FR_last_60_days = np.array([]) | |
| order_data_AU_JP_last_60_days = np.array([]) | |
| if "英国" in selected_cn_list or "法国" in selected_cn_list: | |
| order_data_UK_FR_last_60_days = fetch_data(sql_query_uk_fr,engine=connect_database(user, password, host, database, port)) | |
| elif "澳洲" in selected_cn_list or "日本" in selected_cn_list: | |
| order_data_AU_JP_last_60_days = fetch_data(sql_query_au_jp,engine=connect_database(user, password, host, database, port)) | |
| progress_text += "2.数据查询完毕...\n" | |
| time.sleep(0.5) | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| # 二、源数据更新 | |
| if order_data_UK_FR_last_60_days.size > 0 and not order_data_AU_JP_last_60_days.size > 0: | |
| file_path_model_order_data = { | |
| file_path_model_UK_FR: order_data_UK_FR_last_60_days | |
| } | |
| elif not order_data_UK_FR_last_60_days.size > 0 and order_data_AU_JP_last_60_days.size > 0: | |
| file_path_model_order_data = { | |
| file_path_model_AU_JP: order_data_AU_JP_last_60_days | |
| } | |
| elif order_data_UK_FR_last_60_days.size > 0 and order_data_AU_JP_last_60_days.size > 0: | |
| file_path_model_order_data = { | |
| file_path_model_UK_FR: order_data_UK_FR_last_60_days, | |
| file_path_model_AU_JP: order_data_AU_JP_last_60_days | |
| } | |
| else: | |
| file_path_model_order_data = {} # 这里可以根据需要处理没有数据的情况 | |
| update_origin_data(file_path_model_order_data) | |
| progress_text += "3.数据更新完毕...\n" | |
| time.sleep(0.5) | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| # 解析邮箱地址,使用分号拆分 | |
| # to_recipients_list = ";".join([email.strip() for email in to_recipients.split(';') if email.strip()]) | |
| # cc_recipients_list = ";".join([email.strip() for email in cc_recipients.split(';') if email.strip()]) | |
| to_recipients_list = [email.strip() for email in re.split(r'[;,]', to_recipients) if email.strip()] | |
| cc_recipients_list = [email.strip() for email in re.split(r'[;,]', cc_recipients) if email.strip()] | |
| # 三、发送预警邮件 | |
| file_path_model_target_sheet = { | |
| file_path_model_UK_FR: [], | |
| file_path_model_AU_JP: [] | |
| } | |
| for cn in selected_cn_list: | |
| if cn == '英国': | |
| file_path_model_target_sheet[file_path_model_UK_FR].append('英国') | |
| elif cn == '法国': | |
| file_path_model_target_sheet[file_path_model_UK_FR].append('法国') | |
| elif cn == '澳洲': | |
| file_path_model_target_sheet[file_path_model_AU_JP].append('澳洲') | |
| elif cn == '日本': | |
| file_path_model_target_sheet[file_path_model_AU_JP].append('日本') | |
| lack_of_stock_SKU_dict = lack_of_stock_SKU_count(file_path_model_target_sheet) | |
| progress_text += "4.1.缺货信息获取完毕...\n" | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| # 编写邮件内容 | |
| email_body = email_body_write(lack_of_stock_SKU_dict, model_url_dict) | |
| progress_text += "4.2.邮件内容编写完毕...\n" | |
| progress_text += email_body + "\n" | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| # 发送邮件 | |
| send_email_qq( | |
| subject=email_content_dict['subject'], | |
| body=email_body, | |
| to_recipients = to_recipients_list, | |
| cc_recipients = cc_recipients_list | |
| ) | |
| progress_text += f"4.3.【{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}】 邮件发送完毕...\n" | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| progress_text += "-------------------------\n" | |
| yield gr.update(value=progress_text), gr.update(value=warning_cn_value), gr.update(value="",visible=False) | |
| with gr.Blocks(css=custom_css) as demo: | |
| # 一、总标题 | |
| # gr.Markdown("# 📌 库存预警系统", elem_id="title") | |
| # gr.HTML('<h1 style="background-color:#8B0000; color:white; padding:5px; text-align: center; font-size: 22px; border-radius:10px; width: 92%; height:50px; margin-left: auto; margin-right: auto; display: flex; justify-content: center; align-items: center;">📌 库存预警系统</h1>') | |
| gr.HTML('<h1 style="background-color:#8B0000; color:white; padding:15px; text-align: center; font-size: 24px; border-radius:10px; text-align:center;">📌 库存预警系统</h1>') | |
| # 二、信息输入板块 | |
| with gr.Row(elem_id="container"): | |
| with gr.Column(elem_id="input_section"): | |
| # 标题 | |
| gr.Markdown("## 🔔 前置信息输入", elem_id="input_title") | |
| # 建立预警国家多选选项 | |
| checkbox_group = gr.CheckboxGroup( | |
| label = "请选择预警的国家(*必选,多选*)", | |
| choices = ["英国","法国","澳洲","日本"], | |
| elem_id = "custom-checkbox" | |
| ) | |
| to_recipients = gr.Textbox(label="邮件发送对象(*必填* 请以 邮箱1;邮箱2;邮箱3;... 的格式依次输入发送邮箱)",elem_id="email-textbox") | |
| cc_recipients = gr.Textbox(label="邮件抄送对象(*选填* 请以 邮箱1;邮箱2;邮箱3;... 的格式依次输入发送邮箱)",elem_id="custom-textbox") | |
| with gr.Column(elem_id="output_section"): | |
| # 三、信息输出板块 | |
| # 标题 | |
| gr.Markdown("## 📊 预警结果展示", elem_id="output_title") | |
| # 按钮点击后显示选择的国家 | |
| warning_cn = gr.Textbox(label="您选择预警的国家",elem_id="warning-cn-textbox") | |
| progress_textbox = gr.Textbox(label="库存预警进程明细",interactive=False,elem_id="warning-process-textbox") | |
| error_message = gr.Textbox(label="⚠ 预警报错",visible=False,elem_id="error_message") | |
| run_button = gr.Button("开始预警", elem_id="start-warning-button") | |
| run_button.click(process_code,inputs=[progress_textbox,gr.State(),checkbox_group,to_recipients,cc_recipients],outputs=[progress_textbox,warning_cn,error_message]) | |
| demo.launch(share=True) | |
| print(selected_cn_list) | |