my-ai-humanizer / app.py
Suleyman-AI's picture
Update app.py
e715d8d verified
raw
history blame
2.53 kB
# app.py
import gradio as gr
import spacy, re, subprocess, sys, os
from spellchecker import SpellChecker
from transformers import pipeline
subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
nlp = spacy.load("en_core_web_sm")
spell = SpellChecker(language='en')
para = pipeline("text2text-generation", model="Vamsi/T5_Paraphrase_Paws", device=-1)
def simple_correct(text):
return " ".join(spell.correction(w) or w for w in text.split())
def chunk_text(text, max_words=380):
words = text.split()
for i in range(0, len(words), max_words):
yield " ".join(words[i:i + max_words])
def humanize(text, tone, strength, freeze):
locked = [(ent.text, ent.label_) for ent in nlp(text).ents] if freeze else []
chunks_out = []
for chunk in chunk_text(text, 380):
paraphrased = para(chunk, max_length=512, do_sample=True,
temperature=0.7 * strength / 10)[0]['generated_text']
paraphrased = simple_correct(paraphrased)
for ent, label in locked:
paraphrased = re.sub(re.escape(ent), ent, paraphrased, flags=re.IGNORECASE)
chunks_out.append(paraphrased)
full_text = "\n\n".join(chunks_out)
# save downloadable file
file_path = "/tmp/humanized.txt"
with open(file_path, "w", encoding="utf-8") as f:
f.write(full_text)
return full_text, file_path
with gr.Blocks(title="AI Humanizer – Unlimited Words") as demo:
gr.Markdown("## AI Humanizer – Unlimited Words")
with gr.Row():
with gr.Column():
text_in = gr.Textbox(label="Paste your LONG text", lines=15, max_lines=None)
tone_dd = gr.Dropdown(["Casual", "Academic", "Marketing", "Legal", "Creative"], value="Casual", label="Tone")
strength_sl = gr.Slider(1, 10, value=5, label="Strength 1-10")
lock_cb = gr.Checkbox(label="Lock facts / dates / names")
submit_btn = gr.Button("Humanize", variant="primary")
with gr.Column():
text_out = gr.Textbox(label="FULL humanized text", lines=25, interactive=True)
with gr.Row():
copy_btn = gr.Button("πŸ“‹ Copy Text")
file_out = gr.File(label="Download .txt")
submit_btn.click(humanize, inputs=[text_in, tone_dd, strength_sl, lock_cb],
outputs=[text_out, file_out])
# JavaScript one-line copy
copy_btn.click(None, text_out, None,
js="(txt) => navigator.clipboard.writeText(txt)")
demo.launch()