Spaces:
Runtime error
Runtime error
Vincent Claes
commited on
Commit
•
b597b87
1
Parent(s):
83d644b
add api key
Browse files
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 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
# create the gradio app, passing the function as the input and output
|
32 |
-
app = gr.Interface(
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
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()
|