alan918727 commited on
Commit
15fdf16
·
verified ·
1 Parent(s): df7478f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -54,52 +54,55 @@ def get_features_single(formula):
54
 
55
  def predict_features(formula):
56
  try:
57
- print(f"Received formula: {formula}") # 打印接收到的化学式
 
 
 
 
 
 
 
58
  df = get_features_single(formula)
59
- print(f"Generated features dataframe:\n{df.head()}") # 打印生成的特征数据
 
60
 
61
  X_user = df.iloc[:, 2:].fillna(0)
62
- print(f"Input features for model:\n{X_user.head()}") # 打印输入特征
63
 
64
- # 加载模型
65
  model_path = "saved_model/lgbm_model.pkl"
66
  loaded_model = joblib.load(model_path)
67
- print("Model loaded successfully.") # 确认模型加载成功
68
 
69
- # 加载编码器和映射
70
  label_encoder_path = "saved_model/label_encoder.pkl"
71
  label_encoder = joblib.load(label_encoder_path)
72
- print("Label encoder loaded successfully.")
73
 
 
74
  mapping_path = "saved_model/layer_group_mapping.pkl"
75
  layer_group_mapping = joblib.load(mapping_path)
76
- print("Layer group mapping loaded successfully.")
77
 
78
- # 模型预测
79
  prediction_probs = loaded_model.predict_proba(X_user)[0]
80
- print(f"Prediction probabilities: {prediction_probs}")
81
 
82
- # 获取 Top 5
83
  top_5_indices = np.argsort(prediction_probs)[-5:][::-1]
84
- print(f"Top 5 indices: {top_5_indices}")
85
-
86
  top_5_numbers = label_encoder.inverse_transform(top_5_indices)
87
- print(f"Top 5 layer group numbers: {top_5_numbers}")
88
-
89
  top_5_names = [layer_group_mapping.get(num, "Unknown Layer Group") for num in top_5_numbers]
90
- print(f"Top 5 layer group names: {top_5_names}")
91
-
92
  top_5_probs = prediction_probs[top_5_indices]
 
 
93
  top_5_results = [
94
  [num, name, f"{prob:.2%}"]
95
  for num, name, prob in zip(top_5_numbers, top_5_names, top_5_probs)
96
  ]
97
- print(f"Top 5 results: {top_5_results}")
98
 
 
99
  prediction = loaded_model.predict(X_user)
 
 
100
  decoded_prediction = label_encoder.inverse_transform(prediction)[0]
 
 
101
  layer_group_name = layer_group_mapping.get(decoded_prediction, "Unknown Layer Group")
102
- print(f"Final prediction: {layer_group_name}")
103
 
104
  return {
105
  "Formula": formula,
@@ -108,9 +111,7 @@ def predict_features(formula):
108
  "Top 5 Predictions": top_5_results
109
  }
110
  except Exception as e:
111
- print(f"Error in predict_features: {str(e)}")
112
- return {"Error": str(e)}
113
-
114
 
115
 
116
  # Define a more visually appealing Gradio interface
@@ -133,7 +134,10 @@ def gradio_ui():
133
 
134
  def update_output(formula):
135
  result = predict_features(formula)
136
- return result["Predicted Layer Group Name"], result["Top 5 Predictions"]
 
 
 
137
 
138
  predict_button.click(fn=update_output, inputs=[formula_input], outputs=[final_prediction, top_5_table])
139
 
 
54
 
55
  def predict_features(formula):
56
  try:
57
+ # 验证化学式的有效性
58
+ try:
59
+ comp = Composition(formula) # 如果无效会抛出异常
60
+ _ = comp.get_reduced_formula_and_factor() # 进一步验证
61
+ except Exception:
62
+ return {"Error": "Invalid chemical formula. Please check your input and try again."}
63
+
64
+ # Generate features for the input formula
65
  df = get_features_single(formula)
66
+ if df.empty:
67
+ return {"Error": "Unable to generate features. Please check your input and try again."}
68
 
69
  X_user = df.iloc[:, 2:].fillna(0)
 
70
 
71
+ # Load saved model
72
  model_path = "saved_model/lgbm_model.pkl"
73
  loaded_model = joblib.load(model_path)
 
74
 
75
+ # Load saved LabelEncoder
76
  label_encoder_path = "saved_model/label_encoder.pkl"
77
  label_encoder = joblib.load(label_encoder_path)
 
78
 
79
+ # Load Layer Group Mapping
80
  mapping_path = "saved_model/layer_group_mapping.pkl"
81
  layer_group_mapping = joblib.load(mapping_path)
 
82
 
83
+ # Predict probabilities
84
  prediction_probs = loaded_model.predict_proba(X_user)[0]
 
85
 
86
+ # Get top 5 predictions
87
  top_5_indices = np.argsort(prediction_probs)[-5:][::-1]
 
 
88
  top_5_numbers = label_encoder.inverse_transform(top_5_indices)
 
 
89
  top_5_names = [layer_group_mapping.get(num, "Unknown Layer Group") for num in top_5_numbers]
 
 
90
  top_5_probs = prediction_probs[top_5_indices]
91
+
92
+ # Prepare top 5 results as list of lists
93
  top_5_results = [
94
  [num, name, f"{prob:.2%}"]
95
  for num, name, prob in zip(top_5_numbers, top_5_names, top_5_probs)
96
  ]
 
97
 
98
+ # Predict using the loaded model
99
  prediction = loaded_model.predict(X_user)
100
+
101
+ # Decode Layer Group Number
102
  decoded_prediction = label_encoder.inverse_transform(prediction)[0]
103
+
104
+ # Map to Layer Group Name
105
  layer_group_name = layer_group_mapping.get(decoded_prediction, "Unknown Layer Group")
 
106
 
107
  return {
108
  "Formula": formula,
 
111
  "Top 5 Predictions": top_5_results
112
  }
113
  except Exception as e:
114
+ return {"Error": f"An unexpected error occurred: {str(e)}"}
 
 
115
 
116
 
117
  # Define a more visually appealing Gradio interface
 
134
 
135
  def update_output(formula):
136
  result = predict_features(formula)
137
+ if "Error" in result:
138
+ # 返回错误消息到 Gradio 界面
139
+ return f"Error: {result['Error']}", []
140
+ return result.get("Predicted Layer Group Name", "Unknown"), result.get("Top 5 Predictions", [])
141
 
142
  predict_button.click(fn=update_output, inputs=[formula_input], outputs=[final_prediction, top_5_table])
143