Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import json
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
ct_load = pickle.load(open('ct.pkl', 'rb'))
|
7 |
+
xgb_load = pickle.load(open('xgb_classifier.pkl', 'rb'))
|
8 |
+
label_name_list = ['1', '2', '3']
|
9 |
+
|
10 |
+
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}'
|
11 |
+
one_data_str
|
12 |
+
|
13 |
+
def predict_one(one_data_str):
|
14 |
+
one_data_dict = json.loads(one_data_str)
|
15 |
+
one_df = pd.DataFrame([one_data_dict])
|
16 |
+
one_array = ct_load.transform(one_df)
|
17 |
+
pred_num = xgb_load.predict(one_array)[0]
|
18 |
+
pred_pro = max(xgb_load.predict_proba(one_array)[0])
|
19 |
+
pred_name = label_name_list[pred_num]
|
20 |
+
# 转为json字符串格式
|
21 |
+
result_dict = {'pred_pro':str(pred_pro),'pred_num':str(pred_num),'pred_name':pred_name }
|
22 |
+
result_json = json.dumps(result_dict)
|
23 |
+
|
24 |
+
return result_json
|
25 |
+
|
26 |
+
demo = gr.Interface(fn=predict_one,
|
27 |
+
inputs="text",
|
28 |
+
outputs="text",
|
29 |
+
examples=[one_data_str,one_data_str ],
|
30 |
+
)
|
31 |
+
|
32 |
+
# demo.launch(debug=True)
|
33 |
+
demo.launch()
|