Spaces:
Running
on
Zero
Running
on
Zero
Kavin0003
commited on
Commit
·
d7de9f7
1
Parent(s):
6d9f617
✨ Updated grammar logic with prompt instruction
Browse files
app.py
CHANGED
@@ -11,25 +11,33 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
|
11 |
def correct_grammar(text):
|
12 |
if not text.strip():
|
13 |
return "Please enter some text to correct."
|
14 |
-
|
15 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
16 |
-
with torch.no_grad():
|
17 |
-
outputs = model.generate(
|
18 |
-
**inputs,
|
19 |
-
max_length=512,
|
20 |
-
num_beams=5,
|
21 |
-
early_stopping=True,
|
22 |
-
do_sample=False
|
23 |
-
)
|
24 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
iface = gr.Interface(
|
27 |
fn=correct_grammar,
|
28 |
inputs=gr.Textbox(label="Enter text to correct", placeholder="Type here...", lines=3),
|
29 |
outputs=gr.Textbox(label="Corrected text", lines=3),
|
30 |
title="Grammar Correction",
|
31 |
-
description="
|
32 |
-
examples=["She go to market.", "He don't like
|
33 |
)
|
34 |
|
35 |
if __name__ == "__main__":
|
|
|
11 |
def correct_grammar(text):
|
12 |
if not text.strip():
|
13 |
return "Please enter some text to correct."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Add instruction to prompt
|
16 |
+
prompt = f"Fix grammar: {text}"
|
17 |
+
|
18 |
+
try:
|
19 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model.generate(
|
22 |
+
**inputs,
|
23 |
+
max_length=128,
|
24 |
+
num_beams=5,
|
25 |
+
early_stopping=True,
|
26 |
+
temperature=0.7,
|
27 |
+
do_sample=False
|
28 |
+
)
|
29 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
30 |
+
except Exception as e:
|
31 |
+
return f"⚠️ Error: {str(e)}"
|
32 |
+
|
33 |
+
# Gradio UI
|
34 |
iface = gr.Interface(
|
35 |
fn=correct_grammar,
|
36 |
inputs=gr.Textbox(label="Enter text to correct", placeholder="Type here...", lines=3),
|
37 |
outputs=gr.Textbox(label="Corrected text", lines=3),
|
38 |
title="Grammar Correction",
|
39 |
+
description="Fix grammar errors using a fine-tuned T5 model. Example: 'He don't like it' → 'He doesn't like it'.",
|
40 |
+
examples=["She go to market.", "I is a boy", "He don't like apples", "We was playing outside"]
|
41 |
)
|
42 |
|
43 |
if __name__ == "__main__":
|