Vincent Claes commited on
Commit
b597b87
•
1 Parent(s): 83d644b

add api key

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -1,39 +1,44 @@
 
1
  import io
2
- import gradio as gr
3
  import requests
4
  import base64
 
 
5
  from PIL import Image
6
 
7
  # define the function that will be called when the user inputs text
8
  def get_images(text):
9
- headers = {'Content-Type': 'application/json'}
10
- params = {
11
- 'return-images': 'true',
12
- 'number-results': '4',
13
- }
14
- response = requests.post(
15
- "https://wjdr33c1id.execute-api.eu-west-1.amazonaws.com/dev/prediction",
16
- params=params,
17
- headers=headers,
18
- json={"data": text}
19
- )
20
- # get the list of image data from the response
21
- image_data = response.json()["image"]
22
- # decode the base64-encoded image data and convert it to PIL images
23
- images = [
24
- Image.open(io.BytesIO(base64.b64decode(data))) for data in image_data
25
- ]
26
- # first comes on the top
27
- images.reverse()
28
- # return the list of images
29
- return images
 
 
30
 
31
  # create the gradio app, passing the function as the input and output
32
- app = gr.Interface(get_images,
33
- gr.components.Textbox(label="Description"),
34
- # gr.components.Image(label="Images", type="pil")
35
- gr.Gallery(label="Images")
 
36
  )
37
 
38
  # start the app
39
- app.launch()
 
1
+ import os
2
  import io
 
3
  import requests
4
  import base64
5
+
6
+ import gradio as gr
7
  from PIL import Image
8
 
9
  # define the function that will be called when the user inputs text
10
  def get_images(text):
11
+ headers = {
12
+ "Content-Type": "application/json",
13
+ "x-api-key": os.environ["API_KEY"],
14
+ }
15
+ params = {
16
+ "return-images": "true",
17
+ "number-results": "4",
18
+ }
19
+ response = requests.post(
20
+ "https://wjdr33c1id.execute-api.eu-west-1.amazonaws.com/dev/prediction",
21
+ params=params,
22
+ headers=headers,
23
+ json={"data": text},
24
+ )
25
+ # get the list of image data from the response
26
+ image_data = response.json()["image"]
27
+ # decode the base64-encoded image data and convert it to PIL images
28
+ images = [Image.open(io.BytesIO(base64.b64decode(data))) for data in image_data]
29
+ # first comes on the top
30
+ images.reverse()
31
+ # return the list of images
32
+ return images
33
+
34
 
35
  # create the gradio app, passing the function as the input and output
36
+ app = gr.Interface(
37
+ get_images,
38
+ gr.components.Textbox(label="Description"),
39
+ # gr.components.Image(label="Images", type="pil")
40
+ gr.Gallery(label="Images"),
41
  )
42
 
43
  # start the app
44
+ app.launch()