# Bayan (ุจูŠุงู†) - Arabic Writing Assistant & Text Summarization System Bayan is a state-of-the-art Arabic text editing and summarization application. Similar to assistants like Grammarly, Bayan provides real-time correction of spelling, grammar, and punctuation, combined with autocomplete suggestions and an advanced summarization pipeline. It features a modern, responsive web interface that communicates with a Flask backend powered by deep learning models. --- ## ๐Ÿ“ Repository Layout & File Descriptions ``` Bayan/ โ”œโ”€โ”€ data/ # Directory for raw and processed datasets (empty by default) โ”œโ”€โ”€ models/ # Deep learning models directory (organized by task) โ”‚ โ”œโ”€โ”€ Autocomplete/ # GPT-2 autocomplete model โ”‚ โ”œโ”€โ”€ Grammrar/ # Gemma-based grammar correction model โ”‚ โ”œโ”€โ”€ Punctuation/ # Seq2Seq punctuation correction model โ”‚ โ”œโ”€โ”€ Spelling/ # BERT-based spelling corrector checkpoint โ”‚ โ””โ”€โ”€ Summarization/ # mBART summarization model checkpoint โ”œโ”€โ”€ src/ # Core backend source code and frontend โ”‚ โ”œโ”€โ”€ app.py # Flask server containing API endpoints โ”‚ โ”œโ”€โ”€ ara_spell.py # Custom spell-checking algorithms and post-processing โ”‚ โ”œโ”€โ”€ index.html # TailwindCSS & Vanilla JS responsive web interface โ”‚ โ”œโ”€โ”€ model_loader.py # Loader classes for all deep learning models โ”‚ โ””โ”€โ”€ README.md # Source code instructions and API output contracts โ”œโ”€โ”€ check_dependencies.py # Helper script to check required Python libraries โ”œโ”€โ”€ inspect_decoder.py # Weight inspection helper for the spelling model โ”œโ”€โ”€ inspect_model.py # Basic PyTorch checkpoint architecture identifier โ”œโ”€โ”€ inspect_model_details.py # Detailed tensor shape explorer for spelling checkpoint โ”œโ”€โ”€ inspect_model_weights.py # Checkpoint structure explorer โ”œโ”€โ”€ LICENSE # MIT License โ”œโ”€โ”€ QUICKSTART.md # Quickstart guide for setting up and running Bayan โ”œโ”€โ”€ README.md # Main project overview and directory layout โ”œโ”€โ”€ README_SETUP.md # Detailed step-by-step setup and troubleshooting guide โ”œโ”€โ”€ reproduce_issue.py # Simple local script to test Spelling, Grammar, and Punctuation models โ”œโ”€โ”€ requirements.txt # Python dependencies (PyTorch, Transformers, Flask, etc.) โ”œโ”€โ”€ run_app.py # Standard launcher script for the application โ”œโ”€โ”€ summarization_test.py # Local tests and configuration options for Summarization โ”œโ”€โ”€ test_analyze_api.py # Request test script for the /api/analyze endpoint โ”œโ”€โ”€ test_analyze_methods.py # Request test script for GET/POST validations of analyze endpoint โ”œโ”€โ”€ test_model_load.py # Verification script for local summarization model loading โ”œโ”€โ”€ upload_model.py # Script to upload models to the Hugging Face hub โ””โ”€โ”€ verify_api_live.py # Test script to send sample text to a live API server ``` --- ## ๐Ÿ› ๏ธ Core Features 1. **Smart Spelling Correction (`SpellingModel`)**: - Cleans the text (removes harakat and tatweel), collapses repeated characters, and resolves common keyboard substitution errors. - Generates candidates using seq2seq model inference (beams), smart rules-based heuristics, and edit-distance suggestions (Norvig's spelling corrector adapted for Arabic). - Reranks candidates using a combined formula of **fluency** (evaluated using a BERT Masked Language Model), **similarity** (Damerau-Levenshtein distance), and **vocabulary-aware acceptance** (checks In-Vocabulary/Out-of-Vocabulary words from the tokenizer dictionary). 2. **Grammar Correction (`GrammarModel`)**: - Loads a Gemma causal language model configured to run on CPU. - Evaluates grammar through a standard chat template prompt. - Extracts the first valid non-empty corrected sentence and rejects generic instruction text generated by the model. 3. **Punctuation Insertion (`PunctuationModel`)**: - Uses a Seq2Seq architecture to automatically place Arabic commas (`ุŒ`), semicolons (`ุ›`), question marks (`ุŸ`), periods (`.`), and quotation marks (`ยซ ยป`) into continuous text. 4. **Text Summarization (`SummarizationModel`)**: - Leverages an mBART conditional generation model. - Supports variable length thresholds (short: ~30%, medium: ~50%, long: ~70% of the input text length). - Features a **safe extractive fallback** mechanism: if the generated abstractive summary deviates too far from the original text (monitored by word overlap and similarity ratios), it falls back to a readable extractive summary composed of the opening sentences of the source text. 5. **Autocomplete Suggestions (`AutocompleteModel`)**: - Powered by a local GPT-2 model (CPU-only mode) configured to predict the next word given a text prefix. - Integrates with the web interface to display ghost text prompts that users can accept by pressing the `Tab` key. --- ## ๐Ÿ–ฅ๏ธ Architecture & Web Interface The project uses a unified **Client-Server Architecture**: ```mermaid graph TD Client[Web Interface: HTML / CSS / JS] <-->|JSON over HTTP| Server[Flask API Server: app.py] Server <--> ModelLoader[model_loader.py] ModelLoader <--> Spelling[SpellingModel / ara_spell.py] ModelLoader <--> Grammar[GrammarModel] ModelLoader <--> Punctuation[PunctuationModel] ModelLoader <--> Summarization[SummarizationModel] ModelLoader <--> Autocomplete[AutocompleteModel] ``` ### 1. Backend: Flask API (`src/app.py`) - Manages model state instances and startup loading triggers (loads the summarization model on startup and lazily loads autocomplete as needed). - Provides API endpoints validating text length requirements (between 10 and 5,000 characters). - Implements `/api/analyze` which coordinates a sequential processing pipeline: $$\text{Input Text} \rightarrow \text{Spelling Correction} \rightarrow \text{Grammar Correction} \rightarrow \text{Punctuation Insertion} \rightarrow \text{Diff Calculation}$$ ### 2. Frontend: Modern Web Application (`src/index.html`) - Built using **TailwindCSS** for styling, **Google Fonts** (Tajawal, Noto Kufi Arabic) for premium typography, and glassmorphism cards. - Includes a live, rich editing canvas (`contenteditable`) with instant wavy underlines representing errors: - Red underlines indicate **Spelling Errors**. - Yellow underlines indicate **Grammar / Punctuation Suggestions**. - Features an interactive **suggestion tooltip** allowing users to click on highlighted words to view explanations and apply replacements directly. - Displays a real-time **document score metric** (0โ€“100 circular gauge) based on error density, along with word counters and feedback lists. - Hosts a **Summarization Panel** where users can control the length and generation configuration of the text summarizer. --- ## ๐Ÿ”Œ API Endpoints Reference ### 1. Health Check * **Endpoint**: `GET /api/health` * **Response**: ```json { "status": "healthy", "models": { "summarization": true, "spelling": false, "autocomplete": false, "grammar": false, "punctuation": false } } ``` ### 2. Summarize Text * **Endpoint**: `POST /api/summarize` * **Payload**: ```json { "text": "ุงู„ู†ุต ุงู„ุนุฑุจูŠ ุงู„ุทูˆูŠู„ ุงู„ู…ุฑุงุฏ ุชู„ุฎูŠุตู‡...", "length": 2, // 1 = short, 2 = medium, 3 = long "full_text": true } ``` * **Response**: ```json { "status": "success", "summary": "ุงู„ู…ู„ุฎุต ุงู„ู…ูˆู„ุฏ ู…ู† ุงู„ู†ู…ูˆุฐุฌ...", "original_length": 1420, "summary_length": 620 } ``` ### 3. Spelling Correction * **Endpoint**: `POST /api/spelling` * **Payload**: `{"text": "ุงู„ูƒุชุจุฉ ุงู„ุตุญูŠุญู‡"}` * **Response**: `{"corrected": "ุงู„ูƒุชุงุจุฉ ุงู„ุตุญูŠุญุฉ", "status": "success", ...}` ### 4. Autocomplete * **Endpoint**: `POST /api/autocomplete` * **Payload**: `{"text": "ุฐู‡ุจ ุงู„ุทุงู„ุจ ุฅู„ู‰", "n": 3}` * **Response**: `{"suggestions": ["ุงู„ู…ุฏุฑุณุฉ", "ุงู„ุฌุงู…ุนุฉ", "ุงู„ูุตู„"], "status": "success"}` ### 5. Unified Analyze Text * **Endpoint**: `POST /api/analyze` * **Payload**: `{"text": "ุงู„ุทู„ุงุจ ุฐู‡ุจูˆ ุงู„ู‰ ุงู„ู…ุฏุฑุณุฉ"}` * **Response**: ```json { "original": "ุงู„ุทู„ุงุจ ุฐู‡ุจูˆ ุงู„ู‰ ุงู„ู…ุฏุฑุณุฉ", "corrected": "ุฐู‡ุจ ุงู„ุทู„ุงุจ ุฅู„ู‰ ุงู„ู…ุฏุฑุณุฉ.", "suggestions": [ { "original": "ุฐู‡ุจูˆ", "correction": "ุฐู‡ุจูˆุง", "type": "spelling" }, { "original": "ุฐู‡ุจูˆุง", "correction": "ุฐู‡ุจ", "type": "grammar" }, { "original": "ุงู„ุทู„ุงุจ ุฐู‡ุจ", "correction": "ุฐู‡ุจ ุงู„ุทู„ุงุจ", "type": "grammar" }, { "original": "ุงู„ู…ุฏุฑุณุฉ", "correction": "ุงู„ู…ุฏุฑุณุฉ.", "type": "punctuation" } ], "status": "success" } ``` --- ## ๐Ÿš€ How to Run the Project ### 1. Install Dependencies Make sure you have Python 3.8+ installed, and then run: ```bash pip install -r requirements.txt ``` *Note: If you are running on a CPU-only environment or want to configure PyTorch for CUDA (GPU), visit [PyTorch Local Setup](https://pytorch.org/get-started/locally/) to install the appropriate distribution.* ### 2. Prepare Model Files Verify that you have placed the model files under the `models/` directory: - Summarization: `models/Summarization/Model/` - Spelling: `models/Spelling/Model/` - Autocomplete: `models/Autocomplete/Model/` - Grammar: `models/Grammrar/Model/` - Punctuation: `models/Punctuation/Model/` ### 3. Run the Server Use gunicorn (production) or Flask dev server: ```bash # Production (matches Procfile) cd src && gunicorn app:app --bind 0.0.0.0:7860 --timeout 120 --workers 1 # Development cd src && python -c "from app import app; app.run(host='0.0.0.0', port=7860, debug=True)" ``` Open your web browser and navigate to: ``` http://localhost:7860 ```