Hammad712 commited on
Commit
5599463
Β·
verified Β·
1 Parent(s): e5de087

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -24,20 +24,23 @@ app.add_middleware(
24
  allow_headers=["*"],
25
  )
26
 
27
- # Load Model (Baked into the Docker image)
28
- MODEL_DIR = "local_model"
29
- MODEL_PATH = os.path.join(MODEL_DIR, "pakistani_lid_v3.onnx")
 
30
 
31
- logger.info("πŸš€ Loading pre-baked ONNX model...")
32
  try:
33
- # Check if files exist just in case
34
  if not os.path.exists(MODEL_PATH):
35
- raise FileNotFoundError(f"Model not found at {MODEL_PATH}")
 
 
36
 
37
- session = ort.InferenceSession(MODEL_PATH, providers=['CPUExecutionProvider'])
 
 
38
  logger.info("βœ… Engine is LIVE and Ready!")
39
  except Exception as e:
40
- logger.error(f"❌ Failed to load model: {e}")
41
  raise e
42
 
43
  labels = ("balochi", "english", "pashto", "sindhi", "urdu")
@@ -88,6 +91,7 @@ async def predict(file: UploadFile = File(...)):
88
  os.remove(temp_path)
89
  return {"success": True, "language": lang.upper(), "confidence": round(conf * 100, 2)}
90
  except Exception as e:
 
91
  if os.path.exists(temp_path): os.remove(temp_path)
92
  return {"success": False, "error": str(e)}
93
 
 
24
  allow_headers=["*"],
25
  )
26
 
27
+ # 🚨 Use Absolute Path
28
+ MODEL_PATH = "/app/local_model/pakistani_lid_v3.onnx"
29
+
30
+ logger.info(f"πŸš€ Attempting to load model from: {MODEL_PATH}")
31
 
 
32
  try:
 
33
  if not os.path.exists(MODEL_PATH):
34
+ # List files for debugging in logs if it fails
35
+ logger.error(f"Files in /app/local_model: {os.listdir('/app/local_model') if os.path.exists('/app/local_model') else 'Dir not found'}")
36
+ raise FileNotFoundError(f"Model file missing at {MODEL_PATH}")
37
 
38
+ # Load with mmap to save RAM
39
+ session_options = ort.SessionOptions()
40
+ session = ort.InferenceSession(MODEL_PATH, sess_options=session_options, providers=['CPUExecutionProvider'])
41
  logger.info("βœ… Engine is LIVE and Ready!")
42
  except Exception as e:
43
+ logger.error(f"❌ Initialization Error: {e}")
44
  raise e
45
 
46
  labels = ("balochi", "english", "pashto", "sindhi", "urdu")
 
91
  os.remove(temp_path)
92
  return {"success": True, "language": lang.upper(), "confidence": round(conf * 100, 2)}
93
  except Exception as e:
94
+ logger.error(f"Inference Error: {e}")
95
  if os.path.exists(temp_path): os.remove(temp_path)
96
  return {"success": False, "error": str(e)}
97