Hammad712 commited on
Commit
360682b
Β·
verified Β·
1 Parent(s): 9588eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -1,5 +1,5 @@
 
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
2
- import uvicorn
3
  import torchaudio
4
  import torch.nn.functional as F
5
  import torch
@@ -8,18 +8,39 @@ import onnxruntime as ort
8
  from huggingface_hub import hf_hub_download
9
  import os
10
 
11
- app = FastAPI(title="Pakistani LID AI Engine (Standalone)")
 
 
 
 
 
 
 
 
12
 
13
- print("πŸ“₯ Checking/Downloading ONNX Model from Hugging Face...")
14
- # Yeh line model ko cache kar legi, baar baar download nahi karegi
15
- model_path = hf_hub_download(repo_id="Hammad712/pakistani-lid-v3-sota", filename="pakistani_lid_v3.onnx")
16
 
17
- print("πŸš€ Loading ONNX Session for CPU...")
18
- session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  labels = ("balochi", "english", "pashto", "sindhi", "urdu")
21
  id2label = {i: label for i, label in enumerate(labels)}
22
 
 
 
 
23
  def predict_audio(audio_path):
24
  waveform, sr = torchaudio.load(audio_path)
25
  if waveform.shape[0] > 1: waveform = waveform.mean(dim=0, keepdim=True)
@@ -53,21 +74,29 @@ def predict_audio(audio_path):
53
  pred_id = np.argmax(probs, axis=1)[0]
54
  return id2label[pred_id], float(probs[0][pred_id])
55
 
 
 
 
56
  @app.post("/predict")
57
  async def predict_language(file: UploadFile = File(...)):
 
 
58
  if not file.filename.endswith(('.wav', '.mp3', '.m4a', '.ogg')):
 
59
  raise HTTPException(status_code=400, detail="Invalid audio format. Please upload wav, mp3, m4a, or ogg.")
60
 
61
  temp_audio_path = f"temp_{file.filename}"
62
  try:
63
- # File save karein
64
  with open(temp_audio_path, "wb") as buffer:
65
  buffer.write(await file.read())
66
 
67
- # Prediction lein
 
68
  lang, confidence = predict_audio(temp_audio_path)
 
69
 
70
- # Temp file delete kar dein
71
  os.remove(temp_audio_path)
72
 
73
  return {
@@ -76,10 +105,7 @@ async def predict_language(file: UploadFile = File(...)):
76
  "confidence": round(confidence * 100, 2)
77
  }
78
  except Exception as e:
 
79
  if os.path.exists(temp_audio_path):
80
  os.remove(temp_audio_path)
81
- raise HTTPException(status_code=500, detail=str(e))
82
-
83
- if __name__ == "__main__":
84
- print("✨ Server is LIVE at: http://localhost:8080")
85
- uvicorn.run(app, host="0.0.0.0", port=8080)
 
1
+ import logging
2
  from fastapi import FastAPI, UploadFile, File, HTTPException
 
3
  import torchaudio
4
  import torch.nn.functional as F
5
  import torch
 
8
  from huggingface_hub import hf_hub_download
9
  import os
10
 
11
+ # ==========================================
12
+ # 1. Setup Production Logging
13
+ # ==========================================
14
+ logging.basicConfig(
15
+ level=logging.INFO,
16
+ format="%(asctime)s [%(levelname)s] %(message)s",
17
+ handlers=[logging.StreamHandler()]
18
+ )
19
+ logger = logging.getLogger("LID_Engine")
20
 
21
+ app = FastAPI(title="Pakistani LID AI Engine (Production)")
 
 
22
 
23
+ # ==========================================
24
+ # 2. Model Initialization (Runs once on startup)
25
+ # ==========================================
26
+ logger.info("Initializing Application...")
27
+ try:
28
+ logger.info("Checking/Downloading ONNX Model from Hugging Face...")
29
+ model_path = hf_hub_download(repo_id="Hammad712/pakistani-lid-v3-sota", filename="pakistani_lid_v3.onnx")
30
+
31
+ logger.info("Loading ONNX Session for CPU...")
32
+ session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
33
+ logger.info("βœ… ONNX Session successfully loaded and ready!")
34
+ except Exception as e:
35
+ logger.error(f"❌ Failed to load model during startup: {e}")
36
+ raise e
37
 
38
  labels = ("balochi", "english", "pashto", "sindhi", "urdu")
39
  id2label = {i: label for i, label in enumerate(labels)}
40
 
41
+ # ==========================================
42
+ # 3. Core Inference Logic
43
+ # ==========================================
44
  def predict_audio(audio_path):
45
  waveform, sr = torchaudio.load(audio_path)
46
  if waveform.shape[0] > 1: waveform = waveform.mean(dim=0, keepdim=True)
 
74
  pred_id = np.argmax(probs, axis=1)[0]
75
  return id2label[pred_id], float(probs[0][pred_id])
76
 
77
+ # ==========================================
78
+ # 4. API Endpoints
79
+ # ==========================================
80
  @app.post("/predict")
81
  async def predict_language(file: UploadFile = File(...)):
82
+ logger.info(f"Received request for file: {file.filename}")
83
+
84
  if not file.filename.endswith(('.wav', '.mp3', '.m4a', '.ogg')):
85
+ logger.warning(f"Rejected invalid file type: {file.filename}")
86
  raise HTTPException(status_code=400, detail="Invalid audio format. Please upload wav, mp3, m4a, or ogg.")
87
 
88
  temp_audio_path = f"temp_{file.filename}"
89
  try:
90
+ # Save file
91
  with open(temp_audio_path, "wb") as buffer:
92
  buffer.write(await file.read())
93
 
94
+ # Predict
95
+ logger.info(f"Processing inference for {file.filename}...")
96
  lang, confidence = predict_audio(temp_audio_path)
97
+ logger.info(f"βœ… Prediction successful: {lang.upper()} ({confidence:.2%})")
98
 
99
+ # Cleanup
100
  os.remove(temp_audio_path)
101
 
102
  return {
 
105
  "confidence": round(confidence * 100, 2)
106
  }
107
  except Exception as e:
108
+ logger.error(f"❌ Error processing {file.filename}: {str(e)}", exc_info=True)
109
  if os.path.exists(temp_audio_path):
110
  os.remove(temp_audio_path)
111
+ raise HTTPException(status_code=500, detail="Internal Server Error")