Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer | |
| import torch | |
| # Load model and tokenizer | |
| model_name = "pszemraj/grammar-synthesis-small" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
| # Check if GPU is available | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| model = model.to(device) | |
| # Grammar correction function | |
| def correct_grammar(text): | |
| input_text = "gec: " + text | |
| inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True, padding=True).to(device) | |
| outputs = model.generate(inputs, max_length=512, num_beams=5, early_stopping=True) | |
| corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True) | |
| return corrected_text | |
| # Gradio Interface | |
| gr.Interface( | |
| fn=correct_grammar, | |
| inputs=gr.Textbox(lines=7, placeholder="Enter your text here...", label="Input Text"), | |
| outputs=gr.Textbox(label="Corrected Text"), | |
| title="Grammar Checker (No Java)", | |
| description="Uses a Hugging Face transformer model to fix grammar mistakes in English." | |
| ).launch() | |