anmolmore commited on
Commit
f74338c
1 Parent(s): 32e44f2

Upload 11 files

Browse files
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from flask import Flask, render_template, request
3
+ from difflib import HtmlDiff
4
+ import pandas as pd
5
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5ForConditionalGeneration
6
+ app = Flask(__name__)
7
+
8
+ # Load Grammarly Coedit-Large model
9
+ tokenizer = AutoTokenizer.from_pretrained("grammarly/coedit-large")
10
+ model = T5ForConditionalGeneration.from_pretrained("grammarly/coedit-large")
11
+
12
+ # Load custom dataset
13
+ custom_dataset_path = "styleguide_words.csv"
14
+ custom_dataset = pd.read_csv(custom_dataset_path)
15
+
16
+ # Create a mapping between words to be replaced and their replacements
17
+ replacement_mapping = dict(zip(custom_dataset["Not Allowed"], custom_dataset["Replacement"]))
18
+
19
+ @app.route('/')
20
+ def index():
21
+ return render_template('index.html')
22
+
23
+ @app.route('/correct', methods=['POST'])
24
+ def correct():
25
+ text = request.form['text']
26
+ corrected_text = grammar_correction(text)
27
+ return render_template('result.html', original_text=text, corrected_text=corrected_text)
28
+
29
+ @app.route('/styleguide', methods=['POST'])
30
+ def styleguide():
31
+ text = request.form['corrected_text']
32
+ highlighted_text, suggestions = apply_styleguide(text)
33
+ return render_template('styleguide.html', corrected_text=text, highlighted_text=highlighted_text, suggestions=suggestions)
34
+
35
+ @app.route('/compare', methods=['POST'])
36
+ def compare():
37
+ original_text = request.form['original_text']
38
+ final_text = request.form['final_text']
39
+ highlighted_changes = highlight_changes(original_text, final_text)
40
+ return render_template('compare.html', original_text=original_text, final_text=final_text, highlighted_changes=highlighted_changes)
41
+
42
+ def grammar_correction(text):
43
+ # Split the text into sentences
44
+ sentences = text.split(". ")
45
+ corrected_sentences = []
46
+ for sentence in sentences:
47
+ # Check if the sentence is a command (starts with "-" or contains "_")
48
+ if sentence.startswith("-") or "_" in sentence:
49
+ corrected_sentences.append(sentence) # Skip the command line
50
+ continue
51
+ # Tokenize input text
52
+ input_ids = tokenizer(sentence, return_tensors="pt").input_ids
53
+ outputs = model.generate(input_ids, max_length=256)
54
+ edited_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
55
+ corrected_sentences.append(edited_text)
56
+
57
+ # inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
58
+ # # Generate corrected text using the model
59
+ # with torch.no_grad():
60
+ # outputs = model.generate(**inputs)
61
+ # # Decode and append corrected sentence to list
62
+ # corrected_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
+ # corrected_sentences.append(corrected_sentence)
64
+
65
+ # Join the corrected sentences into a single paragraph
66
+ corrected_text = ". ".join(corrected_sentences)
67
+ return corrected_text
68
+
69
+ def apply_styleguide(text):
70
+ # Highlight words mentioned in the CSV file and suggest replacements
71
+ highlighted_text = text
72
+ suggestions = []
73
+ for not_allowed_word, replacement_word in replacement_mapping.items():
74
+ if not_allowed_word in highlighted_text:
75
+ highlighted_text = highlighted_text.replace(not_allowed_word, f'<span style="background-color: yellow">{not_allowed_word}</span> ({replacement_word})')
76
+ suggestions.append((not_allowed_word, replacement_word))
77
+ return highlighted_text, suggestions
78
+
79
+ def highlight_changes(original_text, final_text):
80
+ # Function to highlight changes between original and final text
81
+ # You can modify this function as needed
82
+ diff = HtmlDiff()
83
+ highlighted_changes = diff.make_table(original_text.splitlines(), final_text.splitlines(), context=True, numlines=2)
84
+ return highlighted_changes
85
+
86
+ if __name__ == '__main__':
87
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cpu
2
+ torch
3
+ torchvision
4
+ torchaudio
5
+
6
+ flask
7
+ pandas
8
+ transformers
static/logo.png ADDED
static/styles.css ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* styles.css */
2
+ /* Common styles */
3
+ body {
4
+ font-family: Arial, sans-serif;
5
+ margin: 0;
6
+ padding: 0;
7
+ }
8
+ header {
9
+ background-color: #f8f8f8;
10
+ padding: 20px;
11
+ display: flex;
12
+ justify-content: space-between;
13
+ align-items: center;
14
+ }
15
+ .logo img {
16
+ height: 40px;
17
+ margin-right: 10px;
18
+ }
19
+ nav ul {
20
+ list-style-type: none;
21
+ padding: 0;
22
+ }
23
+ nav ul li {
24
+ display: inline-block;
25
+ margin-right: 20px;
26
+ }
27
+ nav ul li a {
28
+ text-decoration: none;
29
+ color: #333;
30
+ }
31
+ main {
32
+ padding: 20px;
33
+ }
34
+ footer {
35
+ background-color: #333;
36
+ color: #fff;
37
+ text-align: center;
38
+ padding: 10px;
39
+ }
40
+ /* Page-specific styles */
41
+ .hero {
42
+ text-align: center;
43
+ margin-top: 50px;
44
+ }
45
+ .hero textarea {
46
+ width: 100%;
47
+ height: 200px;
48
+ padding: 10px;
49
+ margin-top: 20px;
50
+ }
51
+ .hero button {
52
+ background-color: #333;
53
+ color: #fff;
54
+ border: none;
55
+ padding: 10px 20px;
56
+ cursor: pointer;
57
+ }
58
+ .compare {
59
+ margin-top: 50px;
60
+ }
61
+ .compare h2 {
62
+ text-align: center;
63
+ }
64
+ .comparison {
65
+ display: flex;
66
+ justify-content: space-between;
67
+ }
68
+ .original, .final {
69
+ flex-basis: 45%;
70
+ }
71
+ .original h3, .final h3 {
72
+ margin-bottom: 10px;
73
+ }
styleguide_words.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Not Allowed,Replacement
2
+ guys,R&D team
3
+ slave,child
4
+ killing,canceling
templates/base.html ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>{% block title %}Your App Name{% endblock %}</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <div class="logo">
12
+ <img src="{{ url_for('static', filename='logo.png') }}" alt="Company Logo">
13
+ <h1>Editor Assistant</h1>
14
+ </div>
15
+ <nav>
16
+ <ul>
17
+ <li><a href="/">Home</a></li>
18
+ <li><a href="/compare">Compare</a></li>
19
+ <li><a href="/styleguide">Style Guide</a></li>
20
+ </ul>
21
+ </nav>
22
+ </header>
23
+ <main>
24
+ {% block content %}{% endblock %}
25
+ </main>
26
+ <footer>
27
+ <p>CIEG, India</p>
28
+ </footer>
29
+ </body>
30
+ </html>
templates/compare.html ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'base.html' %}
2
+ {% block title %}Compare - Editor Assistant{% endblock %}
3
+ {% block content %}
4
+ <section class="compare">
5
+ <h2>Comparison between Original and Final Text</h2>
6
+ <div class="comparison">
7
+ <div class="original">
8
+ <h3>Original Text</h3>
9
+ <p>{{ original_text }}</p>
10
+ </div>
11
+ <div class="final">
12
+ <h3>Final Text</h3>
13
+ <p>{{ final_text }}</p>
14
+ </div>
15
+ </div>
16
+ <div class="highlighted-changes">
17
+ {{ highlighted_changes|safe }}
18
+ </div>
19
+ <a href="/" class="button">Go Back</a>
20
+ </section>
21
+ {% endblock %}
templates/index.html ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>Home - Your App Name</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <div class="logo">
12
+ <img src="{{ url_for('static', filename='logo.png') }}" alt="Your App Logo">
13
+ </div>
14
+ <nav>
15
+ <ul>
16
+ <li><a href="/">Home</a></li>
17
+ </ul>
18
+ </nav>
19
+ </header>
20
+ <main>
21
+ <section class="hero">
22
+ <h2>Editor Assistant</h2>
23
+ <p>Start by entering your text for grammar correction.</p>
24
+ <form action="/correct" method="post">
25
+ <textarea name="text" placeholder="Enter your text here"></textarea>
26
+ <button type="submit">Submit</button>
27
+ </form>
28
+ <a href="https://wwwin.synopsys.com/~docnet/deliverables/help/synopsys_style_guide/#page/style_guide_0/writing_guidelines.htm#ww1000358364" class="button">Style Guide</a>
29
+ </section>
30
+ </main>
31
+ <footer>
32
+ <p>&copy; CIEG, India</p>
33
+ </footer>
34
+ </body>
35
+ </html>
templates/logo.png ADDED
templates/result.html ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>Result - Your App Name</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <div class="logo">
12
+ <img src="{{ url_for('static', filename='logo.png') }}" alt="Your App Logo">
13
+ </div>
14
+ <nav>
15
+ <ul>
16
+ <li><a href="/">Home</a></li>
17
+ </ul>
18
+ </nav>
19
+ </header>
20
+ <main>
21
+ <section class="result">
22
+ <h2>Grammar Correction Result</h2>
23
+ <div class="original">
24
+ <h3>Original Text</h3>
25
+ <p>{{ original_text }}</p>
26
+ </div>
27
+ <div class="corrected">
28
+ <h3>Corrected Text</h3>
29
+ <p>{{ corrected_text }}</p>
30
+ </div>
31
+ <form action="/styleguide" method="post">
32
+ <input type="hidden" name="corrected_text" value="{{ corrected_text }}">
33
+ <button type="submit">Style Guide</button>
34
+ </form>
35
+ </section>
36
+ </main>
37
+ <footer>
38
+ <p>&copy; CIEG, India</p>
39
+ </footer>
40
+ </body>
41
+ </html>
templates/styleguide.html ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>Style Guide - Your App Name</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <div class="logo">
12
+ <img src="{{ url_for('static', filename='logo.png') }}" alt="Your App Logo">
13
+ </div>
14
+ <nav>
15
+ <ul>
16
+ <li><a href="/">Home</a></li>
17
+ </ul>
18
+ </nav>
19
+ </header>
20
+ <main>
21
+ <section class="style-guide">
22
+ <h2>Style Guide</h2>
23
+ <div class="corrected">
24
+ <h3>Corrected Text</h3>
25
+ <p>{{ corrected_text }}</p>
26
+ </div>
27
+ <div class="highlighted">
28
+ <h3>Highlighted Text</h3>
29
+ <p>{{ highlighted_text|safe }}</p>
30
+ </div>
31
+ <div class="suggestions">
32
+ <h3>Suggestions</h3>
33
+ <ul>
34
+ {% for suggestion in suggestions %}
35
+ <li>{{ suggestion[0] }}: {{ suggestion[1] }}</li>
36
+ {% endfor %}
37
+ </ul>
38
+ </div>
39
+ <a href="/" class="button">Go Back</a>
40
+ </section>
41
+ </main>
42
+ <footer>
43
+ <p>&copy; CIEG, India</p>
44
+ </footer>
45
+ </body>
46
+ </html>