Upload 3 files
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse, StreamingResponse, Response
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from scalar_fastapi import get_scalar_api_reference
|
|
@@ -8,6 +8,38 @@ import requests
|
|
| 8 |
import threading
|
| 9 |
from pathlib import Path
|
| 10 |
from huggingface_hub import HfApi, hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI(
|
| 13 |
title="Kalamalah Quran API",
|
|
@@ -175,6 +207,7 @@ async def root():
|
|
| 175 |
"endpoints": {
|
| 176 |
"quran": "/api/quran/{language}/{surah_number}[/{ayah_number}]",
|
| 177 |
"surahs_list": "/api/surahs",
|
|
|
|
| 178 |
"reciters": "/api/reciters",
|
| 179 |
"reciter": "/api/reciters/{reciter_name}",
|
| 180 |
"audio": "/api/{reciter_name}/{type}/{surah_number}",
|
|
@@ -207,6 +240,61 @@ async def get_surahs():
|
|
| 207 |
return JSONResponse(content=SURAHS_DATA["data"])
|
| 208 |
raise HTTPException(status_code=404, detail="Surahs data not found")
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
# Get list of reciters
|
| 211 |
@app.get("/api/reciters")
|
| 212 |
async def get_reciters():
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 2 |
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse, StreamingResponse, Response
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from scalar_fastapi import get_scalar_api_reference
|
|
|
|
| 8 |
import threading
|
| 9 |
from pathlib import Path
|
| 10 |
from huggingface_hub import HfApi, hf_hub_download
|
| 11 |
+
import re
|
| 12 |
+
|
| 13 |
+
# Arabic diacritics normalization
|
| 14 |
+
def normalize_arabic(text: str) -> str:
|
| 15 |
+
# List of Arabic diacritics (tashkeel) to remove
|
| 16 |
+
diacritics = [
|
| 17 |
+
'\u0610', '\u0611', '\u0612', '\u0613', '\u0614',
|
| 18 |
+
'\u0615', '\u0616', '\u0617', '\u0618', '\u0619',
|
| 19 |
+
'\u061A', '\u064B', '\u064C', '\u064D', '\u064E',
|
| 20 |
+
'\u064F', '\u0650', '\u0651', '\u0652', '\u0653',
|
| 21 |
+
'\u0654', '\u0655', '\u0656', '\u0657', '\u0658',
|
| 22 |
+
'\u0659', '\u065A', '\u065B', '\u065C', '\u065D',
|
| 23 |
+
'\u065E', '\u065F', '\u0670', '\u0671', '\u06D6',
|
| 24 |
+
'\u06D7', '\u06D8', '\u06D9', '\u06DA', '\u06DB',
|
| 25 |
+
'\u06DC', '\u06DD', '\u06DE', '\u06DF', '\u06E0',
|
| 26 |
+
'\u06E1', '\u06E2', '\u06E3', '\u06E4', '\u06E5',
|
| 27 |
+
'\u06E6', '\u06E7', '\u06E8', '\u06E9', '\u06EA',
|
| 28 |
+
'\u06EB', '\u06EC', '\u06ED', '\u06EE', '\u06EF',
|
| 29 |
+
'\u06F0', '\u06F1', '\u06F2', '\u06F3', '\u06F4',
|
| 30 |
+
'\u06F5', '\u06F6', '\u06F7', '\u06F8', '\u06F9'
|
| 31 |
+
]
|
| 32 |
+
# Remove all diacritics
|
| 33 |
+
for diacritic in diacritics:
|
| 34 |
+
text = text.replace(diacritic, '')
|
| 35 |
+
return text
|
| 36 |
+
|
| 37 |
+
# Edition to filename mapping
|
| 38 |
+
EDITION_FILENAMES = {
|
| 39 |
+
'uthmani': 'uthmani.json',
|
| 40 |
+
'en.sahih': 'en.sahih.json',
|
| 41 |
+
'ar.muyassar': 'ar.muyassar.json'
|
| 42 |
+
}
|
| 43 |
|
| 44 |
app = FastAPI(
|
| 45 |
title="Kalamalah Quran API",
|
|
|
|
| 207 |
"endpoints": {
|
| 208 |
"quran": "/api/quran/{language}/{surah_number}[/{ayah_number}]",
|
| 209 |
"surahs_list": "/api/surahs",
|
| 210 |
+
"search": "/api/search",
|
| 211 |
"reciters": "/api/reciters",
|
| 212 |
"reciter": "/api/reciters/{reciter_name}",
|
| 213 |
"audio": "/api/{reciter_name}/{type}/{surah_number}",
|
|
|
|
| 240 |
return JSONResponse(content=SURAHS_DATA["data"])
|
| 241 |
raise HTTPException(status_code=404, detail="Surahs data not found")
|
| 242 |
|
| 243 |
+
# Search Quran endpoint
|
| 244 |
+
@app.get("/api/search")
|
| 245 |
+
async def search_quran(
|
| 246 |
+
query: str = Query(..., description="Search query"),
|
| 247 |
+
edition: str = Query("uthmani", description="Edition to search in: uthmani, en.sahih, ar.muyassar"),
|
| 248 |
+
surah_number: int | None = Query(None, description="Search in a specific surah (1-114), or leave empty to search entire Quran"),
|
| 249 |
+
with_tashkeel: bool = Query(False, description="Search with tashkeel (diacritics) enabled")
|
| 250 |
+
):
|
| 251 |
+
# Validate edition
|
| 252 |
+
if edition not in EDITION_FILENAMES:
|
| 253 |
+
raise HTTPException(status_code=400, detail="Invalid edition. Choose from: uthmani, en.sahih, ar.muyassar")
|
| 254 |
+
|
| 255 |
+
# Validate surah number if provided
|
| 256 |
+
if surah_number is not None and (surah_number < 1 or surah_number > 114):
|
| 257 |
+
raise HTTPException(status_code=400, detail="Invalid surah number. Must be between 1 and 114.")
|
| 258 |
+
|
| 259 |
+
# Determine surahs to search
|
| 260 |
+
surahs_to_search = list(range(1, 115)) if surah_number is None else [surah_number]
|
| 261 |
+
|
| 262 |
+
results = []
|
| 263 |
+
filename = EDITION_FILENAMES[edition]
|
| 264 |
+
|
| 265 |
+
# Normalize query if searching without tashkeel (only for Arabic editions)
|
| 266 |
+
normalized_query = normalize_arabic(query) if not with_tashkeel and edition in ["uthmani", "ar.muyassar"] else query
|
| 267 |
+
|
| 268 |
+
for surah_num in surahs_to_search:
|
| 269 |
+
surah_path = API_DATA_DIR / "surah" / str(surah_num) / filename
|
| 270 |
+
if not surah_path.exists():
|
| 271 |
+
continue
|
| 272 |
+
try:
|
| 273 |
+
with open(surah_path, "r", encoding="utf-8") as f:
|
| 274 |
+
data = json.load(f)
|
| 275 |
+
if data.get("code") == 200:
|
| 276 |
+
ayahs = data["data"]["ayahs"]
|
| 277 |
+
for ayah in ayahs:
|
| 278 |
+
ayah_text = ayah["text"]
|
| 279 |
+
# Normalize ayah text if needed
|
| 280 |
+
text_to_search = normalize_arabic(ayah_text) if not with_tashkeel and edition in ["uthmani", "ar.muyassar"] else ayah_text
|
| 281 |
+
# Check if query is in text (case-insensitive)
|
| 282 |
+
if normalized_query.lower() in text_to_search.lower():
|
| 283 |
+
results.append({
|
| 284 |
+
"surah_number": surah_num,
|
| 285 |
+
"surah_name": data["data"]["name"],
|
| 286 |
+
"surah_english_name": data["data"]["englishName"],
|
| 287 |
+
"ayah_number": ayah["numberInSurah"],
|
| 288 |
+
"text": ayah_text,
|
| 289 |
+
"juz": ayah["juz"],
|
| 290 |
+
"page": ayah["page"]
|
| 291 |
+
})
|
| 292 |
+
except Exception as e:
|
| 293 |
+
print(f"Error processing surah {surah_num}: {e}")
|
| 294 |
+
continue
|
| 295 |
+
|
| 296 |
+
return JSONResponse(content={"results": results, "count": len(results)})
|
| 297 |
+
|
| 298 |
# Get list of reciters
|
| 299 |
@app.get("/api/reciters")
|
| 300 |
async def get_reciters():
|