my-ai-humanizer / app.py
Suleyman-AI's picture
Update app.py
deabf71 verified
raw
history blame
2.75 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')
# load model once
para = pipeline(
"text2text-generation",
model="Vamsi/T5_Paraphrase_Paws",
device=-1,
max_new_tokens=1024 # ← allow longer outputs per chunk
)
def simple_correct(text):
return " ".join(spell.correction(w) or w for w in text.split())
def chunk_text(text, max_words=380):
"""Split into exact word-length chunks."""
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 []
out_chunks = []
for chunk in chunk_text(text, 380):
paraphrased = para(
chunk,
max_new_tokens=1024,
do_sample=True,
temperature=0.7 * strength / 10
)[0]['generated_text']
paraphrased = simple_correct(paraphrased)
# restore locked entities
for ent, label in locked:
paraphrased = re.sub(re.escape(ent), ent, paraphrased, flags=re.IGNORECASE)
out_chunks.append(paraphrased)
full_text = " ".join(out_chunks)
# downloadable file
file_path = "/tmp/full_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 – Exact Length") as demo:
gr.Markdown("## AI Humanizer – Exact Length, No Compression")
with gr.Row():
with gr.Column():
text_in = gr.Textbox(label="Paste your text (any length)", 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 (same word-count)", lines=25, interactive=True)
with gr.Row():
copy_btn = gr.Button("πŸ“‹ Copy")
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])
copy_btn.click(None, text_out, None,
js="(txt) => navigator.clipboard.writeText(txt)")
demo.launch()