hassaanik commited on
Commit
669e7d4
·
1 Parent(s): ccf8251

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +29 -0
  2. index.html +68 -0
  3. model.py +37 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ from model import GrammarChecker
3
+
4
+ app = Flask(__name__)
5
+ spell_checker_module = GrammarChecker()
6
+
7
+ # routes
8
+ @app.route('/')
9
+ def index():
10
+ return render_template('index.html')
11
+
12
+ @app.route('/spell',methods=['POST','GET'])
13
+ def spell():
14
+ if request.method=='POST':
15
+ text = request.form['text']
16
+ corrected_grammar = spell_checker_module.correct_grammar(text)
17
+ return render_template('index.html', corrected_grammar=corrected_grammar)
18
+
19
+ @app.route('/grammar',methods=['POST','GET'])
20
+ def grammar():
21
+ if request.method == 'POST':
22
+ file = request.files['file']
23
+ readable_file = file.read().decode('utf-8',errors='ignore')
24
+ corrected_file_grammar = spell_checker_module.correct_grammar(readable_file)
25
+ return render_template('index.html', corrected_file_grammar=corrected_file_grammar)
26
+
27
+ # python main
28
+ if __name__ == "__main__":
29
+ app.run(debug=True)
index.html ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Grammar Checker</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ margin: 0;
11
+ padding: 0;
12
+ background-color: #f4f4f4;
13
+ }
14
+
15
+ header {
16
+ background-color: #333;
17
+ color: #fff;
18
+ padding: 1em;
19
+ text-align: center;
20
+ }
21
+
22
+ main {
23
+ max-width: 800px;
24
+ margin: 20px auto;
25
+ padding: 20px;
26
+ background-color: #fff;
27
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
28
+ }
29
+
30
+ input[type="file"] {
31
+ margin-bottom: 10px;
32
+ }
33
+
34
+ button {
35
+ background-color: #4caf50;
36
+ color: #fff;
37
+ padding: 10px 20px;
38
+ border: none;
39
+ cursor: pointer;
40
+ }
41
+
42
+ button:hover {
43
+ background-color: #45a049;
44
+ }
45
+
46
+ #output {
47
+ margin-top: 20px;
48
+ }
49
+ </style>
50
+ </head>
51
+ <body>
52
+ <header>
53
+ <h1>Grammar Checker</h1>
54
+ </header>
55
+ <main>
56
+ <form action="/grammar" method="post" enctype="multipart/form-data">
57
+ <input type="file" name="file" accept=".txt" required>
58
+ <button type="submit">Check Grammar</button>
59
+ </form>
60
+ <div id="output">
61
+ {% if corrected_file_grammar %}
62
+ <p><strong>Corrected Grammar:</strong></p>
63
+ <pre>{{ corrected_file_grammar }}</pre>
64
+ {% endif %}
65
+ </div>
66
+ </main>
67
+ </body>
68
+ </html>
model.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from happytransformer import HappyTextToText, TTSettings
2
+ import difflib
3
+
4
+ grammar_model = HappyTextToText("T5", "vennify/t5-base-grammar-correction")
5
+ beam_settings = TTSettings(num_beams=5, min_length=1, max_length=20)
6
+
7
+ class GrammarChecker:
8
+ def __init__(self):
9
+ self.grammar_check = grammar_model
10
+
11
+ def correct_grammar(self, text):
12
+ # Generate corrected text from the grammar model
13
+ matches = self.grammar_check.generate_text(text, args=beam_settings)
14
+
15
+ # Extract original and corrected text
16
+ original_text = text
17
+ corrected_text = matches.text if matches and matches.text else text
18
+
19
+ # Calculate the differences between original and corrected text
20
+ differences = list(difflib.ndiff(original_text.split(), corrected_text.split()))
21
+
22
+ # Extract corrected words
23
+ corrected_words = [word[2:] for word in differences if word.startswith('+ ')]
24
+
25
+ # Calculate the total count of found mistakes
26
+ foundmistakes_count = len(corrected_words)
27
+
28
+ return corrected_text, corrected_words, foundmistakes_count
29
+
30
+ if __name__ == "__main__":
31
+ obj = GrammarChecker()
32
+ message = "they is going to spent time together."
33
+ corrected_text, corrected_words, count = obj.correct_grammar(message)
34
+
35
+ print("Output Text:", corrected_text)
36
+ print("Corrected Words:", corrected_words)
37
+ print("Count of Corrected Words:", count)