Spaces:
Runtime error
Runtime error
Commit
Β·
1b86088
1
Parent(s):
c4bd8e9
Added app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# GPT-J-6B API
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-j-6B"
|
| 6 |
+
headers = {"Authorization": "Bearer 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"}
|
| 7 |
+
prompt = """
|
| 8 |
+
word: risk
|
| 9 |
+
poem using word: And then the day came,
|
| 10 |
+
when the risk
|
| 11 |
+
to remain tight
|
| 12 |
+
in a bud
|
| 13 |
+
was more painful
|
| 14 |
+
than the risk
|
| 15 |
+
it took
|
| 16 |
+
to blossom.
|
| 17 |
+
word: bird
|
| 18 |
+
poem using word: She sights a bird, she chuckles
|
| 19 |
+
She flattens, then she crawls
|
| 20 |
+
She runs without the look of feet
|
| 21 |
+
Her eyes increase to Balls.
|
| 22 |
+
word: """
|
| 23 |
+
|
| 24 |
+
examples = [["river"], ["night"], ["trees"],["table"],["laughs"]]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def poem_generate(word):
|
| 28 |
+
|
| 29 |
+
p = prompt + word.lower() + "\n" + "rewrite the text: "
|
| 30 |
+
print(f"*****Inside Rewrite - Prompt is :{p}")
|
| 31 |
+
json_ = {"inputs": p,
|
| 32 |
+
"parameters":
|
| 33 |
+
{
|
| 34 |
+
"top_p": 0.9,
|
| 35 |
+
"temperature": 1.1,
|
| 36 |
+
"max_new_tokens": 500,
|
| 37 |
+
"return_full_text": False
|
| 38 |
+
}}
|
| 39 |
+
response = requests.post(API_URL, headers=headers, json=json_)
|
| 40 |
+
output = response.json()
|
| 41 |
+
print(f"Was there an error? Reason is : {output}")
|
| 42 |
+
output_tmp = output[0]['generated_text']
|
| 43 |
+
print(f"GPTJ response without splits is: {output_tmp}")
|
| 44 |
+
#poem = output[0]['generated_text'].split("\n\n")[0] # +"."
|
| 45 |
+
if "\n\n" not in output_tmp:
|
| 46 |
+
if output_tmp.find('.') != -1:
|
| 47 |
+
idx = output_tmp.find('.')
|
| 48 |
+
poem = output_tmp[:idx+1]
|
| 49 |
+
else:
|
| 50 |
+
idx = output_tmp.rfind('\n')
|
| 51 |
+
poem = output_tmp[:idx]
|
| 52 |
+
else:
|
| 53 |
+
poem = output_tmp.split("\n\n")[0] # +"."
|
| 54 |
+
print(f"Poem being returned is: {poem}")
|
| 55 |
+
return poem
|
| 56 |
+
|
| 57 |
+
def poem_to_image(poem):
|
| 58 |
+
print("*****Inside Poem_to_image")
|
| 59 |
+
poem = " ".join(poem.split('\n'))
|
| 60 |
+
poem = poem + " animation."
|
| 61 |
+
steps, width, height, images, diversity = '50','256','256','1',15
|
| 62 |
+
img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(poem, steps, width, height, images, diversity)[0]
|
| 63 |
+
return img
|
| 64 |
+
|
| 65 |
+
demo = gr.Blocks()
|
| 66 |
+
|
| 67 |
+
with demo:
|
| 68 |
+
gr.Markdown("<h1><center>Generate Short Poem along with an Illustration</center></h1>")
|
| 69 |
+
gr.Markdown(
|
| 70 |
+
"<div>Enter a single word you would want GPTJ-6B to write Poetry π€ on.</div>"
|
| 71 |
+
"<div>Generate an illustration π¨ provided by Latent Diffusion model.</div><div>GPJ-6B is a 6 Billion parameter autoregressive language model. It generates the Poem based on how it has been 'prompt-engineered' π€ The complete text of generated poem then goes in as a prompt to the amazing Latent Diffusion Art space by <a href='https://huggingface.co/spaces/multimodalart/latentdiffusion' target='_blank'>Multimodalart</a>.</div>Please note that some of the Poems/Illustrations might not look at par, and well, this is what happens when you can't 'cherry-pick' and post π <div> Some of the example words that you can use are 'river', 'night', 'trees', 'table', 'laughs' or maybe on similar lines to get best results!"
|
| 72 |
+
)
|
| 73 |
+
with gr.Row():
|
| 74 |
+
input_word = gr.Textbox(placeholder="Enter a word here to create a Poem on..")
|
| 75 |
+
poem_txt = gr.Textbox(lines=7)
|
| 76 |
+
output_image = gr.Image(type="filepath", shape=(256,256))
|
| 77 |
+
|
| 78 |
+
b1 = gr.Button("Generate Text")
|
| 79 |
+
b2 = gr.Button("Generate Image")
|
| 80 |
+
|
| 81 |
+
b1.click(poem_generate, input_word, poem_txt)
|
| 82 |
+
b2.click(poem_to_image, poem_txt, output_image)
|
| 83 |
+
#examples=examples
|
| 84 |
+
|
| 85 |
+
demo.launch(enable_queue=True, debug=True)
|