File size: 1,545 Bytes
b5d0fef 19f79d5 c4b2fd6 9fc880b c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef c4b2fd6 b5d0fef |
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 |
import gradio as gr
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load T5 model and tokenizer
model_name = "t5-base" # Use a smaller model for faster inference
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
# Use GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# Grammar correction function
def correct_grammar(text):
input_text = f"correct: {text}"
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
# Generate corrected text
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
# Gradio interface function
def correct_grammar_interface(text):
corrected_text = correct_grammar(text)
return corrected_text
# Gradio interface
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")
# Button click event
submit_button.click(fn=correct_grammar_interface, inputs=input_box, outputs=output_box)
# Launch the app
if __name__ == "__main__":
grammar_app.launch()
|