yxqo41main commited on
Commit
8ffd378
·
verified ·
1 Parent(s): 3ad3b12

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -37
main.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, HTTPException
2
- from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from scalar_fastapi import get_scalar_api_reference
5
  import json
@@ -182,6 +182,7 @@ async def get_quran_text(language: str, surah_number: int, ayah_number: int = No
182
  }
183
  file_name = edition_map.get(language, f"{language}.json")
184
  hf_url = f"{HF_DATASET_URL}/api_data/surah/{surah_number}/{file_name}"
 
185
 
186
  try:
187
  response = requests.get(hf_url, timeout=10)
@@ -265,47 +266,53 @@ async def get_reciter_audio(reciter_name: str, recitation_type: str, surah_numbe
265
  if recitation_type == "ayah-by-ayah" and reciter_name != "al-huzaifi":
266
  raise HTTPException(status_code=400, detail="Ayah-by-ayah only available for Al-Huzaifi")
267
 
268
- if USE_HF_CDN:
269
- surah_str_padded = str(surah_number).zfill(3)
270
- hf_url = f"{HF_DATASET_URL}/reciters/audio/{reciter_name}/{recitation_type}/{surah_str_padded}.mp3"
271
- return RedirectResponse(url=hf_url)
272
- else:
273
- surah_str_padded = str(surah_number).zfill(3)
274
- surah_str_normal = str(surah_number)
275
 
276
- audio_path = RECITERS_DIR / reciter_name / recitation_type / f"{surah_str_padded}.mp3"
277
- if audio_path.exists():
278
- return FileResponse(audio_path, media_type="audio/mpeg")
 
 
 
 
 
 
 
 
 
279
 
280
- audio_path = RECITERS_DIR / reciter_name / recitation_type / f"{surah_str_normal}.mp3"
 
 
 
 
 
 
 
 
 
281
  if audio_path.exists():
282
  return FileResponse(audio_path, media_type="audio/mpeg")
283
 
284
- old_dir_names = {
285
- "yasser-al-dossari": "yasser_ad-dossary/mp3",
286
- "abdul-baset": "abdul_baset",
287
- "khalil-al-husary": "khalil_al_husary",
288
- "siddiq-minshawi": "siddiq_minshawi"
289
- }
290
-
291
- if reciter_name in old_dir_names:
292
- old_base_dir = RECITERS_DIR / old_dir_names[reciter_name]
293
- if (old_base_dir / recitation_type).exists():
294
- audio_path = old_base_dir / recitation_type / f"{surah_str_padded}.mp3"
295
- if audio_path.exists():
296
- return FileResponse(audio_path, media_type="audio/mpeg")
297
- audio_path = old_base_dir / recitation_type / f"{surah_str_normal}.mp3"
298
- if audio_path.exists():
299
- return FileResponse(audio_path, media_type="audio/mpeg")
300
- else:
301
- audio_path = old_base_dir / f"{surah_str_padded}.mp3"
302
- if audio_path.exists():
303
- return FileResponse(audio_path, media_type="audio/mpeg")
304
- audio_path = old_base_dir / f"{surah_str_normal}.mp3"
305
- if audio_path.exists():
306
- return FileResponse(audio_path, media_type="audio/mpeg")
307
-
308
- raise HTTPException(status_code=404, detail="Audio file not found")
309
 
310
  if __name__ == "__main__":
311
  import uvicorn
 
1
  from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import FileResponse, JSONResponse, RedirectResponse, StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from scalar_fastapi import get_scalar_api_reference
5
  import json
 
182
  }
183
  file_name = edition_map.get(language, f"{language}.json")
184
  hf_url = f"{HF_DATASET_URL}/api_data/surah/{surah_number}/{file_name}"
185
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
186
 
187
  try:
188
  response = requests.get(hf_url, timeout=10)
 
266
  if recitation_type == "ayah-by-ayah" and reciter_name != "al-huzaifi":
267
  raise HTTPException(status_code=400, detail="Ayah-by-ayah only available for Al-Huzaifi")
268
 
269
+ surah_str_padded = str(surah_number).zfill(3)
270
+ surah_str_normal = str(surah_number)
 
 
 
 
 
271
 
272
+ # Try to serve from local files first
273
+ possible_local_paths = [
274
+ RECITERS_DIR / reciter_name / recitation_type / f"{surah_str_padded}.mp3",
275
+ RECITERS_DIR / reciter_name / recitation_type / f"{surah_str_normal}.mp3",
276
+ ]
277
+
278
+ old_dir_names = {
279
+ "yasser-al-dossari": "yasser_ad-dossary/mp3",
280
+ "abdul-baset": "abdul_baset",
281
+ "khalil-al-husary": "khalil_al_husary",
282
+ "siddiq-minshawi": "siddiq_minshawi"
283
+ }
284
 
285
+ if reciter_name in old_dir_names:
286
+ old_base_dir = RECITERS_DIR / old_dir_names[reciter_name]
287
+ if (old_base_dir / recitation_type).exists():
288
+ possible_local_paths.append(old_base_dir / recitation_type / f"{surah_str_padded}.mp3")
289
+ possible_local_paths.append(old_base_dir / recitation_type / f"{surah_str_normal}.mp3")
290
+ else:
291
+ possible_local_paths.append(old_base_dir / f"{surah_str_padded}.mp3")
292
+ possible_local_paths.append(old_base_dir / f"{surah_str_normal}.mp3")
293
+
294
+ for audio_path in possible_local_paths:
295
  if audio_path.exists():
296
  return FileResponse(audio_path, media_type="audio/mpeg")
297
 
298
+ # If not found locally and USE_HF_CDN is true, try to fetch from HF CDN
299
+ if USE_HF_CDN:
300
+ hf_url = f"{HF_DATASET_URL}/reciters/audio/{reciter_name}/{recitation_type}/{surah_str_padded}.mp3"
301
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
302
+ try:
303
+ # Use stream=True to avoid loading the entire file into memory
304
+ response = requests.get(hf_url, stream=True, headers=headers, timeout=10)
305
+ response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
306
+
307
+ # Return a StreamingResponse
308
+ return StreamingResponse(response.iter_content(chunk_size=8192),
309
+ media_type=response.headers.get('Content-Type', 'audio/mpeg'),
310
+ headers={"Content-Disposition": f"attachment; filename="{surah_str_padded}.mp3""})
311
+ except requests.exceptions.RequestException as e:
312
+ print(f"Failed to fetch from HF CDN: {e}")
313
+ # Fall through to 404 if HF CDN also fails
314
+
315
+ raise HTTPException(status_code=404, detail="Audio file not found")
 
 
 
 
 
 
 
316
 
317
  if __name__ == "__main__":
318
  import uvicorn