File size: 3,588 Bytes
e48b19d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# πŸ“– Code Explanation: Grammar & Spelling Corrector

This document provides a step-by-step explanation of the **Grammar & Spelling Corrector** app, showing how each part works so even a beginner can follow.

---

## πŸ“ Overview

**Purpose**  
Improve grammar and spelling of English sentences or paragraphs using a state-of-the-art NLP model.

**Tech Stack**  
- **Model**: `grammarly/coedit-large` (Hugging Face Transformers)  
- **Interface**: Gradio Blocks + Buttons

---

## βš™οΈ Setup & Dependencies

Install required libraries:

```
pip install transformers gradio torch
```

---

## πŸ” Block-by-Block Explanation

### 1. Imports

```python
from transformers import pipeline
import gradio as gr
import torch
```

- **`pipeline`**: high-level API to load pre-trained models for various tasks.  
- **`gradio`**: library to quickly build web interfaces.

### 2. Model Loading

```python
pipe = pipeline(
    "text2text-generation",
    model="grammarly/coedit-large,
    torch_dtype=torch.bfloat16
)
```

- Loads the **CoEdit** model, fine-tuned for grammar and spelling correction.  
- The pipeline can generate corrected text from raw input.

### 3. Grammar Correction Function

```python
def correct_grammar(text: str) -> str:
    outputs = pipe(
        text,
        max_length=256,
        num_beams=5,
        early_stopping=True
    )
    return outputs[0]["generated_text"]
```

- **Input**: raw English text.  
- **Parameters**:  
  - `max_length=256`: maximum tokens for output.  
  - `num_beams=5`: beam search for more accurate generation.  
  - `early_stopping=True`: stop beams when best hypothesis is found.  
- **Output**: corrected text string.

### 4. Gradio User Interface

```python
with gr.Blocks(theme=gr.themes.Default()) as demo:
    gr.Markdown("# πŸ“ Grammar & Spelling Corrector")
    gr.Markdown("Paste any English sentence or paragraph below to get a corrected version.")

    with gr.Row():
        inp = gr.Textbox(
            label="✍️ Input Text",
            placeholder="Type your text here...",
            lines=4,
            show_copy_button=True
        )
        out = gr.Textbox(
            label="βœ… Corrected Text",
            placeholder="Corrected text appears here.",
            lines=4,
            interactive=False,
            show_copy_button=True
        )

    btn = gr.Button("βœ”οΈ Correct Grammar")
    btn.click(fn=correct_grammar, inputs=inp, outputs=out)

    gr.Markdown("---")
    gr.Markdown("Built with πŸ€— Transformers (`grammarly/coedit-large`) and πŸš€ Gradio")

demo.launch()
```

- **Blocks** and **Rows** organize layout.  
- **Textbox** for input and output.  
- **Button** triggers the `correct_grammar` function.

---

## πŸš€ Core Concepts

| Concept               | Why It Matters                                  |
|-----------------------|-------------------------------------------------|
| Hugging Face Pipeline | Simplifies loading and using pre-trained models |
| CoEdit Model          | Specialized for grammar and spelling correction |
| Gradio Interface      | Quickly builds interactive web apps            |

---

## πŸ”„ Extensions & Alternatives

- **Alternative Models**:  
  - `prithivida/grammar_error_correcter_v1` (T5-based)  
  - `microsoft/trocr-base-handwritten` for OCR + correction  

- **UI Enhancements**:  
  - Add file upload for batch corrections.  
  - Include copy-to-clipboard buttons and download corrected text.  

- **Performance**:  
  - Enable GPU support for faster inference.  
  - Experiment with `torch_dtype=torch.bfloat16` on supported hardware.