Arafath10 commited on
Commit
42ce81c
·
verified ·
1 Parent(s): a309ca7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +72 -5
main.py CHANGED
@@ -166,11 +166,78 @@ async def your_continuous_function(page: int,paginate: int,Tenant: str):
166
 
167
  train_the_model(data)
168
 
169
- return "model trained with page number: "+str(page)+" data count :"+str(data_count)
 
 
 
170
 
171
  @app.get("/get_latest_model_updated_time")
172
  async def model_updated_time():
173
- m_time_encoder = os.path.getmtime('encoders.joblib')
174
- m_time_model = os.path.getmtime('xgb_model.joblib')
175
- return {"base model created time ":datetime.datetime.fromtimestamp(m_time_encoder),
176
- "last model updated time":datetime.datetime.fromtimestamp(m_time_model)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  train_the_model(data)
168
 
169
+ return {"model trained details":{"page_number":page,"data_count":data_count}}
170
+
171
+
172
+
173
 
174
  @app.get("/get_latest_model_updated_time")
175
  async def model_updated_time():
176
+ try:
177
+ m_time_encoder = os.path.getmtime('encoders.joblib')
178
+ m_time_model = os.path.getmtime('xgb_model.joblib')
179
+ return {"base model created time ":datetime.datetime.fromtimestamp(m_time_encoder),
180
+ "last model updated time":datetime.datetime.fromtimestamp(m_time_model)}
181
+ except:
182
+ return {"no model found so first trained the model using data fecther"}
183
+
184
+
185
+
186
+
187
+
188
+ # Endpoint for making predictions
189
+ @app.post("/predict")
190
+ def predict(customer_name: str,
191
+ customer_address: str,
192
+ customer_phone: str,
193
+ customer_email: str,
194
+ cod:str,
195
+ weight: str,
196
+ pickup_address: str,
197
+ origin_city_name: str,
198
+ destination_city_name: str,
199
+ origin_country: str):
200
+
201
+ try:
202
+ # Load your trained model and encoders
203
+ xgb_model = load('xgb_model.joblib')
204
+ encoders = load('encoders.joblib')
205
+ except:
206
+ return {"no model found so first trained the model using data fecther"}
207
+
208
+
209
+ # Function to handle unseen labels during encoding
210
+ def safe_transform(encoder, column):
211
+ classes = encoder.classes_
212
+ return [encoder.transform([x])[0] if x in classes else -1 for x in column]
213
+
214
+ # Convert input data to DataFrame
215
+ input_data = {
216
+ 'customer_name': customer_name,
217
+ 'customer_address': customer_address,
218
+ 'customer_phone': customer_phone,
219
+ 'customer_email': customer_email,
220
+ 'cod': float(cod),
221
+ 'weight': float(weight),
222
+ 'origin_city.name':origin_city_name,
223
+ 'destination_city.name':destination_city_name
224
+ }
225
+ input_df = pd.DataFrame([input_data])
226
+
227
+ # Encode categorical variables using the same encoders used during training
228
+ for col in input_df.columns:
229
+ if col in encoders:
230
+ input_df[col] = safe_transform(encoders[col], input_df[col])
231
+
232
+ # Predict and obtain probabilities
233
+ pred = xgb_model.predict(input_df)
234
+ pred_proba = xgb_model.predict_proba(input_df)
235
+
236
+ # Output
237
+ predicted_status = "Unknown" if pred[0] == -1 else encoders['status.name'].inverse_transform([pred])[0]
238
+ probability = pred_proba[0][pred[0]] * 100 if pred[0] != -1 else "Unknown"
239
+
240
+ if predicted_status == "RETURN TO CLIENT":
241
+ probability = 100 - probability
242
+
243
+ return {"Probability": round(probability,2)}