awacke1 commited on
Commit
7888440
1 Parent(s): c124a24

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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" + "poem using word: "
30
+ print(f"*****Inside poem_generate - Prompt is :{p}")
31
+ json_ = {"inputs": p,
32
+ "parameters":
33
+ {
34
+ "top_p": 0.9,
35
+ "temperature": 1.1,
36
+ "max_new_tokens": 50,
37
+ "return_full_text": False
38
+ }}
39
+ response = requests.post(API_URL, headers=headers, json=json_)
40
+ output = response.json()
41
+ print(f"If there was 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
+ poem = poem.replace('?','')
55
+ print(f"Poem being returned is: {poem}")
56
+ return poem
57
+
58
+ def poem_to_image(poem):
59
+ print("*****Inside Poem_to_image")
60
+ poem = " ".join(poem.split('\n'))
61
+ poem = poem + " oil on canvas."
62
+ steps, width, height, images, diversity = '50','256','256','1',15
63
+ img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(poem, steps, width, height, images, diversity)[0]
64
+ return img
65
+
66
+ demo = gr.Blocks()
67
+
68
+ with demo:
69
+ gr.Markdown("<h1><center>Generate Short Poem along with an Illustration</center></h1>")
70
+ gr.Markdown(
71
+ "<div>Enter a single word you would want GPTJ-6B to write Poetry 🎤 on.</div>"
72
+ "<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!"
73
+ )
74
+ with gr.Row():
75
+ input_word = gr.Textbox(placeholder="Enter a word here to create a Poem on..")
76
+ poem_txt = gr.Textbox(lines=7)
77
+ output_image = gr.Image(type="filepath", shape=(256,256))
78
+
79
+ b1 = gr.Button("Generate Poem")
80
+ b2 = gr.Button("Generate Image")
81
+
82
+ b1.click(poem_generate, input_word, poem_txt)
83
+ b2.click(poem_to_image, poem_txt, output_image)
84
+ #examples=examples
85
+
86
+ demo.launch(enable_queue=True, debug=True)