|
import gradio as gr |
|
import torch |
|
from transformers import T5Tokenizer, T5ForConditionalGeneration |
|
|
|
|
|
model_name = "t5-base" |
|
tokenizer = T5Tokenizer.from_pretrained(model_name) |
|
model = T5ForConditionalGeneration.from_pretrained(model_name) |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model.to(device) |
|
|
|
|
|
def correct_grammar(text): |
|
input_text = f"correct: {text}" |
|
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device) |
|
|
|
|
|
output_ids = model.generate(input_ids, max_length=512, num_beams=5, early_stopping=True) |
|
corrected_text = tokenizer.decode(output_ids[0], skip_special_tokens=True) |
|
|
|
return corrected_text |
|
|
|
|
|
def correct_grammar_interface(text): |
|
corrected_text = correct_grammar(text) |
|
return corrected_text |
|
|
|
|
|
with gr.Blocks() as grammar_app: |
|
gr.Markdown("<h1>Fast Grammar Correction with T5</h1>") |
|
|
|
with gr.Row(): |
|
input_box = gr.Textbox(label="Input Text", placeholder="Enter text to be corrected", lines=4) |
|
output_box = gr.Textbox(label="Corrected Text", placeholder="Corrected text will appear here", lines=4) |
|
|
|
submit_button = gr.Button("Correct Grammar") |
|
|
|
|
|
submit_button.click(fn=correct_grammar_interface, inputs=input_box, outputs=output_box) |
|
|
|
|
|
if __name__ == "__main__": |
|
grammar_app.launch() |
|
|