ysharma HF staff commited on
Commit
230feb9
β€’
1 Parent(s): 24abfe1
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ ##Bloom
6
+ API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
7
+ headers = {"Authorization": "Bearer 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"}
8
+
9
+ #def query(payload):
10
+ # response = requests.post(API_URL, headers=headers, json=payload)
11
+ # return response.json()
12
+
13
+ #output = query({
14
+ # "inputs": "Can you please let us know more details about your ",
15
+ #})
16
+
17
+
18
+ # GPT-J-6B API
19
+ #API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-j-6B"
20
+ #HF_TOKEN = os.environ["HF_TOKEN"]
21
+ #headers = {"Authorization": f"Bearer {HF_TOKEN}"}
22
+
23
+ prompt = """
24
+ word: risk
25
+ poem using word: And then the day came,
26
+ when the risk
27
+ to remain tight
28
+ in a bud
29
+ was more painful
30
+ than the risk
31
+ it took
32
+ to blossom.
33
+ word: bird
34
+ poem using word: She sights a bird, she chuckles
35
+ She flattens, then she crawls
36
+ She runs without the look of feet
37
+ Her eyes increase to Balls.
38
+ word: """
39
+
40
+ examples = [["river"], ["night"], ["trees"],["table"],["laughs"]]
41
+
42
+
43
+ def poem_generate(word):
44
+
45
+ p = prompt + word.lower() + "\n" + "poem using word: "
46
+ print(f"*****Inside poem_generate - Prompt is :{p}")
47
+ json_ = {"inputs": p,
48
+ "parameters":
49
+ {
50
+ "top_p": 0.9,
51
+ "temperature": 1.1,
52
+ "max_new_tokens": 50,
53
+ "return_full_text": False
54
+ }}
55
+ response = requests.post(API_URL, headers=headers, json=json_)
56
+ output = response.json()
57
+ print(f"If there was an error? Reason is : {output}")
58
+ output_tmp = output[0]['generated_text']
59
+ print(f"GPTJ response without splits is: {output_tmp}")
60
+ #poem = output[0]['generated_text'].split("\n\n")[0] # +"."
61
+ if "\n\n" not in output_tmp:
62
+ if output_tmp.find('.') != -1:
63
+ idx = output_tmp.find('.')
64
+ poem = output_tmp[:idx+1]
65
+ else:
66
+ idx = output_tmp.rfind('\n')
67
+ poem = output_tmp[:idx]
68
+ else:
69
+ poem = output_tmp.split("\n\n")[0] # +"."
70
+ poem = poem.replace('?','')
71
+ print(f"Poem being returned is: {poem}")
72
+ return poem
73
+
74
+ def poem_to_image(poem):
75
+ print("*****Inside Poem_to_image")
76
+ poem = " ".join(poem.split('\n'))
77
+ poem = poem + " oil on canvas."
78
+ steps, width, height, images, diversity = '50','256','256','1',15
79
+ img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(poem, steps, width, height, images, diversity)[0]
80
+ return img
81
+
82
+ demo = gr.Blocks()
83
+
84
+ with demo:
85
+ gr.Markdown("<h1><center>Generate Short Poem along with an Illustration</center></h1>")
86
+ gr.Markdown(
87
+ """Enter a single word you would want GPTJ-6B to write Poetry 🎀 on. A Space by [Yuvraj Sharma](https://huggingface.co/ysharma)."""
88
+ )
89
+ gr.Markdown("""<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!"""
90
+ )
91
+ with gr.Row():
92
+ input_word = gr.Textbox(placeholder="Enter a word here to create a Poem on..")
93
+ poem_txt = gr.Textbox(lines=7)
94
+ output_image = gr.Image(type="filepath", shape=(256,256))
95
+
96
+ b1 = gr.Button("Generate Poem")
97
+ b2 = gr.Button("Generate Image")
98
+
99
+ b1.click(poem_generate, input_word, poem_txt)
100
+ b2.click(poem_to_image, poem_txt, output_image)
101
+ #examples=examples
102
+
103
+ demo.launch(enable_queue=True, debug=True)