File size: 2,532 Bytes
031f350
 
e715d8d
9962db8
 
031f350
9962db8
031f350
9962db8
e715d8d
 
9962db8
47c8401
9962db8
e715d8d
47c8401
 
e715d8d
031f350
 
 
e715d8d
 
 
 
47c8401
 
 
e715d8d
 
47c8401
e715d8d
47c8401
 
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
# 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()