Manveer commited on
Commit
08fd96b
·
1 Parent(s): 3c9596b

Add application file 11

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py CHANGED
@@ -203,6 +203,60 @@ async def predict_batch(request: POBatchRequest):
203
  logger.error(f"Prediction failed: {e}")
204
  raise HTTPException(status_code=500, detail=str(e))
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
  if __name__ == "__main__":
208
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
203
  logger.error(f"Prediction failed: {e}")
204
  raise HTTPException(status_code=500, detail=str(e))
205
 
206
+ @app.post("/predict/single", response_model=RiskAssessment)
207
+ async def predict_single(item: POItem):
208
+ try:
209
+ po_df = pd.DataFrame([item.dict()])
210
+
211
+ po_df.rename(columns={
212
+ "product_name": "Product_Name",
213
+ "quantity": "Quantity",
214
+ "delivery_date": "Delivery_Date",
215
+ "filename": "Filename",
216
+ "company_name": "Company_Name"
217
+ }, inplace=True)
218
+
219
+ processed_df = process_po(po_df, sku_df)
220
+ result = processed_df.iloc[0]
221
+
222
+ return {
223
+ "poid": result.get("POID", ""),
224
+ "product_name": result["Product_Name"],
225
+ "predicted_risk_label": result["predicted_risk_label"],
226
+ "missing_field_score_v2": result["missing_field_score_v2"],
227
+ "semantic_signal": result["semantic_signal"],
228
+ "delivery_lag_flag": result["delivery_lag_flag"],
229
+ "description_token_rarity_score": result["description_token_rarity_score"],
230
+ "filename_type_encoded": result["filename_type_encoded"]
231
+ }
232
+
233
+ except Exception as e:
234
+ logger.error(f"Single prediction failed: {e}")
235
+ raise HTTPException(status_code=500, detail=str(e))
236
+
237
+ @app.get("/health")
238
+ async def health_check():
239
+ """
240
+ Health check endpoint to verify API status and model readiness
241
+ """
242
+ try:
243
+ is_hf_space = "HUGGINGFACE_SPACE" in os.environ
244
+ return {
245
+ "status": "healthy",
246
+ "timestamp": datetime.now().isoformat(),
247
+ "environment": {
248
+ "is_huggingface_space": is_hf_space,
249
+ "space_id": os.getenv("SPACE_ID", "Not available"),
250
+ "python_version": f"{os.sys.version_info.major}.{os.sys.version_info.minor}.{os.sys.version_info.micro}"
251
+ },
252
+ "models": {
253
+ "sbert_loaded": sbert_model is not None,
254
+ "xgb_loaded": xgb_model is not None
255
+ }
256
+ }
257
+ except Exception as e:
258
+ logger.error(f"Health check failed: {e}")
259
+ raise HTTPException(status_code=500, detail=str(e))
260
 
261
  if __name__ == "__main__":
262
  uvicorn.run(app, host="0.0.0.0", port=7860)