NightPrince/Quran-Semantic-Retrieval
<div align="center">
๐ Quran Semantic Retrieval
Find the Quranic Ayah that speaks to your question โ powered by AI search across 7 classical Tafsir books.
   
</div>
What Is This?
This engine answers a deceptively simple question: "Which Ayah of the Quran is most relevant to what I'm asking about?"
Instead of keyword-matching directly against Quranic text, it searches semantically through 7 major classical Tafsir (exegesis) books. Scholars have already explained every verse in depth โ so matching your question against their explanations is far more semantically rich than matching against the verses alone.
The top-N most relevant Tafsir passages are found, their verse references are extracted, and the actual Ayah text is returned from a local copy of the Quran.
The Tafsir is the search index. The Ayah is the answer.
Demo
๐ [Live on HuggingFace Spaces โ](https://huggingface.co/spaces/NightPrince/Quran-Semantic-Retrieval)
Type any question or concept in Arabic โ the engine finds and displays the most semantically related Quranic verse(s).
How It Works
User Query (Arabic)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Arabic Preprocessing โ
โ Remove tashkeel ยท Normalize letters โ
โ Strip domain stopwords โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Hybrid Search (3 signals) โ
โ โ
โ โ BM25 50% โ keyword precision โ
โ โก Semantic 35% โ neural meaning match โ
โ โข Anchor 15% โ verse-ref boosting โ
โ โ
โ Corpus: ~32,000 Tafsir passages โ
โ Model: paraphrase-multilingual-MiniLM-L12 โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
Top-N unique verse keys
(deduplicated across books)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ quran-simple.xml lookup โ
โ verse_key "3:135" โ Ayah Arabic text โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
Ayah text returned to userFeatures
- Semantic search โ understands meaning, not just keywords
- Hybrid retrieval โ BM25 + FAISS + verse-anchor scoring combined
- Top-N results โ user selects 1โ5 Ayahs per query
- 7 Tafsir sources โ from easy (ุงูู ูุณุฑ) to classical (ุงูุทุจุฑู)
- Pure Quranic output โ only the Ayah is shown, never the Tafsir text
- RTL Arabic UI โ clean glassmorphism design, fully right-to-left
- Fast cold start โ embedding model baked into Docker image at build time
Tafsir Sources
Tech Stack
Project Structure
โโโ app.py # FastAPI entrypoint, /api/search endpoint
โโโ tafsir_mcp.py # Search orchestrator, XML loader, Ayah resolver
โโโ retrieval.py # Hybrid search algorithm (BM25 + semantic + anchor)
โโโ hf_model.py # Thread-safe SentenceTransformer with TTL cache
โโโ utils.py # Arabic text utilities (tashkeel, normalization)
โโโ index.html # Frontend UI
โโโ assets/
โ โโโ script.js # Fetch + render Ayah cards
โ โโโ style.css # Glassmorphism RTL design
โโโ data/
โ โโโ tafsir.csv # ~32k Tafsir passages (7 books)
โ โโโ tafsir_embeddings.npy
โ โโโ bm25.pkl
โ โโโ faiss_anchor.index
โ โโโ anchor_dict.pkl
โ โโโ unique_anchor_texts.pkl
โ โโโ quran-simple.xml # Tanzil Quran text
โโโ DockerfileAPI
POST /api/search
// Request
{ "query": "ุงูุตุจุฑ ุนูู ุงูุจูุงุก", "top_k": 3 }
// Response
{
"results": [
{ "ayah_text": "ููููููุจููููููููููู
ุจูุดูููุกู ู
ูููู ุงููุฎููููู...", "verse_key": "2:155" },
{ "ayah_text": "ุฅููููู
ูุง ูููููููู ุงูุตููุงุจูุฑูููู ุฃูุฌูุฑูููู
...", "verse_key": "39:10" },
{ "ayah_text": "ููุง ุฃููููููุง ุงูููุฐูููู ุขู
ููููุง ุงุณูุชูุนูููููุง...", "verse_key": "2:153" }
]
}top_k accepts 1โ5. The engine searches a pool of top_k ร 7 candidates internally, then returns the top-N results with unique verse keys.
Local Setup
# Install dependencies
pip install -r requirements.txt
# Run
uvicorn app:app --host 0.0.0.0 --port 7860 --reload
# Open
http://localhost:7860Built by
ูุญูู ุงูููุณุงูู โ HuggingFace
Quran text sourced from the [Tanzil Project](https://tanzil.net) under Creative Commons Attribution 3.0.
