File size: 1,120 Bytes
ab04512
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import T5ForConditionalGeneration, T5Tokenizer

# Load model and tokenizer
model_name = "vennify/t5-base-grammar-correction"
print("Loading model...")
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
print("Model loaded.")

def correct_text(text):
    if not text.strip():
        return ""
    # "vennify/t5-base-grammar-correction" requires "grammar: " prefix
    inputs = tokenizer("grammar: " + text, return_tensors="pt")
    
    # Generate prediction
    outputs = model.generate(
        **inputs, 
        num_beams=5,
        max_length=128
    )
    
    corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return corrected

# Simple Gradio interface
iface = gr.Interface(
    fn=correct_text,
    inputs=gr.Textbox(lines=5, placeholder="Enter text with grammar errors..."),
    outputs=gr.Textbox(label="Corrected text"),
    title="Grammar Correction API",
    description="A simple API for the local Windows grammar autocorrect."
)

iface.launch()