Spaces:
Running
Running
File size: 2,748 Bytes
031f350 e715d8d 9962db8 031f350 9962db8 031f350 9962db8 deabf71 e715d8d 9962db8 47c8401 9962db8 e715d8d deabf71 47c8401 e715d8d 031f350 deabf71 e715d8d deabf71 47c8401 deabf71 47c8401 deabf71 47c8401 e715d8d deabf71 e715d8d deabf71 e715d8d deabf71 e715d8d deabf71 e715d8d deabf71 e715d8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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() |