murat4595 commited on
Commit
7dc1531
·
verified ·
1 Parent(s): 571eb84

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ 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 unless you have a GPU upgrade.
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",
15
+ device=-1
16
+ )
17
+ print("Model loaded successfully.")
18
+ except Exception as e:
19
+ print(f"Error loading model: {e}")
20
+ corrector = None
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. Please check the 'Logs' tab in your Space."
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
+ # 🇬🇧 Grammar Correction AI
50
+ Type a sentence below to fix grammar, spelling, and punctuation errors.
51
+ """
52
+ )
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ input_box = gr.Textbox(
57
+ label="Input Text",
58
+ placeholder="Example: I has went to the store yesterday...",
59
+ lines=4
60
+ )
61
+ submit_btn = gr.Button("Fix Grammar", variant="primary")
62
+
63
+ with gr.Column():
64
+ output_box = gr.Textbox(
65
+ label="Corrected Output",
66
+ lines=4,
67
+ interactive=False,
68
+ show_copy_button=True
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()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers>=4.30.0
2
+ torch
3
+ gradio
4
+ sentencepiece
5
+ protobuf