Suleyman-AI commited on
Commit
cb46076
·
verified ·
1 Parent(s): ea88d91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -35
app.py CHANGED
@@ -1,42 +1,31 @@
1
  # app.py
 
 
2
  import os
3
- openai.api_key = os.getenv("OPENAI_API_KEY")
4
- from spellchecker import SpellChecker
5
- from transformers import pipeline
6
-
7
- # download spaCy model once
8
- subprocess.check_call([sys.executable, "-m", "spacy", "download", "en_core_web_sm"])
9
- nlp = spacy.load("en_core_web_sm")
10
- spell = SpellChecker(language='en')
11
- para = pipeline("text2text-generation", model="Vamsi/T5_Paraphrase_Paws", max_new_tokens=2000)
12
 
13
- def correct(t): return " ".join(spell.correction(w) or w for w in t.split())
 
14
 
15
- def humanize(text, tone, strength, lock):
16
- locked = [(ent.text, ent.label_) for ent in nlp(text).ents] if lock else []
17
- out = []
18
- for chunk in (lambda x: [" ".join(x[i:i+380]) for i in range(0, len(x), 380)])(text.split()):
19
- p = para(chunk, max_new_tokens=2000, temperature=0.7*strength/10)[0]['generated_text']
20
- p = correct(p)
21
- for e, l in locked: p = re.sub(re.escape(e), e, p, flags=re.IGNORECASE)
22
- out.append(p)
23
- full = " ".join(out)
24
- with open("/tmp/full.txt", "w", encoding="utf-8") as f: f.write(full)
 
25
  return full, "/tmp/full.txt"
26
 
27
- with gr.Blocks(title="AI Humanizer – Unlimited") as demo:
28
- with gr.Row():
29
- with gr.Column():
30
- txt = gr.Textbox(label="Paste text (any length)", lines=15)
31
- tone = gr.Dropdown(["Casual","Academic","Marketing","Legal","Creative"], value="Casual")
32
- strg = gr.Slider(1,10,value=5,label="Strength")
33
- lock = gr.Checkbox(label="Lock facts")
34
- btn = gr.Button("Humanize", variant="primary")
35
- with gr.Column():
36
- out = gr.Textbox(label="Full humanized text", lines=25, interactive=True)
37
- with gr.Row():
38
- copy = gr.Button("📋 Copy")
39
- file = gr.File(label="Download .txt")
40
- btn.click(humanize,[txt,tone,strg,lock],[out,file])
41
- copy.click(None,out,None,js="(t)=>navigator.clipboard.writeText(t)")
42
  demo.launch()
 
1
  # app.py
2
+ import gradio as gr
3
+ import openai
4
  import os
 
 
 
 
 
 
 
 
 
5
 
6
+ # load key from Hugging Face secret
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
8
 
9
+ def humanize(text, tone, strength):
10
+ prompt = f"Rewrite the text in a {tone} tone without shortening:\n\n{text}"
11
+ res = openai.ChatCompletion.create(
12
+ model="gpt-3.5-turbo",
13
+ messages=[{"role": "user", "content": prompt}],
14
+ max_tokens=len(text.split()) + 200,
15
+ temperature=0.7 * strength / 10
16
+ )
17
+ full = res.choices[0].message.content.strip()
18
+ with open("/tmp/full.txt", "w", encoding="utf-8") as f:
19
+ f.write(full)
20
  return full, "/tmp/full.txt"
21
 
22
+ with gr.Blocks() as demo:
23
+ txt = gr.Textbox(label="Paste text", lines=15)
24
+ tone = gr.Dropdown(["Casual","Academic","Marketing","Legal","Creative"], value="Casual")
25
+ strg = gr.Slider(1,10,value=5,label="Strength")
26
+ btn = gr.Button("Humanize")
27
+ out = gr.Textbox(label="Humanized", lines=25)
28
+ file = gr.File(label="Download")
29
+ btn.click(humanize,[txt,tone,strg],[out,file])
30
+
 
 
 
 
 
 
31
  demo.launch()