NightPrince/Tafsir_Search
<div align="center">
๐ Tafsir Search
Semantic search across 7 classical Quranic commentaries โ find the exact scholarly interpretation you're looking for.
   
</div>
What Is This?
A hybrid AI-powered search engine over ~32,000 passages from 7 major classical Tafsir (Quranic exegesis) books. Unlike simple keyword search, it understands the meaning of your query โ returning the most relevant scholarly commentary even when the exact words don't appear.
Ask about a concept, a verse, a ruling, or a theological question in Arabic and get ranked excerpts from the scholars most likely to have addressed it.
Demo
๐ [Live on HuggingFace Spaces โ](https://huggingface.co/spaces/NightPrince/Tafsir_Search)
How It Works
User Query (Arabic)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Arabic Preprocessing โ
โ Remove tashkeel ยท Normalize letters โ
โ Strip Tafsir-specific 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-K ranked results
(text ยท verse ref ยท difficulty)The anchor signal embeds Quranic verse keys and boosts results linked to the verse most relevant to your query โ making it verse-aware without hardcoding any mapping.
Features
- Hybrid retrieval โ BM25 + FAISS semantic + verse-anchor scoring
- 7 Tafsir sources โ spanning 1,200 years of Islamic scholarship
- Difficulty levels โ from easy (ุงูู ูุณุฑ) to classical Arabic (ุงูุทุจุฑู)
- Verse references โ every result linked to its Quranic source
- RTL Arabic UI โ glassmorphism design, fully right-to-left
- Fast cold start โ 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, RAG initialization
โโโ retrieval.py # Hybrid search: BM25 + semantic + anchor
โโโ hf_model.py # Thread-safe SentenceTransformer + TTL cache
โโโ utils.py # Arabic text utilities (tashkeel, normalization)
โโโ index.html # Frontend UI
โโโ assets/
โ โโโ script.js # Fetch + render result cards
โ โโโ style.css # Glassmorphism RTL design
โโโ data/
โ โโโ tafsir.csv # ~32k Tafsir passages (7 books)
โ โโโ tafsir_embeddings.npy # Pre-computed embeddings
โ โโโ bm25.pkl # BM25 index
โ โโโ faiss_anchor.index # FAISS verse-anchor index
โ โโโ anchor_dict.pkl # verse_key โ row indices
โ โโโ unique_anchor_texts.pkl # Ordered verse key list
โโโ DockerfileAPI
POST /api/search
// Request
{ "query": "ุญูู
ุงูุบูุจุฉ ูุนููุจุชูุง", "top_k": 5 }
// Response
{
"results": [
{
"rank": 1,
"text": "ูุงู ุงุจู ูุซูุฑ ุฑุญู
ู ุงููู: ุงูุบูุจุฉ ูู ุฐูุฑู ุฃุฎุงู ุจู
ุง ููุฑู...",
"verses_keys": "49:12",
"resource_name": "ุชูุณูุฑ ุงุจู ูุซูุฑ",
"difficulty": 2
}
]
}top_k accepts 1โ10.
Local Setup
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 7860 --reload
# open http://localhost:7860Built by
ูุญูู ุงูููุณุงูู โ HuggingFace
Part of a series of Islamic knowledge retrieval engines. See also: [Hadith Search](https://github.com/NightPrinceY/Hadith_Search) ยท [Quran Semantic Retrieval](https://github.com/NightPrinceY/Quran-Semantic-Retrieval)
