Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the Grammarly CoEdit model for grammar & spelling correction
|
6 |
+
pipe = pipeline("text2text-generation",
|
7 |
+
model="grammarly/coedit-large",
|
8 |
+
torch_dtype=torch.bfloat16)
|
9 |
+
|
10 |
+
def correct_grammar(text: str) -> str:
|
11 |
+
"""
|
12 |
+
Takes an English sentence or paragraph and returns
|
13 |
+
a grammatically and orthographically corrected version.
|
14 |
+
"""
|
15 |
+
# Generate corrected text
|
16 |
+
outputs = pipe(
|
17 |
+
text,
|
18 |
+
max_length=256,
|
19 |
+
num_beams=5,
|
20 |
+
early_stopping=True
|
21 |
+
)
|
22 |
+
return outputs[0]["generated_text"]
|
23 |
+
|
24 |
+
# Build a simple Gradio interface
|
25 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
26 |
+
gr.Markdown(
|
27 |
+
"""
|
28 |
+
# 📝 Grammar & Spelling Corrector
|
29 |
+
Paste or type any English sentence below and receive a corrected version
|
30 |
+
powered by `grammarly/coedit-large`.
|
31 |
+
"""
|
32 |
+
)
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
inp = gr.Textbox(
|
36 |
+
label="✍️ Input Text",
|
37 |
+
placeholder="Type a sentence or paragraph here…",
|
38 |
+
lines=4,
|
39 |
+
show_copy_button=True
|
40 |
+
)
|
41 |
+
out = gr.Textbox(
|
42 |
+
label="✅ Corrected Text",
|
43 |
+
placeholder="The corrected text will appear here.",
|
44 |
+
lines=4,
|
45 |
+
interactive=False,
|
46 |
+
show_copy_button=True
|
47 |
+
)
|
48 |
+
|
49 |
+
btn = gr.Button("✔️ Correct Grammar")
|
50 |
+
btn.click(fn=correct_grammar, inputs=inp, outputs=out)
|
51 |
+
|
52 |
+
gr.Markdown(
|
53 |
+
"""
|
54 |
+
---
|
55 |
+
Built with 🤗 Transformers (`grammarly/coedit-large`) and 🚀 Gradio
|
56 |
+
"""
|
57 |
+
)
|
58 |
+
|
59 |
+
demo.launch()
|