shanzaejaz commited on
Commit
10f9710
·
verified ·
1 Parent(s): 798dc36

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+ import difflib
4
+
5
+ # -----------------------------
6
+ # Load Grammar Correction Model
7
+ # -----------------------------
8
+ MODEL_NAME = "vennify/t5-base-grammar-correction"
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
11
+
12
+ # -----------------------------
13
+ # Grammar correction function
14
+ # -----------------------------
15
+ def correct_grammar(text, language):
16
+ if not text.strip():
17
+ return "", ""
18
+
19
+ # Prepend instruction
20
+ input_text = "grammar: " + text
21
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
22
+
23
+ outputs = model.generate(input_ids, max_length=512)
24
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
+
26
+ # Highlight differences
27
+ diff = difflib.ndiff(text.split(), corrected_text.split())
28
+ highlighted = []
29
+ for token in diff:
30
+ if token.startswith("+"):
31
+ highlighted.append(f"**{token[2:]}**") # Added / corrected
32
+ elif token.startswith("-"):
33
+ highlighted.append(f"~~{token[2:]}~~") # Removed
34
+ else:
35
+ highlighted.append(token[2:])
36
+ highlighted_text = " ".join(highlighted)
37
+
38
+ return corrected_text, highlighted_text
39
+
40
+ # -----------------------------
41
+ # HF-ready Gradio UI
42
+ # -----------------------------
43
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
44
+ gr.Markdown("<h1 style='color:#8a2be2'>✨ Grammar Corrector</h1>")
45
+ gr.Markdown("<p style='color:#4b0082'>Correct your English sentences instantly!</p>")
46
+
47
+ with gr.Row():
48
+ with gr.Column(scale=1):
49
+ language = gr.Dropdown(
50
+ choices=["English"],
51
+ value="English",
52
+ label="Language"
53
+ )
54
+ user_input = gr.Textbox(
55
+ label="Enter text (multi-line supported)",
56
+ placeholder="Type your text here...",
57
+ lines=5
58
+ )
59
+ correct_btn = gr.Button("Correct Grammar ✅")
60
+
61
+ with gr.Column(scale=1):
62
+ corrected_output = gr.Textbox(
63
+ label="Corrected Text",
64
+ lines=5
65
+ )
66
+ highlighted_output = gr.Markdown(
67
+ label="Highlighted Changes"
68
+ )
69
+ download_btn = gr.File(label="Download Corrected Text")
70
+
71
+ # Function to generate and save corrected text
72
+ def generate_and_save(text, language):
73
+ corrected, highlighted = correct_grammar(text, language)
74
+ with open("corrected_text.txt", "w", encoding="utf-8") as f:
75
+ f.write(corrected)
76
+ return corrected, highlighted, "corrected_text.txt"
77
+
78
+ correct_btn.click(
79
+ fn=generate_and_save,
80
+ inputs=[user_input, language],
81
+ outputs=[corrected_output, highlighted_output, download_btn]
82
+ )
83
+
84
+ # Launch app (HF Spaces automatically runs this)
85
+ demo.launch()