Suleyman-AI commited on
Commit
e53bcad
Β·
verified Β·
1 Parent(s): deabf71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
app.py CHANGED
@@ -8,19 +8,18 @@ subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_s
8
  nlp = spacy.load("en_core_web_sm")
9
  spell = SpellChecker(language='en')
10
 
11
- # load model once
12
  para = pipeline(
13
  "text2text-generation",
14
  model="Vamsi/T5_Paraphrase_Paws",
15
  device=-1,
16
- max_new_tokens=1024 # ← allow longer outputs per chunk
17
  )
18
 
19
  def simple_correct(text):
20
  return " ".join(spell.correction(w) or w for w in text.split())
21
 
22
- def chunk_text(text, max_words=380):
23
- """Split into exact word-length chunks."""
24
  words = text.split()
25
  for i in range(0, len(words), max_words):
26
  yield " ".join(words[i:i + max_words])
@@ -29,33 +28,30 @@ def humanize(text, tone, strength, freeze):
29
  locked = [(ent.text, ent.label_) for ent in nlp(text).ents] if freeze else []
30
  out_chunks = []
31
 
32
- for chunk in chunk_text(text, 380):
 
 
33
  paraphrased = para(
34
- chunk,
35
- max_new_tokens=1024,
36
  do_sample=True,
37
  temperature=0.7 * strength / 10
38
  )[0]['generated_text']
39
 
40
  paraphrased = simple_correct(paraphrased)
41
-
42
- # restore locked entities
43
  for ent, label in locked:
44
  paraphrased = re.sub(re.escape(ent), ent, paraphrased, flags=re.IGNORECASE)
45
-
46
  out_chunks.append(paraphrased)
47
 
48
  full_text = " ".join(out_chunks)
49
 
50
- # downloadable file
51
  file_path = "/tmp/full_humanized.txt"
52
  with open(file_path, "w", encoding="utf-8") as f:
53
  f.write(full_text)
54
-
55
  return full_text, file_path
56
 
57
  with gr.Blocks(title="AI Humanizer – Exact Length") as demo:
58
- gr.Markdown("## AI Humanizer – Exact Length, No Compression")
59
  with gr.Row():
60
  with gr.Column():
61
  text_in = gr.Textbox(label="Paste your text (any length)", lines=15, max_lines=None)
@@ -65,15 +61,14 @@ with gr.Blocks(title="AI Humanizer – Exact Length") as demo:
65
  submit_btn = gr.Button("Humanize", variant="primary")
66
 
67
  with gr.Column():
68
- text_out = gr.Textbox(label="Full humanized text (same word-count)", lines=25, interactive=True)
 
69
  with gr.Row():
70
  copy_btn = gr.Button("πŸ“‹ Copy")
71
  file_out = gr.File(label="Download .txt")
72
 
73
  submit_btn.click(humanize, inputs=[text_in, tone_dd, strength_sl, lock_cb],
74
  outputs=[text_out, file_out])
75
-
76
- copy_btn.click(None, text_out, None,
77
- js="(txt) => navigator.clipboard.writeText(txt)")
78
 
79
  demo.launch()
 
8
  nlp = spacy.load("en_core_web_sm")
9
  spell = SpellChecker(language='en')
10
 
11
+ # πŸ”§ 1) raise max_new_tokens so each chunk can be LONGER
12
  para = pipeline(
13
  "text2text-generation",
14
  model="Vamsi/T5_Paraphrase_Paws",
15
  device=-1,
16
+ max_new_tokens=2000 # 2000 tokens β‰ˆ 1 500 words
17
  )
18
 
19
  def simple_correct(text):
20
  return " ".join(spell.correction(w) or w for w in text.split())
21
 
22
+ def chunk_text(text, max_words=350):
 
23
  words = text.split()
24
  for i in range(0, len(words), max_words):
25
  yield " ".join(words[i:i + max_words])
 
28
  locked = [(ent.text, ent.label_) for ent in nlp(text).ents] if freeze else []
29
  out_chunks = []
30
 
31
+ for chunk in chunk_text(text, 350):
32
+ # πŸ”§ 2) tell the model *not to summarize*
33
+ prompt = f"Rewrite the following text naturally without shortening it:\n{chunk}"
34
  paraphrased = para(
35
+ prompt,
36
+ max_new_tokens=2000,
37
  do_sample=True,
38
  temperature=0.7 * strength / 10
39
  )[0]['generated_text']
40
 
41
  paraphrased = simple_correct(paraphrased)
 
 
42
  for ent, label in locked:
43
  paraphrased = re.sub(re.escape(ent), ent, paraphrased, flags=re.IGNORECASE)
 
44
  out_chunks.append(paraphrased)
45
 
46
  full_text = " ".join(out_chunks)
47
 
 
48
  file_path = "/tmp/full_humanized.txt"
49
  with open(file_path, "w", encoding="utf-8") as f:
50
  f.write(full_text)
 
51
  return full_text, file_path
52
 
53
  with gr.Blocks(title="AI Humanizer – Exact Length") as demo:
54
+ gr.Markdown("## AI Humanizer – Exact Length, No Shortening")
55
  with gr.Row():
56
  with gr.Column():
57
  text_in = gr.Textbox(label="Paste your text (any length)", lines=15, max_lines=None)
 
61
  submit_btn = gr.Button("Humanize", variant="primary")
62
 
63
  with gr.Column():
64
+ # πŸ”§ 3) make the output box big enough
65
+ text_out = gr.Textbox(label="Humanized text (same length)", lines=30, interactive=True)
66
  with gr.Row():
67
  copy_btn = gr.Button("πŸ“‹ Copy")
68
  file_out = gr.File(label="Download .txt")
69
 
70
  submit_btn.click(humanize, inputs=[text_in, tone_dd, strength_sl, lock_cb],
71
  outputs=[text_out, file_out])
72
+ copy_btn.click(None, text_out, None, js="(txt) => navigator.clipboard.writeText(txt)")
 
 
73
 
74
  demo.launch()