Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 14 |
|
| 15 |
-
def humanize(text, tone, strength
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
full =
|
| 24 |
-
with open("/tmp/full.txt", "w", encoding="utf-8") as f:
|
|
|
|
| 25 |
return full, "/tmp/full.txt"
|
| 26 |
|
| 27 |
-
with gr.Blocks(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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()
|