sashtech commited on
Commit
69fcd05
1 Parent(s): 4e57aa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -81
app.py CHANGED
@@ -1,11 +1,32 @@
1
  import os
 
 
2
  import gradio as gr
3
  from transformers import pipeline
4
  import spacy
5
- import subprocess
6
  import nltk
7
  from nltk.corpus import wordnet
8
- from gramformer import Gramformer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Initialize the English text classification pipeline for AI detection
11
  pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
@@ -26,82 +47,14 @@ except OSError:
26
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
27
  nlp = spacy.load("en_core_web_sm")
28
 
29
- # Initialize Gramformer for grammar correction
30
- gf = Gramformer(models=1, use_gpu=False) # You can set use_gpu=True if running on a machine with a GPU
31
-
32
- # Function to correct grammar using Gramformer
33
- def correct_grammar(text):
34
- corrections = gf.correct(text)
35
- return ' '.join(corrections)
36
-
37
- # Function to get synonyms using NLTK WordNet (Humanifier)
38
- def get_synonyms_nltk(word, pos):
39
- synsets = wordnet.synsets(word, pos=pos)
40
- if synsets:
41
- lemmas = synsets[0].lemmas()
42
- return [lemma.name() for lemma in lemmas]
43
- return []
44
-
45
- # Function to capitalize the first letter of sentences and proper nouns (Humanifier)
46
- def capitalize_sentences_and_nouns(text):
47
- doc = nlp(text)
48
- corrected_text = []
49
-
50
- for sent in doc.sents:
51
- sentence = []
52
- for token in sent:
53
- if token.i == sent.start: # First word of the sentence
54
- sentence.append(token.text.capitalize())
55
- elif token.pos_ == "PROPN": # Proper noun
56
- sentence.append(token.text.capitalize())
57
- else:
58
- sentence.append(token.text)
59
- corrected_text.append(' '.join(sentence))
60
-
61
- return ' '.join(corrected_text)
62
-
63
- # Paraphrasing function using SpaCy and NLTK (Humanifier)
64
- def paraphrase_with_spacy_nltk(text):
65
- doc = nlp(text)
66
- paraphrased_words = []
67
-
68
- for token in doc:
69
- # Map SpaCy POS tags to WordNet POS tags
70
- pos = None
71
- if token.pos_ in {"NOUN"}:
72
- pos = wordnet.NOUN
73
- elif token.pos_ in {"VERB"}:
74
- pos = wordnet.VERB
75
- elif token.pos_ in {"ADJ"}:
76
- pos = wordnet.ADJ
77
- elif token.pos_ in {"ADV"}:
78
- pos = wordnet.ADV
79
-
80
- synonyms = get_synonyms_nltk(token.text.lower(), pos) if pos else []
81
-
82
- # Replace with a synonym only if it makes sense
83
- if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"} and synonyms[0] != token.text.lower():
84
- paraphrased_words.append(synonyms[0])
85
- else:
86
- paraphrased_words.append(token.text)
87
-
88
- # Join the words back into a sentence
89
- paraphrased_sentence = ' '.join(paraphrased_words)
90
-
91
- # Capitalize sentences and proper nouns
92
- corrected_text = capitalize_sentences_and_nouns(paraphrased_sentence)
93
-
94
- return corrected_text
95
-
96
- # Combined function: Paraphrase -> Capitalization (Humanifier)
97
- def paraphrase_and_correct(text):
98
- # Step 1: Paraphrase the text
99
- paraphrased_text = paraphrase_with_spacy_nltk(text)
100
-
101
- # Step 2: Capitalize sentences and proper nouns
102
- final_text = capitalize_sentences_and_nouns(paraphrased_text)
103
-
104
- return final_text
105
 
106
  # Gradio app setup with three tabs
107
  with gr.Blocks() as demo:
@@ -120,15 +73,15 @@ with gr.Blocks() as demo:
120
  output_text = gr.Textbox(label="Paraphrased Text")
121
 
122
  # Connect the paraphrasing function to the button
123
- paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
124
 
125
  with gr.Tab("Grammar Correction"):
126
  grammar_input = gr.Textbox(lines=5, label="Input Text")
127
  grammar_button = gr.Button("Correct Grammar")
128
  grammar_output = gr.Textbox(label="Corrected Text")
129
 
130
- # Connect the grammar correction function to the button
131
- grammar_button.click(correct_grammar, inputs=grammar_input, outputs=grammar_output)
132
 
133
  # Launch the app with all functionalities
134
  demo.launch()
 
1
  import os
2
+ import subprocess
3
+ import sys
4
  import gradio as gr
5
  from transformers import pipeline
6
  import spacy
 
7
  import nltk
8
  from nltk.corpus import wordnet
9
+
10
+ # Function to install GECToR
11
+ def install_gector():
12
+ if not os.path.exists('gector'):
13
+ print("Cloning GECToR repository...")
14
+ subprocess.run(["git", "clone", "https://github.com/grammarly/gector.git"], check=True)
15
+
16
+ # Install dependencies from GECToR requirements
17
+ subprocess.run([sys.executable, "-m", "pip", "install", "-r", "gector/requirements.txt"], check=True)
18
+
19
+ # Manually add GECToR to the Python path
20
+ sys.path.append(os.path.abspath('gector'))
21
+
22
+ # Install and import GECToR
23
+ install_gector()
24
+ from gector.gec_model import GecBERTModel
25
+
26
+ # Initialize GECToR model for grammar correction
27
+ gector_model = GecBERTModel(vocab_path='gector/data/output_vocabulary',
28
+ model_paths=['https://grammarly-nlp-data.s3.amazonaws.com/gector/roberta_1_gector.th'],
29
+ is_ensemble=False)
30
 
31
  # Initialize the English text classification pipeline for AI detection
32
  pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
 
47
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
48
  nlp = spacy.load("en_core_web_sm")
49
 
50
+ # Function to correct grammar using GECToR
51
+ def correct_grammar_with_gector(text):
52
+ corrected_sentences = []
53
+ sentences = [text]
54
+ for sentence in sentences:
55
+ preds = gector_model.handle_batch([sentence])
56
+ corrected_sentences.append(preds[0])
57
+ return ' '.join(corrected_sentences)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Gradio app setup with three tabs
60
  with gr.Blocks() as demo:
 
73
  output_text = gr.Textbox(label="Paraphrased Text")
74
 
75
  # Connect the paraphrasing function to the button
76
+ paraphrase_button.click(correct_grammar_with_gector, inputs=text_input, outputs=output_text)
77
 
78
  with gr.Tab("Grammar Correction"):
79
  grammar_input = gr.Textbox(lines=5, label="Input Text")
80
  grammar_button = gr.Button("Correct Grammar")
81
  grammar_output = gr.Textbox(label="Corrected Text")
82
 
83
+ # Connect the GECToR grammar correction function to the button
84
+ grammar_button.click(correct_grammar_with_gector, inputs=grammar_input, outputs=grammar_output)
85
 
86
  # Launch the app with all functionalities
87
  demo.launch()