gaur3009 commited on
Commit
754c265
·
verified ·
1 Parent(s): 4b9008b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
+ from happytransformer import HappyTextToText, TTSettings
5
+ import gradio as gr
6
+
7
+ # Load models
8
+ happy_tt = HappyTextToText("T5", "prithivida/grammar_error_correcter_v1")
9
+
10
+ def correct_grammar(text):
11
+ args = TTSettings(num_beams=5, min_length=1)
12
+ correction = happy_tt.generate_text("gec: " + text, args=args).text
13
+ grammar_score = 100 - abs(len(text) - len(correction)) # Simplified scoring
14
+ return correction, grammar_score
15
+
16
+ # Gradio interface
17
+ def process_input(text):
18
+ return correct_grammar(text)
19
+
20
+ # Add CORS support
21
+ app = gr.Interface(
22
+ fn=process_input,
23
+ inputs=gr.Textbox(placeholder="Enter text here...", label="Input Text"),
24
+ outputs=[
25
+ gr.Textbox(label="Corrected Text"),
26
+ gr.Number(label="Grammar Score")
27
+ ],
28
+ title="Grammar Checker API",
29
+ allow_flagging="never"
30
+ )
31
+
32
+ app.app.add_middleware(
33
+ CORSMiddleware,
34
+ allow_origins=["*"],
35
+ allow_methods=["*"],
36
+ allow_headers=["*"],
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ app.launch()