Upload 3 files
Browse files- Dockerfile +2 -2
- main.py +50 -26
- requirements.txt +1 -0
Dockerfile
CHANGED
|
@@ -11,11 +11,11 @@ COPY . /app
|
|
| 11 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
# Make port 8000 available to the world outside this container
|
| 14 |
-
EXPOSE
|
| 15 |
|
| 16 |
# Define environment variables
|
| 17 |
ENV NAME KalamalahQuranAPI
|
| 18 |
ENV USE_HF_CDN true
|
| 19 |
|
| 20 |
# Run uvicorn when the container launches
|
| 21 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "
|
|
|
|
| 11 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
# Make port 8000 available to the world outside this container
|
| 14 |
+
EXPOSE 8000
|
| 15 |
|
| 16 |
# Define environment variables
|
| 17 |
ENV NAME KalamalahQuranAPI
|
| 18 |
ENV USE_HF_CDN true
|
| 19 |
|
| 20 |
# Run uvicorn when the container launches
|
| 21 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
CHANGED
|
@@ -27,7 +27,10 @@ app.add_middleware(
|
|
| 27 |
|
| 28 |
# Environment configuration
|
| 29 |
USE_HF_CDN = os.getenv("USE_HF_CDN", "false").lower() == "true"
|
| 30 |
-
HF_DATASET_URL = "
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Base paths
|
| 33 |
BASE_DIR = Path(__file__).parent.parent
|
|
@@ -148,15 +151,20 @@ async def root():
|
|
| 148 |
async def get_surahs():
|
| 149 |
if USE_HF_CDN:
|
| 150 |
try:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
if data and data.get("code") == 200:
|
| 156 |
return JSONResponse(content=data["data"])
|
| 157 |
raise HTTPException(status_code=404, detail="Surahs data not found")
|
| 158 |
except Exception as e:
|
| 159 |
-
|
|
|
|
| 160 |
else:
|
| 161 |
if SURAHS_DATA and SURAHS_DATA.get("code") == 200:
|
| 162 |
return JSONResponse(content=SURAHS_DATA["data"])
|
|
@@ -181,13 +189,16 @@ async def get_quran_text(language: str, surah_number: int, ayah_number: int = No
|
|
| 181 |
"tafsir": "ar.muyassar.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 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
if ayah_number:
|
| 193 |
if data.get("code") == 200:
|
|
@@ -198,7 +209,8 @@ async def get_quran_text(language: str, surah_number: int, ayah_number: int = No
|
|
| 198 |
|
| 199 |
return JSONResponse(content=data.get("data", data))
|
| 200 |
except Exception as e:
|
| 201 |
-
|
|
|
|
| 202 |
else:
|
| 203 |
surah_dir = API_DATA_DIR / "surah" / str(surah_number)
|
| 204 |
if not surah_dir.exists():
|
|
@@ -239,8 +251,18 @@ async def get_quran_text(language: str, surah_number: int, ayah_number: int = No
|
|
| 239 |
async def get_mushaf_page(mushaf_name: str, page_number: int):
|
| 240 |
if USE_HF_CDN:
|
| 241 |
for ext in [".jpg", ".jpeg", ".png"]:
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
raise HTTPException(status_code=404, detail="Page not found")
|
| 245 |
else:
|
| 246 |
mushaf_dir = MUSHAFS_DIR / mushaf_name
|
|
@@ -298,25 +320,27 @@ async def get_reciter_audio(reciter_name: str, recitation_type: str, surah_numbe
|
|
| 298 |
# If not found locally and USE_HF_CDN is true, try to fetch from HF CDN
|
| 299 |
if USE_HF_CDN:
|
| 300 |
print(f"[DEBUG] USE_HF_CDN is True. HF_TOKEN present: {bool(HF_TOKEN)}")
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
print(f"[DEBUG] Attempting to fetch from HF CDN: {
|
| 304 |
try:
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
headers={"Content-Disposition": f"attachment; filename='{surah_str_padded}.mp3'"})
|
| 314 |
except requests.exceptions.HTTPError as e:
|
| 315 |
print(f"HTTP Error fetching from HF CDN: {e.response.status_code} - {e.response.text}")
|
| 316 |
raise HTTPException(status_code=e.response.status_code, detail=f"Failed to fetch audio from CDN: {e.response.status_code}")
|
| 317 |
except requests.exceptions.RequestException as e:
|
| 318 |
print(f"Network Error fetching from HF CDN: {e}")
|
| 319 |
raise HTTPException(status_code=500, detail=f"Network error fetching audio from CDN: {e}")
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 322 |
|
|
|
|
| 27 |
|
| 28 |
# Environment configuration
|
| 29 |
USE_HF_CDN = os.getenv("USE_HF_CDN", "false").lower() == "true"
|
| 30 |
+
HF_DATASET_URL = "Kalamalah/cdn" # This will be the repo_id
|
| 31 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 32 |
+
|
| 33 |
+
api = HfApi(token=HF_TOKEN)
|
| 34 |
|
| 35 |
# Base paths
|
| 36 |
BASE_DIR = Path(__file__).parent.parent
|
|
|
|
| 151 |
async def get_surahs():
|
| 152 |
if USE_HF_CDN:
|
| 153 |
try:
|
| 154 |
+
local_path = hf_hub_download(
|
| 155 |
+
repo_id=HF_DATASET_URL,
|
| 156 |
+
filename="api_data/surahs.json",
|
| 157 |
+
repo_type="dataset",
|
| 158 |
+
token=HF_TOKEN
|
| 159 |
+
)
|
| 160 |
+
with open(local_path, "r", encoding="utf-8") as f:
|
| 161 |
+
data = json.load(f)
|
| 162 |
if data and data.get("code") == 200:
|
| 163 |
return JSONResponse(content=data["data"])
|
| 164 |
raise HTTPException(status_code=404, detail="Surahs data not found")
|
| 165 |
except Exception as e:
|
| 166 |
+
print(f"Error fetching surahs from HF CDN: {e}")
|
| 167 |
+
raise HTTPException(status_code=500, detail=f"Failed to fetch surahs from CDN: {e}")
|
| 168 |
else:
|
| 169 |
if SURAHS_DATA and SURAHS_DATA.get("code") == 200:
|
| 170 |
return JSONResponse(content=SURAHS_DATA["data"])
|
|
|
|
| 189 |
"tafsir": "ar.muyassar.json"
|
| 190 |
}
|
| 191 |
file_name = edition_map.get(language, f"{language}.json")
|
|
|
|
|
|
|
| 192 |
|
| 193 |
try:
|
| 194 |
+
local_path = hf_hub_download(
|
| 195 |
+
repo_id=HF_DATASET_URL,
|
| 196 |
+
filename=f"api_data/surah/{surah_number}/{file_name}",
|
| 197 |
+
repo_type="dataset",
|
| 198 |
+
token=HF_TOKEN
|
| 199 |
+
)
|
| 200 |
+
with open(local_path, "r", encoding="utf-8") as f:
|
| 201 |
+
data = json.load(f)
|
| 202 |
|
| 203 |
if ayah_number:
|
| 204 |
if data.get("code") == 200:
|
|
|
|
| 209 |
|
| 210 |
return JSONResponse(content=data.get("data", data))
|
| 211 |
except Exception as e:
|
| 212 |
+
print(f"Error fetching Quran text from HF CDN: {e}")
|
| 213 |
+
raise HTTPException(status_code=500, detail=f"Failed to fetch Quran text from CDN: {e}")
|
| 214 |
else:
|
| 215 |
surah_dir = API_DATA_DIR / "surah" / str(surah_number)
|
| 216 |
if not surah_dir.exists():
|
|
|
|
| 251 |
async def get_mushaf_page(mushaf_name: str, page_number: int):
|
| 252 |
if USE_HF_CDN:
|
| 253 |
for ext in [".jpg", ".jpeg", ".png"]:
|
| 254 |
+
filename = f"mushafs/{mushaf_name}/{page_number}{ext}"
|
| 255 |
+
try:
|
| 256 |
+
local_path = hf_hub_download(
|
| 257 |
+
repo_id=HF_DATASET_URL,
|
| 258 |
+
filename=filename,
|
| 259 |
+
repo_type="dataset",
|
| 260 |
+
token=HF_TOKEN
|
| 261 |
+
)
|
| 262 |
+
return FileResponse(local_path, media_type=f"image/{ext[1:]}")
|
| 263 |
+
except Exception as e:
|
| 264 |
+
print(f"Error fetching mushaf page from HF CDN: {e}")
|
| 265 |
+
continue # Try next extension
|
| 266 |
raise HTTPException(status_code=404, detail="Page not found")
|
| 267 |
else:
|
| 268 |
mushaf_dir = MUSHAFS_DIR / mushaf_name
|
|
|
|
| 320 |
# If not found locally and USE_HF_CDN is true, try to fetch from HF CDN
|
| 321 |
if USE_HF_CDN:
|
| 322 |
print(f"[DEBUG] USE_HF_CDN is True. HF_TOKEN present: {bool(HF_TOKEN)}")
|
| 323 |
+
surah_str_padded = str(surah_number).zfill(3)
|
| 324 |
+
filename = f"reciters/audio/{reciter_name}/{recitation_type}/{surah_str_padded}.mp3"
|
| 325 |
+
print(f"[DEBUG] Attempting to fetch from HF CDN: {filename}")
|
| 326 |
try:
|
| 327 |
+
local_path = hf_hub_download(
|
| 328 |
+
repo_id=HF_DATASET_URL,
|
| 329 |
+
filename=filename,
|
| 330 |
+
repo_type="dataset",
|
| 331 |
+
token=HF_TOKEN
|
| 332 |
+
)
|
| 333 |
+
print(f"[DEBUG] Successfully downloaded {filename} to {local_path}")
|
| 334 |
+
return FileResponse(local_path, media_type="audio/mpeg")
|
|
|
|
| 335 |
except requests.exceptions.HTTPError as e:
|
| 336 |
print(f"HTTP Error fetching from HF CDN: {e.response.status_code} - {e.response.text}")
|
| 337 |
raise HTTPException(status_code=e.response.status_code, detail=f"Failed to fetch audio from CDN: {e.response.status_code}")
|
| 338 |
except requests.exceptions.RequestException as e:
|
| 339 |
print(f"Network Error fetching from HF CDN: {e}")
|
| 340 |
raise HTTPException(status_code=500, detail=f"Network error fetching audio from CDN: {e}")
|
| 341 |
+
except Exception as e:
|
| 342 |
+
print(f"Error fetching audio from HF CDN: {e}")
|
| 343 |
+
raise HTTPException(status_code=500, detail=f"Failed to fetch audio from CDN: {e}")
|
| 344 |
|
| 345 |
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 346 |
|
requirements.txt
CHANGED
|
@@ -4,3 +4,4 @@ scalar-fastapi>=1.0.0
|
|
| 4 |
python-multipart>=0.0.6
|
| 5 |
typing-inspect>=0.9.0
|
| 6 |
requests>=2.31.0
|
|
|
|
|
|
| 4 |
python-multipart>=0.0.6
|
| 5 |
typing-inspect>=0.9.0
|
| 6 |
requests>=2.31.0
|
| 7 |
+
huggingface_hub>=0.20.0
|