shreeramy commited on
Commit
ea97277
·
1 Parent(s): cacec0e

Add application file

Browse files
Files changed (3) hide show
  1. app.py +165 -0
  2. nltk_requirements.py +8 -0
  3. requirements.txt +57 -0
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from nltk import pos_tag, ne_chunk
3
+ from nltk.tokenize import word_tokenize, sent_tokenize
4
+ from nltk.tokenize.treebank import TreebankWordDetokenizer
5
+ from nltk.corpus import wordnet, brown
6
+ from spellchecker import SpellChecker
7
+ import re
8
+ import nltk
9
+
10
+ # Preload resources
11
+ detokenizer = TreebankWordDetokenizer()
12
+ global_freq_dist = nltk.FreqDist(w.lower() for w in brown.words())
13
+
14
+ def extract_proper_nouns(text):
15
+ """
16
+ Extracts proper nouns such as PERSON, ORGANIZATION, and GPE from the given text.
17
+ Returns a set of detected proper nouns in lowercase.
18
+ """
19
+ tokens = word_tokenize(text)
20
+ tagged = pos_tag(tokens)
21
+ chunks = ne_chunk(tagged)
22
+ proper_nouns = set()
23
+
24
+ for chunk in chunks:
25
+ if hasattr(chunk, 'label') and chunk.label() in ('PERSON', 'ORGANIZATION', 'GPE'):
26
+ entity = " ".join(c[0] for c in chunk)
27
+ proper_nouns.add(entity.lower())
28
+ return proper_nouns
29
+
30
+ def protect_proper_nouns(text, dynamic_entities):
31
+ """
32
+ Identifies and marks proper nouns to prevent them from being altered.
33
+ Returns a list of tuples (word, is_protected).
34
+ """
35
+ tokens = word_tokenize(text)
36
+ protected = []
37
+ for token in tokens:
38
+ lower_token = token.lower()
39
+ if any(lower_token in entity for entity in dynamic_entities):
40
+ protected.append((token, True))
41
+ else:
42
+ protected.append((token, False))
43
+ return protected
44
+
45
+ def replace_words_with_synonyms(tokens_with_protection, pos_tags):
46
+ """
47
+ Replaces words with synonyms while maintaining readability and ensuring proper nouns remain unchanged.
48
+ """
49
+ new_tokens = []
50
+ for (token, protected), (_, tag) in zip(tokens_with_protection, pos_tags):
51
+ if protected or not token.isalpha():
52
+ new_tokens.append(token)
53
+ continue
54
+
55
+ pos = None
56
+ if tag.startswith('JJ'):
57
+ pos = wordnet.ADJ
58
+ elif tag.startswith('RB'):
59
+ pos = wordnet.ADV
60
+ elif tag.startswith('NN'):
61
+ pos = wordnet.NOUN
62
+ elif tag.startswith('VB'):
63
+ pos = wordnet.VERB
64
+
65
+ if pos:
66
+ candidates = []
67
+ for syn in wordnet.synsets(token, pos=pos):
68
+ for lemma in syn.lemmas():
69
+ candidate = lemma.name().replace('_', ' ')
70
+ if candidate.lower() == token.lower() or ' ' in candidate:
71
+ continue
72
+ if len(candidate) > len(token) * 1.2:
73
+ continue
74
+ candidates.append(candidate)
75
+ if candidates:
76
+ best_candidate = max(candidates,
77
+ key=lambda x: global_freq_dist[x.lower()],
78
+ default=token)
79
+ new_tokens.append(best_candidate)
80
+ else:
81
+ new_tokens.append(token)
82
+ else:
83
+ new_tokens.append(token)
84
+ return new_tokens
85
+
86
+ def restructure_sentences(text):
87
+ """
88
+ Splits long sentences for better readability.
89
+ Uses punctuation-based splitting if necessary.
90
+ """
91
+ sentences = sent_tokenize(text)
92
+ restructured = []
93
+ for sent in sentences:
94
+ if len(sent.split()) > 25:
95
+ parts = re.split(r'[,;]', sent)
96
+ if len(parts) > 1:
97
+ sent = parts[0] + '. ' + ' '.join(parts[1:])
98
+ restructured.append(sent)
99
+ return ' '.join(restructured)
100
+
101
+ def contextual_spell_check(text):
102
+ """
103
+ Performs spell checking while ensuring proper nouns remain unchanged.
104
+ """
105
+ protected = protect_proper_nouns(text, extract_proper_nouns(text))
106
+ spell = SpellChecker()
107
+ corrected = []
108
+ for token, protected_flag in protected:
109
+ if protected_flag or not token.isalpha():
110
+ corrected.append(token)
111
+ continue
112
+ correction = spell.correction(token)
113
+ if correction:
114
+ if token[0].isupper():
115
+ corrected.append(correction.capitalize())
116
+ else:
117
+ corrected.append(correction.lower())
118
+ else:
119
+ corrected.append(token)
120
+ return detokenizer.detokenize(corrected)
121
+
122
+ def finalize_formatting(text):
123
+ """
124
+ Cleans up text formatting including spaces before punctuation, quote styles, and em dashes.
125
+ """
126
+ text = re.sub(r'\s+([.,!?;:])', r'\1', text)
127
+ text = re.sub(r'([(])\s+', r'\1', text)
128
+ text = re.sub(r'\s+([)])', r'\1', text)
129
+ text = re.sub(r'\"(.*?)\"', r'“\1”', text)
130
+ text = re.sub(r' -- ', r' — ', text)
131
+ return text.strip()
132
+
133
+ def humanize_text(text):
134
+ """
135
+ Applies synonym replacement, sentence restructuring, and other enhancements to make text more natural.
136
+ """
137
+ dynamic_entities = extract_proper_nouns(text)
138
+ protected_tokens = protect_proper_nouns(text, dynamic_entities)
139
+ tokens = [t[0] for t in protected_tokens]
140
+ tags = pos_tag(tokens)
141
+ new_tokens = replace_words_with_synonyms(protected_tokens, tags)
142
+ text = detokenizer.detokenize(new_tokens)
143
+ return restructure_sentences(text)
144
+
145
+ def process_text(input_text):
146
+ """
147
+ Processes input text to enhance readability by applying all transformations sequentially.
148
+ """
149
+ if input_text:
150
+ humanized = humanize_text(input_text)
151
+ spell_checked = contextual_spell_check(humanized)
152
+ final_output = finalize_formatting(spell_checked)
153
+ return final_output
154
+ return ""
155
+
156
+ # Gradio Interface
157
+ iface = gr.Interface(
158
+ fn=process_text,
159
+ inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
160
+ outputs=gr.Textbox(lines=5),
161
+ title="AI to Humanized Text Converter",
162
+ description="Enter text and get a more humanized, readable output.",
163
+ )
164
+
165
+ iface.launch()
nltk_requirements.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import nltk
2
+
3
+ nltk.download('punkt')
4
+ nltk.download('averaged_perceptron_tagger')
5
+ nltk.download('maxent_ne_chunker')
6
+ nltk.download('words')
7
+ nltk.download('wordnet')
8
+ nltk.download('brown')
requirements.txt ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ annotated-types==0.7.0
3
+ anyio==4.8.0
4
+ certifi==2025.1.31
5
+ charset-normalizer==3.4.1
6
+ click==8.1.8
7
+ colorama==0.4.6
8
+ fastapi==0.115.8
9
+ ffmpy==0.5.0
10
+ filelock==3.17.0
11
+ fsspec==2025.2.0
12
+ gradio==5.16.2
13
+ gradio_client==1.7.1
14
+ h11==0.14.0
15
+ httpcore==1.0.7
16
+ httpx==0.28.1
17
+ huggingface-hub==0.29.0
18
+ idna==3.10
19
+ Jinja2==3.1.5
20
+ joblib==1.4.2
21
+ markdown-it-py==3.0.0
22
+ MarkupSafe==2.1.5
23
+ mdurl==0.1.2
24
+ nltk==3.9.1
25
+ numpy==2.2.3
26
+ orjson==3.10.15
27
+ packaging==24.2
28
+ pandas==2.2.3
29
+ pillow==11.1.0
30
+ pydantic==2.10.6
31
+ pydantic_core==2.27.2
32
+ pydub==0.25.1
33
+ Pygments==2.19.1
34
+ pyspellchecker==0.8.2
35
+ python-dateutil==2.9.0.post0
36
+ python-multipart==0.0.20
37
+ pytz==2025.1
38
+ PyYAML==6.0.2
39
+ regex==2024.11.6
40
+ requests==2.32.3
41
+ rich==13.9.4
42
+ ruff==0.9.6
43
+ safehttpx==0.1.6
44
+ semantic-version==2.10.0
45
+ shellingham==1.5.4
46
+ six==1.17.0
47
+ sniffio==1.3.1
48
+ starlette==0.45.3
49
+ tomlkit==0.13.2
50
+ tqdm==4.67.1
51
+ typer==0.15.1
52
+ typing_extensions==4.12.2
53
+ tzdata==2025.1
54
+ urllib3==2.3.0
55
+ uvicorn==0.34.0
56
+ websockets==14.2
57
+