| import torch |
| from fastapi.middleware.cors import CORSMiddleware |
| from transformers import AutoModelForSeq2SeqLM, AutoTokenizer |
| from happytransformer import HappyTextToText, TTSettings |
| import gradio as gr |
|
|
| |
| happy_tt = HappyTextToText("T5", "prithivida/grammar_error_correcter_v1") |
|
|
| def correct_grammar(text): |
| args = TTSettings(num_beams=5, min_length=1) |
| correction = happy_tt.generate_text("gec: " + text, args=args).text |
| grammar_score = 100 - abs(len(text) - len(correction)) |
| return correction, grammar_score |
|
|
| |
| def process_input(text): |
| return correct_grammar(text) |
|
|
| |
| app = gr.Interface( |
| fn=process_input, |
| inputs=gr.Textbox(placeholder="Enter text here...", label="Input Text"), |
| outputs=[ |
| gr.Textbox(label="Corrected Text"), |
| gr.Number(label="Grammar Score") |
| ], |
| title="Grammar Checker API", |
| allow_flagging="never" |
| ) |
|
|
| app.app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| if __name__ == "__main__": |
| app.launch() |