Xhaheen commited on
Commit
6ab17c5
β€’
1 Parent(s): e4f0de1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
18
+ word: bird
19
+ poem using word: She sights a bird, she chuckles
20
+ She flattens, then she crawls
21
+ She runs without the look of feet
22
+ Her eyes increase to Balls.
23
+
24
+ word: """
25
+
26
+ examples = [["river"], ["night"], ["trees"],["table"],["laughs"]]
27
+
28
+
29
+ def poem_generate(word):
30
+
31
+ p = prompt + word.lower() + "\n" + "poem using word: "
32
+ print(f"*****Inside poem_generate - Prompt is :{p}")
33
+ json_ = {"inputs": p,
34
+ "parameters":
35
+ {
36
+ "top_p": 0.9,
37
+ "temperature": 1.1,
38
+ "max_new_tokens": 50,
39
+ "return_full_text": False
40
+ }}
41
+ response = requests.post(API_URL, headers=headers, json=json_)
42
+ output = response.json()
43
+ print(f"Was there an error? Reason is : {output}")
44
+ output_tmp = output[0]['generated_text']
45
+ print(f"GPTJ response without splits is: {output_tmp}")
46
+ #poem = output[0]['generated_text'].split("\n\n")[0] # +"."
47
+ if "\n\n" not in output_tmp:
48
+ if output_tmp.find('.') != -1:
49
+ idx = output_tmp.find('.')
50
+ poem = output_tmp[:idx+1]
51
+ else:
52
+ idx = output_tmp.rfind('\n')
53
+ poem = output_tmp[:idx]
54
+ else:
55
+ poem = output_tmp.split("\n\n")[0] # +"."
56
+ print(f"Poem being returned is: {poem}")
57
+ return poem
58
+
59
+ def poem_to_image(poem):
60
+ print("*****Inside Poem_to_image")
61
+ poem = " ".join(poem.split('\n'))
62
+ poem = poem + " oil on canvas."
63
+ steps, width, height, images, diversity = '50','256','256','1',15
64
+ img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(poem, steps, width, height, images, diversity)[0]
65
+ return img
66
+
67
+ demo = gr.Blocks()
68
+
69
+ with demo:
70
+ gr.Markdown("<h1><center>Generate Short Poem along with an Illustration</center></h1>")
71
+ gr.Markdown(
72
+ "<div>Enter a single word you would want GPTJ-6B to write Poetry 🎀 on.</div>"
73
+ "<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!"
74
+ )
75
+ with gr.Row():
76
+ input_word = gr.Textbox(placeholder="Enter a word here to create a Poem on..")
77
+ poem_txt = gr.Textbox(lines=7)
78
+ output_image = gr.Image(type="filepath", shape=(256,256))
79
+
80
+ b1 = gr.Button("Generate Poem")
81
+ b2 = gr.Button("Generate Image")
82
+
83
+ b1.click(poem_generate, input_word, poem_txt)
84
+ b2.click(poem_to_image, poem_txt, output_image)
85
+ #examples=examples
86
+
87
+ demo.launch(enable_queue=True, debug=True)