| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
|
|
|
|
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
| from transformers import MarianMTModel, MarianTokenizer |
| import gradio as gr |
| import torch |
|
|
| |
| device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| |
| tokenizer_gc = AutoTokenizer.from_pretrained("vennify/t5-base-grammar-correction") |
| model_gc = AutoModelForSeq2SeqLM.from_pretrained("vennify/t5-base-grammar-correction").to(device) |
|
|
| |
| |
| sp_model_name = "Helsinki-NLP/opus-mt-en-es" |
| tokenizer_es = MarianTokenizer.from_pretrained(sp_model_name) |
| model_es = MarianMTModel.from_pretrained(sp_model_name).to(device) |
|
|
| |
| ar_model_name = "Helsinki-NLP/opus-mt-en-ar" |
| tokenizer_ar = MarianTokenizer.from_pretrained(ar_model_name) |
| model_ar = MarianMTModel.from_pretrained(ar_model_name).to(device) |
|
|
| |
| def correct_grammar(sentence): |
| input_text = f"grammar: {sentence}" |
| tokens = tokenizer_gc.encode(input_text, return_tensors="pt").to(device) |
| with torch.no_grad(): |
| outputs = model_gc.generate(tokens, max_length=64, num_beams=4, early_stopping=True) |
| corrected = tokenizer_gc.decode(outputs[0], skip_special_tokens=True) |
| return corrected |
|
|
| |
| def translate_to_spanish(text): |
| batch = tokenizer_es.prepare_seq2seq_batch([text], return_tensors="pt").to(device) |
| with torch.no_grad(): |
| generated = model_es.generate(**batch) |
| return tokenizer_es.decode(generated[0], skip_special_tokens=True) |
|
|
| |
| def translate_to_arabic(text): |
| batch = tokenizer_ar.prepare_seq2seq_batch([text], return_tensors="pt").to(device) |
| with torch.no_grad(): |
| generated = model_ar.generate(**batch) |
| return tokenizer_ar.decode(generated[0], skip_special_tokens=True) |
|
|
| |
| def pipeline(sentence): |
| corrected = correct_grammar(sentence) |
| grammar_status = "Correct" if corrected.strip().lower() == sentence.strip().lower() else "Corrected" |
| spanish = translate_to_spanish(corrected) |
| arabic = translate_to_arabic(corrected) |
| return { |
| "Input Sentence": sentence, |
| "Grammar Status": grammar_status, |
| "Corrected Sentence": corrected, |
| "Spanish Translation": spanish, |
| "Arabic Translation": arabic |
| } |
|
|
| |
| iface = gr.Interface( |
| fn=pipeline, |
| inputs=gr.Textbox(label="Enter a sentence in English"), |
| outputs="json", |
| title="π Grammar Checker + π Translator (Fast Version)", |
| description="Checks grammar, corrects if needed, and translates the corrected sentence to Spanish and Arabic using optimized models." |
| ) |
|
|
| if __name__ == "__main__": |
| |
| print("Warming up models...") |
| _ = pipeline("This is a test sentence.") |
| iface.launch(debug=True) |
|
|