Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,10 @@ import os
|
|
| 4 |
|
| 5 |
# --- Model Loading ---
|
| 6 |
# We use the 'text2text-generation' pipeline.
|
| 7 |
-
# device=-1 forces CPU usage, which is required for the free tier
|
| 8 |
print("Initializing model...")
|
| 9 |
|
| 10 |
try:
|
| 11 |
-
# Attempt to load the specific grammar correction model
|
| 12 |
corrector = pipeline(
|
| 13 |
"text2text-generation",
|
| 14 |
model="HamadML/grammer_correction",
|
|
@@ -21,33 +20,26 @@ except Exception as e:
|
|
| 21 |
|
| 22 |
# --- Core Logic ---
|
| 23 |
def correct_grammar(text):
|
| 24 |
-
"""
|
| 25 |
-
Takes input text and returns the corrected version using the loaded model.
|
| 26 |
-
"""
|
| 27 |
if not text or not text.strip():
|
| 28 |
return ""
|
| 29 |
|
| 30 |
if corrector is None:
|
| 31 |
-
return "Error: The model failed to load.
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
# Generate correction
|
| 35 |
-
# max_length=128 ensures we don't run out of memory on long sentences
|
| 36 |
results = corrector(text, max_length=128)
|
| 37 |
-
|
| 38 |
-
# The pipeline returns a list of dicts: [{'generated_text': '...'}]
|
| 39 |
corrected_text = results[0]['generated_text']
|
| 40 |
return corrected_text
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error during processing: {str(e)}"
|
| 43 |
|
| 44 |
# --- User Interface ---
|
| 45 |
-
# Using Gradio Blocks for a custom layout
|
| 46 |
with gr.Blocks(title="Grammar Corrector", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 47 |
gr.Markdown(
|
| 48 |
"""
|
| 49 |
-
#
|
| 50 |
-
Type a sentence below to
|
| 51 |
"""
|
| 52 |
)
|
| 53 |
|
|
@@ -55,27 +47,23 @@ with gr.Blocks(title="Grammar Corrector", theme=gr.themes.Soft()) as demo:
|
|
| 55 |
with gr.Column():
|
| 56 |
input_box = gr.Textbox(
|
| 57 |
label="Input Text",
|
| 58 |
-
placeholder="Example: I has went to the store
|
| 59 |
lines=4
|
| 60 |
)
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
with gr.Column():
|
| 64 |
-
# Removed show_copy_button=True to fix TypeError
|
| 65 |
output_box = gr.Textbox(
|
| 66 |
label="Corrected Output",
|
| 67 |
lines=4,
|
| 68 |
interactive=False
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Event listeners
|
| 72 |
submit_btn.click(fn=correct_grammar, inputs=input_box, outputs=output_box)
|
| 73 |
input_box.submit(fn=correct_grammar, inputs=input_box, outputs=output_box)
|
| 74 |
|
| 75 |
-
gr.Markdown(
|
| 76 |
-
"Powered by [HamadML/grammer_correction](https://huggingface.co/HamadML/grammer_correction)"
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
# Launch the app
|
| 80 |
if __name__ == "__main__":
|
| 81 |
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
# --- Model Loading ---
|
| 6 |
# We use the 'text2text-generation' pipeline.
|
| 7 |
+
# device=-1 forces CPU usage, which is required for the free tier.
|
| 8 |
print("Initializing model...")
|
| 9 |
|
| 10 |
try:
|
|
|
|
| 11 |
corrector = pipeline(
|
| 12 |
"text2text-generation",
|
| 13 |
model="HamadML/grammer_correction",
|
|
|
|
| 20 |
|
| 21 |
# --- Core Logic ---
|
| 22 |
def correct_grammar(text):
|
|
|
|
|
|
|
|
|
|
| 23 |
if not text or not text.strip():
|
| 24 |
return ""
|
| 25 |
|
| 26 |
if corrector is None:
|
| 27 |
+
return "Error: The model failed to load."
|
| 28 |
|
| 29 |
try:
|
|
|
|
|
|
|
| 30 |
results = corrector(text, max_length=128)
|
|
|
|
|
|
|
| 31 |
corrected_text = results[0]['generated_text']
|
| 32 |
return corrected_text
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error during processing: {str(e)}"
|
| 35 |
|
| 36 |
# --- User Interface ---
|
|
|
|
| 37 |
with gr.Blocks(title="Grammar Corrector", theme=gr.themes.Soft()) as demo:
|
| 38 |
+
# UPDATED: Removed Emoji and "GB"
|
| 39 |
gr.Markdown(
|
| 40 |
"""
|
| 41 |
+
# Grammar Correction AI
|
| 42 |
+
Type a sentence below to check your text.
|
| 43 |
"""
|
| 44 |
)
|
| 45 |
|
|
|
|
| 47 |
with gr.Column():
|
| 48 |
input_box = gr.Textbox(
|
| 49 |
label="Input Text",
|
| 50 |
+
placeholder="Example: I has went to the store...",
|
| 51 |
lines=4
|
| 52 |
)
|
| 53 |
+
# UPDATED: Button text changed
|
| 54 |
+
submit_btn = gr.Button("Check My Text", variant="primary")
|
| 55 |
|
| 56 |
with gr.Column():
|
|
|
|
| 57 |
output_box = gr.Textbox(
|
| 58 |
label="Corrected Output",
|
| 59 |
lines=4,
|
| 60 |
interactive=False
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
submit_btn.click(fn=correct_grammar, inputs=input_box, outputs=output_box)
|
| 64 |
input_box.submit(fn=correct_grammar, inputs=input_box, outputs=output_box)
|
| 65 |
|
| 66 |
+
gr.Markdown("Powered by HamadML/grammer_correction")
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|