File size: 1,479 Bytes
fa9f85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from corrector import GrammarCorrector
import difflib

corrector = GrammarCorrector()

def highlight_diffs(original, corrected):
    diff = difflib.ndiff(original.split(), corrected.split())
    result = []
    for word in diff:
        if word.startswith("-"):
            result.append(f"~~{word[2:]}~~")
        elif word.startswith("+"):
            result.append(f"**{word[2:]}**")
        elif word.startswith(" "):
            result.append(word[2:])
    return " ".join(result)

def fix_sentence(text):
    corrected = corrector.correct(text)
    highlighted = highlight_diffs(text, corrected)
    return corrected, highlighted

gr.Interface(
    fn=fix_sentence,
    inputs=gr.Textbox(lines=2, label="Input Sentence"),
    outputs=[
        gr.Textbox(label="Corrected Sentence"),
        gr.Markdown(label="Changes Highlighted")
    ],
    title="Context-Aware Grammar & Spell Checker",
    description="Fixes grammar and spelling using a T5-based model.",

    examples=[
        ["She go to school every day."],
        ["I can has cheeseburger?"],
        ["The cat sleeped on the mat."],
        ["We was going to the park yesterday."],
        ["This is teh best day of my life!"],
        ["He no went to office today."],
        ["Their coming too the party."],
        ["I hopes you gets better soon."],
        ["Where is you going now?"],
        ["He do not likes pizza."]
    ]
).launch()