Spaces:
Sleeping
Sleeping
| import pickle | |
| import json | |
| import pandas as pd | |
| import gradio as gr | |
| ct_load = pickle.load(open('ct.pkl', 'rb')) | |
| xgb_load = pickle.load(open('xgb_classifier.pkl', 'rb')) | |
| label_name_list = ['1', '2', '3'] | |
| one_data_str = '{"Box_type": "光分箱", "Scenes": "【光分】箱体缺盖", "OBD_number": 1, "ONU_number": 1, "Budget": 440.0, "Average_price": 440.0, "Low_light_obd_number": 0, "Low_light_onu_number": 0, "Low_light_frequency": 0, "Low_light_ratio": 0.0, "Is_low_ligh_over_25": "否", "Is_low_ligh_over_5_users": "否", "Is_obd_all_low_light": "否", "break_off_frequency": 0, "break_off_duration": 0, "break_off_resources_number": 0, "Single_user_break_off_duration": 0.0, "label": 3}' | |
| one_data_str | |
| def predict_one(one_data_str): | |
| one_data_dict = json.loads(one_data_str) | |
| one_df = pd.DataFrame([one_data_dict]) | |
| one_array = ct_load.transform(one_df) | |
| pred_num = xgb_load.predict(one_array)[0] | |
| pred_pro = max(xgb_load.predict_proba(one_array)[0]) | |
| pred_name = label_name_list[pred_num] | |
| # 转为json字符串格式 | |
| result_dict = {'pred_pro':str(pred_pro),'pred_num':str(pred_num),'pred_name':pred_name } | |
| result_json = json.dumps(result_dict) | |
| return result_json | |
| demo = gr.Interface(fn=predict_one, | |
| inputs="text", | |
| outputs="text", | |
| examples=[one_data_str,one_data_str ], | |
| ) | |
| # demo.launch(debug=True) | |
| demo.launch() |