CoolFace
Apppublic

NightPrince/Tafsir_Search

sourceHugging Facemitupdated 7mo agoView on Hugging Face
0likes
App README

<div align="center">

๐Ÿ“š Tafsir Search

Semantic search across 7 classical Quranic commentaries โ€” find the exact scholarly interpretation you're looking for.

![HuggingFace Space](https://huggingface.co/spaces/NightPrince/Tafsir_Search) ![License: MIT](LICENSE) ![Python 3.10](https://python.org) ![FastAPI](https://fastapi.tiangolo.com)

</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

DifficultyBook
โญ EasyุชูุณูŠุฑ ุงู„ู…ูŠุณุฑ ยท ุชูุณูŠุฑ ุงู„ุณุนุฏูŠ
โญโญ MediumุชูุณูŠุฑ ุงู„ุฌู„ุงู„ูŠู† ยท ุชูุณูŠุฑ ุงุจู† ูƒุซูŠุฑ
โญโญโญ AdvancedุชูุณูŠุฑ ุงู„ุจุบูˆูŠ ยท ุชูุณูŠุฑ ุงู„ู‚ุฑุทุจูŠ
โญโญโญโญ ClassicalุชูุณูŠุฑ ุงู„ุทุจุฑูŠ

Tech Stack

LayerTechnology
BackendFastAPI + Uvicorn
Embeddingssentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
Vector SearchFAISS (CPU)
Keyword SearchBM25 (rank-bm25)
FrontendVanilla HTML/CSS/JS โ€” RTL Arabic
DeploymentDocker on HuggingFace Spaces

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
โ””โ”€โ”€ Dockerfile

API

POST /api/search

json
// 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

bash
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 7860 --reload
# open http://localhost:7860

Built 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)