Spaces:
Running on Zero
Running on Zero
File size: 9,253 Bytes
51e1331 043f08c 51e1331 3d9ba5b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | ---
title: Bilingual Text Summarization NLP Suite
emoji: ๐
colorFrom: indigo
colorTo: blue
sdk: gradio
sdk_version: 5.9.1
app_file: app.py
pinned: false
license: mit
---
# ๐ Bilingual ArabicโEnglish NLP System for Automatic Text Summarization
### ูุธุงู
ุงูู
ุนุงูุฌุฉ ุงููุบููุฉ ุงูุชููุงุฆูุฉ ููุชูุฎูุต ุงูุงุณุชุฎุฑุงุฌู ูุงูุชุฌุฑูุฏู ูููุตูุต ุงูุนุฑุจูุฉ ูุงูุฅูุฌููุฒูุฉ
---
## ๐ Executive Overview
This repository contains an end-to-end **Bilingual (Arabic & English) Natural Language Processing (NLP) System for Automatic Text Summarization**, implemented completely **from scratch**.
It addresses the fundamental linguistic, structural, and morphological differences between Arabic (a morphologically rich, non-concatenative language) and English (an analytic language with fixed syntax), offering both **Extractive** and **Abstractive** summarization paradigms alongside a rigorous quantitative evaluation suite (**ROUGE**, **BLEU**, **TTR**, and **Compression Metrics**) and a modern interactive Web Dashboard.
---
## ๐๏ธ System Architecture
```mermaid
flowchart TD
A[User Input: Text / PDF / DOCX / TXT] --> B[Language Detection & Character Routing]
B -->|Arabic| C1[Arabic Preprocessing Engine: Tashkeel, Normalization, ISRI Stemmer]
B -->|English| C2[English Preprocessing Engine: Contractions, Lemmatizer, Filter]
C1 --> D{Summarization Mode}
C2 --> D
D -->|Extractive| E1[Sentence Segmentation & TF-IDF Vectorizer]
E1 --> E2[Graph-based TextRank / LSA SVD / Hybrid Centroid]
E2 --> E3[Extractive Chronological Summary]
D -->|Abstractive| F1[Bilingual Vocabulary & Dynamic Pad Batching]
F1 --> F2[PyTorch Seq2Seq with Bahdanau / Luong Attention]
F2 --> F3[Beam Search / Greedy Decoding]
F3 --> F4[Abstractive Paraphrased Summary]
E3 --> G[Evaluation Suite: ROUGE-1/2/L, BLEU-1..4, Diversity & Ratio]
F4 --> G
G --> H[Streamlit Interactive Web GUI & CLI]
```
---
## ๐ Repository Structure
```text
sumary project/
โ
โโโ data/
โ โโโ sample_arabic.txt # Comprehensive Arabic sample text
โ โโโ sample_english.txt # Comprehensive English sample text
โ โโโ arabic_stopwords.txt # Curated Arabic stopword lexicon
โ โโโ english_stopwords.txt # Standard English stopword lexicon
โ โโโ datasets/ # Generated bilingual parallel corpora (.json)
โ
โโโ nlp_core/ # Fundamental NLP and preprocessing engine
โ โโโ __init__.py
โ โโโ language_detector.py # Unicode & n-gram language classifier (AR/EN)
โ โโโ arabic_preprocessor.py # Tashkeel removal, Alef/Yaa/Taa normalization, Light stemmer
โ โโโ english_preprocessor.py # Contraction expansion, lowercasing, stemmer
โ โโโ tokenizer.py # Arabic & English sentence segmenter and word tokenizer
โ โโโ vocabulary.py # Vocab manager, token encoder/decoder, special tokens
โ
โโโ models/
โ โโโ extractive/ # Extractive summarization algorithms (From Scratch)
โ โ โโโ tfidf.py # Custom Sublinear TF-IDF & Cosine Similarity
โ โ โโโ textrank.py # Graph PageRank power iteration summarizer
โ โ โโโ lsa.py # Latent Semantic Analysis with SVD
โ โ โโโ hybrid_scorer.py # Multi-feature (TextRank + Centroid + Position + Length)
โ โ
โ โโโ abstractive/ # Deep learning Sequence-to-Sequence (PyTorch From Scratch)
โ โโโ attention.py # Bahdanau (Additive) & Luong Attention mechanisms
โ โโโ encoder.py # Bidirectional GRU/LSTM Encoder
โ โโโ decoder.py # Attention-guided GRU Decoder
โ โโโ seq2seq_model.py # Full Seq2Seq model with Greedy & Beam Search
โ โโโ transformer_wrap.py # Modular wrapper for Hugging Face Pretrained Models
โ
โโโ training/ # ML/DL Training and Dataset pipeline
โ โโโ sample_data_gen.py # Bilingual parallel dataset synthesizer
โ โโโ dataset.py # PyTorch Dataset and dynamic pad collator
โ โโโ train.py # Training loop with teacher forcing, validation, checkpointing
โ
โโโ evaluation/ # Quantitative benchmark suite (From Scratch)
โ โโโ rouge.py # ROUGE-1, ROUGE-2, and ROUGE-L (Precision, Recall, F1)
โ โโโ bleu.py # BLEU-1 to BLEU-4 and Cumulative BLEU with Brevity Penalty
โ โโโ metrics_manager.py # Unified evaluator (Compression, Diversity, Overlap)
โ
โโโ checkpoints/ # Saved PyTorch model weights (.pt)
โ โโโ seq2seq_arabic.pt
โ โโโ seq2seq_english.pt
โ
โโโ tests/ # Complete automated unit test suite
โ โโโ test_preprocessing.py
โ โโโ test_extractive.py
โ โโโ test_abstractive.py
โ โโโ test_evaluation.py
โ
โโโ app.py # Modern Interactive Streamlit Web GUI
โโโ main_cli.py # Versatile Command-Line Interface
โโโ requirements.txt # Python dependencies
โโโ README.md # Documentation & System Manual
```
---
## ๐ Getting Started
### 1. Installation & Environment Setup
```bash
# Clone the repository and navigate to folder
cd "sumary project"
# Install dependencies
pip install -r requirements.txt
```
### 2. Run Automated Unit Tests
```bash
python -m unittest discover tests
```
*Executes all 14 unit tests validating preprocessing, TF-IDF, TextRank, LSA, Seq2Seq Attention forward pass, ROUGE, and BLEU.*
---
## ๐ป Usage
### 1. Interactive Web Application (GUI)
Launch the modern glassmorphic dashboard:
```bash
streamlit run app.py
```
**Features in the Web App:**
- ๐ Upload `.txt`, `.pdf`, or `.docx` documents.
- ๐ Automatic language detection for Arabic and English.
- ๐๏ธ Toggle between **Extractive** (TextRank, LSA, Hybrid) and **Abstractive** (Seq2Seq Attention, Transformers).
- ๐ Visual sentence highlighting showing exact extracted sentences in the source text.
- ๐ Live quantitative ROUGE and BLEU calculation against reference summaries.
- ๐๏ธ **Training Studio tab** to train and fine-tune Seq2Seq models directly from the GUI.
- ๐พ One-click export to download generated summaries.
---
### 2. Command-Line Interface (CLI)
#### A. Arabic Extractive Summarization (TextRank)
```bash
python main_cli.py --file data/sample_arabic.txt --mode extractive --method textrank --sentences 2
```
#### B. English Extractive Summarization (Hybrid Multi-Feature)
```bash
python main_cli.py --file data/sample_english.txt --mode extractive --method hybrid --sentences 3
```
#### C. Arabic Abstractive Summarization (Neural Seq2Seq with Attention)
```bash
python main_cli.py --text "ุชุนุฏ ู
ุนุงูุฌุฉ ุงููุบุงุช ุงูุทุจูุนูุฉ ูุฑุนุง ุฌููุฑูุง ู
ู ุงูุฐูุงุก ุงูุงุตุทูุงุนู ูููู
ูุชูููุฏ ุงููุตูุต ุงูุจุดุฑูุฉ." --mode abstractive --method seq2seq --beam 3
```
#### D. Summarize and Evaluate with ROUGE & BLEU Benchmarks
```bash
python main_cli.py --file data/sample_english.txt --mode extractive --method textrank --ref "Natural language processing enables computers to comprehend text and summarize documents efficiently."
```
#### E. JSON Output for API / Automated Pipelines
```bash
python main_cli.py --file data/sample_arabic.txt --mode extractive --json
```
---
## ๐ง Model Training
To retrain the neural Seq2Seq models on custom or augmented datasets:
```bash
# Train both Arabic and English models for 10 epochs
python training/train.py --lang all --epochs 10 --batch_size 8 --lr 0.002
# Train only Arabic model
python training/train.py --lang ar --epochs 15
# Train only English model
python training/train.py --lang en --epochs 15
```
Checkpoints are automatically saved to `checkpoints/seq2seq_arabic.pt` and `checkpoints/seq2seq_english.pt`.
---
## ๐ Evaluation & Mathematical Formulations
### 1. ROUGE-N (Recall, Precision, F1)
$$\text{ROUGE-N Recall} = \frac{\sum_{S \in \{\text{Reference}\}} \sum_{\text{gram}_n \in S} \text{Count}_{\text{match}}(\text{gram}_n)}{\sum_{S \in \{\text{Reference}\}} \sum_{\text{gram}_n \in S} \text{Count}(\text{gram}_n)}$$
### 2. BLEU Score with Brevity Penalty (BP)
$$\text{BLEU} = \text{BP} \cdot \exp\left(\sum_{n=1}^{N} w_n \ln p_n\right), \quad \text{where } \text{BP} = \begin{cases} 1 & \text{if } c > r \\ e^{(1 - r/c)} & \text{if } c \le r \end{cases}$$
### 3. PageRank Centrality (TextRank)
$$WS(V_i) = (1 - d) + d \sum_{V_j \in \text{In}(V_i)} \frac{w_{ji}}{\sum_{V_k \in \text{Out}(V_j)} w_{jk}} WS(V_j)$$
### 4. Bahdanau Additive Attention
$$e_{ij} = v_a^T \tanh(W_a s_{i-1} + U_a h_j), \quad \alpha_{ij} = \frac{\exp(e_{ij})}{\sum_k \exp(e_{ik})}, \quad c_i = \sum_j \alpha_{ij} h_j$$
---
## ๐ License
This project is licensed under the MIT License - open for educational, academic research, and commercial use.
|