my-ai-humanizer / app.py
Suleyman-AI's picture
Update app.py
e53bcad verified
# 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')
# πŸ”§ 1) raise max_new_tokens so each chunk can be LONGER
para = pipeline(
"text2text-generation",
model="Vamsi/T5_Paraphrase_Paws",
device=-1,
max_new_tokens=2000 # 2000 tokens β‰ˆ 1 500 words
)
def simple_correct(text):
return " ".join(spell.correction(w) or w for w in text.split())
def chunk_text(text, max_words=350):
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, 350):
# πŸ”§ 2) tell the model *not to summarize*
prompt = f"Rewrite the following text naturally without shortening it:\n{chunk}"
paraphrased = para(
prompt,
max_new_tokens=2000,
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)
out_chunks.append(paraphrased)
full_text = " ".join(out_chunks)
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 Shortening")
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():
# πŸ”§ 3) make the output box big enough
text_out = gr.Textbox(label="Humanized text (same length)", lines=30, 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()