sashtech commited on
Commit
f036c05
·
verified ·
1 Parent(s): e91f3ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -61
app.py CHANGED
@@ -6,55 +6,32 @@ import subprocess
6
  import nltk
7
  from nltk.corpus import wordnet
8
 
9
- # Initialize the English text classification pipeline for AI detection
10
- pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
11
-
12
- # Ensure necessary NLTK data is downloaded for Humanifier
13
  nltk.download('wordnet')
14
  nltk.download('omw-1.4')
15
 
16
- # Ensure the SpaCy model is installed for Humanifier
17
  try:
18
  nlp = spacy.load("en_core_web_sm")
19
  except OSError:
20
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
21
  nlp = spacy.load("en_core_web_sm")
22
 
23
- # Function to check subject-verb agreement
24
- def check_subject_verb_agreement(doc):
25
- corrected_text = []
26
- for token in doc:
27
- if token.dep_ == "nsubj": # Check if the token is a subject
28
- subject = token
29
- verb = token.head # Find the associated verb
30
- if verb.tag_ in {"VBZ", "VBP"}: # Singular/plural verb forms
31
- if subject.tag_ == "NNS" and verb.tag_ == "VBZ": # Plural subject with singular verb
32
- corrected_text.append(verb.lemma_) # Convert verb to plural form
33
- elif subject.tag_ == "NN" and verb.tag_ == "VBP": # Singular subject with plural verb
34
- corrected_text.append(verb.lemma_ + 's') # Convert verb to singular form
35
- else:
36
- corrected_text.append(verb.text) # No correction needed
37
- else:
38
- corrected_text.append(verb.text)
39
- corrected_text.append(token.text)
40
- return ' '.join(corrected_text)
41
 
42
- # Function to correct singular/plural errors using dependency parsing
43
- def correct_singular_plural_errors(doc):
44
- corrected_text = []
45
-
46
- for token in doc:
47
- if token.pos_ == "NOUN":
48
- if token.tag_ == "NN" and token.head.pos_ == "VERB" and token.head.tag_ == "VBP":
49
- corrected_text.append(token.lemma_ + 's') # Singular noun, plural verb
50
- elif token.tag_ == "NNS" and token.head.pos_ == "VERB" and token.head.tag_ == "VBZ":
51
- corrected_text.append(token.lemma_) # Plural noun, singular verb
52
- else:
53
- corrected_text.append(token.text)
54
- else:
55
- corrected_text.append(token.text)
56
-
57
- return ' '.join(corrected_text)
58
 
59
  # Paraphrasing function using SpaCy and NLTK (Humanifier)
60
  def paraphrase_with_spacy_nltk(text):
@@ -81,34 +58,20 @@ def paraphrase_with_spacy_nltk(text):
81
  else:
82
  paraphrased_words.append(token.text)
83
 
84
- return ' '.join(paraphrased_words)
 
 
 
85
 
86
  # Combined function: Paraphrase -> Grammar Correction -> Capitalization (Humanifier)
87
  def paraphrase_and_correct(text):
88
  # Step 1: Paraphrase the text
89
  paraphrased_text = paraphrase_with_spacy_nltk(text)
90
 
91
- # Step 2: Parse the text with spaCy
92
- doc = nlp(paraphrased_text)
93
-
94
- # Step 3: Apply grammatical corrections on the paraphrased text
95
- corrected_text = correct_article_errors(doc)
96
-
97
- corrected_text = capitalize_sentences_and_nouns(corrected_text)
98
 
99
- corrected_text = check_subject_verb_agreement(nlp(corrected_text))
100
-
101
- corrected_text = correct_singular_plural_errors(nlp(corrected_text))
102
-
103
- # Step 4: Capitalize sentences and proper nouns (final correction step)
104
- final_text = correct_tense_errors(nlp(corrected_text))
105
-
106
- return final_text
107
- def predict_en(text):
108
- prediction = pipeline_en(text)
109
- label = prediction[0]['label']
110
- score = prediction[0]['score']
111
- return label, round(score, 4)
112
 
113
  # Gradio app setup with two tabs
114
  with gr.Blocks() as demo:
@@ -130,4 +93,4 @@ with gr.Blocks() as demo:
130
  paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
131
 
132
  # Launch the app with the remaining functionalities
133
- demo.launch()
 
6
  import nltk
7
  from nltk.corpus import wordnet
8
 
9
+ # Ensure necessary NLTK data is downloaded
 
 
 
10
  nltk.download('wordnet')
11
  nltk.download('omw-1.4')
12
 
13
+ # Ensure the SpaCy model is installed
14
  try:
15
  nlp = spacy.load("en_core_web_sm")
16
  except OSError:
17
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
18
  nlp = spacy.load("en_core_web_sm")
19
 
20
+ # Initialize the English text classification pipeline for AI detection
21
+ pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # Function to predict the label and score for English text (AI Detection)
24
+ def predict_en(text):
25
+ res = pipeline_en(text)[0]
26
+ return res['label'], res['score']
27
+
28
+ # Function to get synonyms using NLTK WordNet (Humanifier)
29
+ def get_synonyms_nltk(word, pos):
30
+ synsets = wordnet.synsets(word, pos=pos)
31
+ if synsets:
32
+ lemmas = synsets[0].lemmas()
33
+ return [lemma.name() for lemma in lemmas]
34
+ return []
 
 
 
 
35
 
36
  # Paraphrasing function using SpaCy and NLTK (Humanifier)
37
  def paraphrase_with_spacy_nltk(text):
 
58
  else:
59
  paraphrased_words.append(token.text)
60
 
61
+ # Join the words back into a sentence
62
+ paraphrased_sentence = ' '.join(paraphrased_words)
63
+
64
+ return paraphrased_sentence
65
 
66
  # Combined function: Paraphrase -> Grammar Correction -> Capitalization (Humanifier)
67
  def paraphrase_and_correct(text):
68
  # Step 1: Paraphrase the text
69
  paraphrased_text = paraphrase_with_spacy_nltk(text)
70
 
71
+ # Additional steps (grammar correction, capitalization) can go here...
72
+ # For now, we'll return the paraphrased text as an example.
 
 
 
 
 
73
 
74
+ return paraphrased_text
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  # Gradio app setup with two tabs
77
  with gr.Blocks() as demo:
 
93
  paraphrase_button.click(paraphrase_and_correct, inputs=text_input, outputs=output_text)
94
 
95
  # Launch the app with the remaining functionalities
96
+ demo.launch()