CoolFace
Apppublic

NightPrince/Quran-Semantic-Retrieval

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

<div align="center">

๐Ÿ“– Quran Semantic Retrieval

Find the Quranic Ayah that speaks to your question โ€” powered by AI search across 7 classical Tafsir books.

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

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

Features

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

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

Tech Stack

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

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

API

POST /api/search

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

bash
# Install dependencies
pip install -r requirements.txt

# Run
uvicorn app:app --host 0.0.0.0 --port 7860 --reload

# Open
http://localhost:7860

Built by

ูŠุญูŠู‰ ุงู„ู†ูˆุณุงู†ูŠ โ€” HuggingFace


Quran text sourced from the [Tanzil Project](https://tanzil.net) under Creative Commons Attribution 3.0.