File size: 1,110 Bytes
754c265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from fastapi.middleware.cors import CORSMiddleware
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from happytransformer import HappyTextToText, TTSettings
import gradio as gr

# Load models
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))  # Simplified scoring
    return correction, grammar_score

# Gradio interface
def process_input(text):
    return correct_grammar(text)

# Add CORS support
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()