Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,61 @@
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
-
import spacy, re, subprocess, sys,
|
4 |
from spellchecker import SpellChecker
|
5 |
from transformers import pipeline
|
6 |
|
7 |
-
# 1. download spaCy English model
|
8 |
subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
|
9 |
nlp = spacy.load("en_core_web_sm")
|
10 |
-
|
11 |
-
# 2. lightweight spell checker
|
12 |
spell = SpellChecker(language='en')
|
|
|
|
|
13 |
def simple_correct(text):
|
14 |
return " ".join(spell.correction(w) or w for w in text.split())
|
15 |
|
16 |
-
|
17 |
-
para = pipeline("text2text-generation", model="Vamsi/T5_Paraphrase_Paws", device=-1) # CPU
|
18 |
-
|
19 |
-
def chunk_text(text, max_words=400):
|
20 |
-
"""Split text into ~max_words chunks."""
|
21 |
words = text.split()
|
22 |
for i in range(0, len(words), max_words):
|
23 |
-
yield " ".join(words[i:i+max_words])
|
24 |
|
25 |
def humanize(text, tone, strength, freeze):
|
26 |
-
# lock entities if requested
|
27 |
locked = [(ent.text, ent.label_) for ent in nlp(text).ents] if freeze else []
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
paraphrased = para(
|
33 |
-
chunk,
|
34 |
-
max_length=512,
|
35 |
-
do_sample=True,
|
36 |
-
temperature=0.7 * strength / 10
|
37 |
-
)[0]['generated_text']
|
38 |
paraphrased = simple_correct(paraphrased)
|
39 |
-
# restore locked entities in each chunk
|
40 |
for ent, label in locked:
|
41 |
paraphrased = re.sub(re.escape(ent), ent, paraphrased, flags=re.IGNORECASE)
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
# make a downloadable file
|
46 |
file_path = "/tmp/humanized.txt"
|
47 |
with open(file_path, "w", encoding="utf-8") as f:
|
48 |
-
f.write(
|
49 |
-
return
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
gr.
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
+
import spacy, re, subprocess, sys, os
|
4 |
from spellchecker import SpellChecker
|
5 |
from transformers import pipeline
|
6 |
|
|
|
7 |
subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
|
8 |
nlp = spacy.load("en_core_web_sm")
|
|
|
|
|
9 |
spell = SpellChecker(language='en')
|
10 |
+
para = pipeline("text2text-generation", model="Vamsi/T5_Paraphrase_Paws", device=-1)
|
11 |
+
|
12 |
def simple_correct(text):
|
13 |
return " ".join(spell.correction(w) or w for w in text.split())
|
14 |
|
15 |
+
def chunk_text(text, max_words=380):
|
|
|
|
|
|
|
|
|
16 |
words = text.split()
|
17 |
for i in range(0, len(words), max_words):
|
18 |
+
yield " ".join(words[i:i + max_words])
|
19 |
|
20 |
def humanize(text, tone, strength, freeze):
|
|
|
21 |
locked = [(ent.text, ent.label_) for ent in nlp(text).ents] if freeze else []
|
22 |
+
chunks_out = []
|
23 |
+
for chunk in chunk_text(text, 380):
|
24 |
+
paraphrased = para(chunk, max_length=512, do_sample=True,
|
25 |
+
temperature=0.7 * strength / 10)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
paraphrased = simple_correct(paraphrased)
|
|
|
27 |
for ent, label in locked:
|
28 |
paraphrased = re.sub(re.escape(ent), ent, paraphrased, flags=re.IGNORECASE)
|
29 |
+
chunks_out.append(paraphrased)
|
30 |
+
full_text = "\n\n".join(chunks_out)
|
31 |
|
32 |
+
# save downloadable file
|
|
|
33 |
file_path = "/tmp/humanized.txt"
|
34 |
with open(file_path, "w", encoding="utf-8") as f:
|
35 |
+
f.write(full_text)
|
36 |
+
return full_text, file_path
|
37 |
+
|
38 |
+
with gr.Blocks(title="AI Humanizer – Unlimited Words") as demo:
|
39 |
+
gr.Markdown("## AI Humanizer – Unlimited Words")
|
40 |
+
with gr.Row():
|
41 |
+
with gr.Column():
|
42 |
+
text_in = gr.Textbox(label="Paste your LONG text", lines=15, max_lines=None)
|
43 |
+
tone_dd = gr.Dropdown(["Casual", "Academic", "Marketing", "Legal", "Creative"], value="Casual", label="Tone")
|
44 |
+
strength_sl = gr.Slider(1, 10, value=5, label="Strength 1-10")
|
45 |
+
lock_cb = gr.Checkbox(label="Lock facts / dates / names")
|
46 |
+
submit_btn = gr.Button("Humanize", variant="primary")
|
47 |
+
|
48 |
+
with gr.Column():
|
49 |
+
text_out = gr.Textbox(label="FULL humanized text", lines=25, interactive=True)
|
50 |
+
with gr.Row():
|
51 |
+
copy_btn = gr.Button("📋 Copy Text")
|
52 |
+
file_out = gr.File(label="Download .txt")
|
53 |
+
|
54 |
+
submit_btn.click(humanize, inputs=[text_in, tone_dd, strength_sl, lock_cb],
|
55 |
+
outputs=[text_out, file_out])
|
56 |
+
|
57 |
+
# JavaScript one-line copy
|
58 |
+
copy_btn.click(None, text_out, None,
|
59 |
+
js="(txt) => navigator.clipboard.writeText(txt)")
|
60 |
+
|
61 |
+
demo.launch()
|