Commit ·
ce18823
1
Parent(s): a10a9fe
docs: rewrite README to reflect the actual codebase (features, pipeline, extension, API)
Browse files
README.md
CHANGED
|
@@ -7,35 +7,154 @@ sdk: docker
|
|
| 7 |
app_port: 7860
|
| 8 |
---
|
| 9 |
|
| 10 |
-
#
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
- Style / clarity suggestions for Modern Standard Arabic (MSA) and dialects
|
| 20 |
-
- Sentence rewriting and paraphrasing suggestions
|
| 21 |
-
- Plagiarism or similarity checks (optional)
|
| 22 |
-
- Integration with web UI / browser extension / API
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
- Add tests in `src/tests/` and keep the public API stable.
|
| 37 |
-
- Use small, focused pull requests that include a short description and test(s) if applicable.
|
| 38 |
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
|
|
|
| 7 |
app_port: 7860
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# BAYAN (بَيان) — AI-Powered Arabic Writing Assistant ✍️
|
| 11 |
|
| 12 |
+
**BAYAN** is a full-stack Arabic writing assistant — a Grammarly-style platform for Modern
|
| 13 |
+
Standard Arabic that combines fine-tuned Transformer models with handcrafted linguistic
|
| 14 |
+
rules. It corrects **spelling, grammar, and punctuation** in real time, and adds
|
| 15 |
+
**summarization, autocomplete, dialect→MSA translation, and Quran verification** — delivered
|
| 16 |
+
through a modern web app and a Chrome extension.
|
| 17 |
|
| 18 |
+
> Graduation Project — Cairo University, Faculty of Computers & Artificial Intelligence.
|
| 19 |
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## 🌟 Overview
|
| 23 |
+
|
| 24 |
+
Production Arabic NLP is roughly 20% model training and 80% engineering safety scaffolding.
|
| 25 |
+
BAYAN reflects that: multiple fine-tuned models wrapped in a **deterministic, collision-free
|
| 26 |
+
correction engine** that guarantees stable, non-overlapping edits, plus religious-text
|
| 27 |
+
protection so sacred text is never altered.
|
| 28 |
+
|
| 29 |
+
The core `/api/analyze` endpoint runs a sequential pipeline:
|
| 30 |
+
|
| 31 |
+
```
|
| 32 |
+
Input → Spelling → Grammar → Punctuation → Diff
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
## 🧠 Core Features
|
| 38 |
+
|
| 39 |
+
| Feature | Model / Method |
|
| 40 |
+
|--------|-----------------|
|
| 41 |
+
| **Spelling** | Seq2Seq (BERT-based) beam candidates + Norvig-style edit distance, reranked by MLM fluency, Damerau-Levenshtein similarity, and in-/out-of-vocabulary acceptance; CAMeL Tools morphological reranking |
|
| 42 |
+
| **Grammar** | Gemma causal LM via chat-template prompting, with rule-based guards that reject generic/instructional output |
|
| 43 |
+
| **Punctuation** | Seq2Seq model inserting Arabic marks (`،` `؛` `؟` `.` `« »`) into continuous text |
|
| 44 |
+
| **Summarization** | mBART conditional generation with short/medium/long thresholds and a **safe extractive fallback** when abstractive output drifts too far from the source |
|
| 45 |
+
| **Autocomplete** | Local GPT-2 next-word prediction, surfaced as ghost text (accept with `Tab`) |
|
| 46 |
+
| **Dialect → MSA** | Dialect-to-Modern-Standard-Arabic translation (`src/nlp/dialect`) |
|
| 47 |
+
| **Quran Verification** | Dual-stage normalization + cascading anchor search + RapidFuzz fuzzy matching against an Uthmani-script database (`quran.py`) |
|
| 48 |
+
|
| 49 |
+
### Evaluation highlights
|
| 50 |
+
- **Grammar:** GLEU 75% · ChrF++ 88% — exceeding published SOTA (62–68% / 72–78%); ~60% hallucination reduction via LoRA
|
| 51 |
+
- **Spelling:** 95.63% word accuracy · 1.40% CER — outperforming Google Docs (~90%)
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## 🏗️ Architecture
|
| 56 |
+
|
| 57 |
+
**Client–Server**, model-agnostic and modular:
|
| 58 |
+
|
| 59 |
+
```
|
| 60 |
+
Web UI / Chrome Extension ⇄ Flask API (src/app.py) ⇄ model_loader.py ⇄ NLP models
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
- **Multi-stage correction engine** (`src/nlp/`) with `pipeline_context.py` and
|
| 64 |
+
`stage_locker.py` ensuring collision-free, deterministic corrections across stages.
|
| 65 |
+
- **Backend:** Flask API — loads summarization on startup, lazily loads the rest; validates
|
| 66 |
+
input length (10–5,000 chars). Endpoints: `/api/health`, `/api/analyze`, `/api/spelling`,
|
| 67 |
+
`/api/summarize`, `/api/autocomplete`.
|
| 68 |
+
- **Frontend (`src/index.html`):** TailwindCSS + Vanilla JS, glassmorphism UI, a live
|
| 69 |
+
`contenteditable` canvas with wavy underlines (red = spelling, yellow = grammar/punctuation),
|
| 70 |
+
click-to-apply suggestion tooltips, a 0–100 document-score gauge, and a summarization panel.
|
| 71 |
+
- **Chrome Extension (Manifest V3, `extension/`):** popup, side panel, and inline
|
| 72 |
+
content overlay — works on any webpage, with localization (`_locales`).
|
| 73 |
+
|
| 74 |
+
---
|
| 75 |
|
| 76 |
+
## 📁 Repository Layout
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
```
|
| 79 |
+
src/
|
| 80 |
+
app.py Flask API + endpoints
|
| 81 |
+
model_loader.py Loaders for all models
|
| 82 |
+
nlp/ Correction engine: spelling, grammar, punctuation,
|
| 83 |
+
autocomplete, dialect + pipeline_context / stage_locker
|
| 84 |
+
index.html, css/, js/, services/, middleware/, routes/
|
| 85 |
+
extension/ Chrome extension (MV3): popup, side panel, inline overlay
|
| 86 |
+
quran.py Quran verification + quran_master.db
|
| 87 |
+
models/ Model checkpoints (not committed — see .gitignore)
|
| 88 |
+
Dockerfile Container build (HF Spaces / any host, port 7860)
|
| 89 |
+
Procfile gunicorn entrypoint
|
| 90 |
+
requirements.txt Python dependencies
|
| 91 |
+
```
|
| 92 |
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
## 🚀 How to Run
|
| 96 |
+
|
| 97 |
+
### Option A — Docker (matches deployment)
|
| 98 |
+
```bash
|
| 99 |
+
docker build -t bayan .
|
| 100 |
+
docker run -p 7860:7860 bayan
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
### Option B — Local Python
|
| 104 |
+
```bash
|
| 105 |
+
pip install -r requirements.txt
|
| 106 |
+
|
| 107 |
+
# place model checkpoints under models/ (Spelling, Grammrar, Punctuation,
|
| 108 |
+
# Summarization, Autocomplete)
|
| 109 |
+
|
| 110 |
+
cd src && gunicorn app:app --bind 0.0.0.0:7860 --timeout 120 --workers 1
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
Then open **http://localhost:7860**.
|
| 114 |
+
|
| 115 |
+
### Chrome Extension
|
| 116 |
+
`chrome://extensions` → enable **Developer mode** → **Load unpacked** → select `extension/`.
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
## 🔌 API Quick Reference
|
| 121 |
+
|
| 122 |
+
| Endpoint | Method | Purpose |
|
| 123 |
+
|----------|--------|---------|
|
| 124 |
+
| `/api/health` | GET | Model load status |
|
| 125 |
+
| `/api/analyze` | POST | Full pipeline (spelling → grammar → punctuation) + suggestions diff |
|
| 126 |
+
| `/api/spelling` | POST | Spelling correction only |
|
| 127 |
+
| `/api/summarize` | POST | Summarize (`length`: 1 short / 2 medium / 3 long) |
|
| 128 |
+
| `/api/autocomplete` | POST | Next-word suggestions |
|
| 129 |
|
| 130 |
+
Example — `POST /api/analyze`:
|
| 131 |
+
```json
|
| 132 |
+
{ "text": "الطلاب ذهبو الى المدرسة" }
|
| 133 |
+
```
|
| 134 |
+
```json
|
| 135 |
+
{
|
| 136 |
+
"original": "الطلاب ذهبو الى المدرسة",
|
| 137 |
+
"corrected": "ذهب الطلاب إلى المدرسة.",
|
| 138 |
+
"suggestions": [
|
| 139 |
+
{ "original": "ذهبو", "correction": "ذهبوا", "type": "spelling" },
|
| 140 |
+
{ "original": "المدرسة", "correction": "المدرسة.", "type": "punctuation" }
|
| 141 |
+
],
|
| 142 |
+
"status": "success"
|
| 143 |
+
}
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
---
|
| 147 |
+
|
| 148 |
+
## ⚙️ Tech Stack
|
| 149 |
+
|
| 150 |
+
Python · PyTorch · Hugging Face Transformers · Gemma · mBART · GPT-2 · BERT · LoRA ·
|
| 151 |
+
CAMeL Tools · RapidFuzz · Flask · Gunicorn · TailwindCSS · Vanilla JS · Chrome Extension
|
| 152 |
+
(MV3) · Docker · Hugging Face Spaces.
|
| 153 |
+
|
| 154 |
+
---
|
| 155 |
|
| 156 |
+
## 📄 License
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
MIT — see [`LICENSE`](LICENSE).
|
| 159 |
|
| 160 |
+
*Model weights and datasets are kept out of Git; checkpoints are hosted on the Hugging Face Hub.*
|