Mike Bright commited on
Commit
eb60b1e
1 Parent(s): 5c1440a

adding py files

Browse files
Files changed (3) hide show
  1. app.py +53 -0
  2. backend.py +33 -0
  3. index.html +0 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+ import os
4
+
5
+ import gradio as gr
6
+ from backend import get_images_from_backend
7
+
8
+ block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
9
+ backend_url = os.environ["BACKEND_SERVER"] + "/generate"
10
+
11
+
12
+ def infer(prompt):
13
+ response = get_images_from_backend(prompt, backend_url)
14
+ return response["images"]
15
+
16
+
17
+ with block:
18
+ gr.Markdown("<h1><center>DALL·E mini</center></h1>")
19
+ gr.Markdown(
20
+ "DALL·E mini is an AI model that generates images from any prompt you give!"
21
+ )
22
+ with gr.Group():
23
+ with gr.Box():
24
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
25
+
26
+ text = gr.Textbox(
27
+ label="Enter your prompt", show_label=False, max_lines=1
28
+ ).style(
29
+ border=(True, False, True, True),
30
+ margin=False,
31
+ rounded=(True, False, False, True),
32
+ container=False,
33
+ )
34
+ btn = gr.Button("Run").style(
35
+ margin=False,
36
+ rounded=(False, True, True, False),
37
+ )
38
+ gallery = gr.Gallery(label="Generated images", show_label=False).style(
39
+ grid=[3], height="auto"
40
+ )
41
+ btn.click(infer, inputs=text, outputs=gallery)
42
+
43
+ gr.Markdown(
44
+ """___
45
+ <p style='text-align: center'>
46
+ Created by <a href="https://twitter.com/borisdayma" target="_blank">Boris Dayma</a> et al. 2021-2022
47
+ <br/>
48
+ <a href="https://github.com/borisdayma/dalle-mini" target="_blank">GitHub</a> | <a href="https://wandb.ai/dalle-mini/dalle-mini/reports/DALL-E-mini-Generate-images-from-any-text-prompt--VmlldzoyMDE4NDAy" target="_blank">Project Report</a>
49
+ </p>"""
50
+ )
51
+
52
+
53
+ block.launch(enable_queue=False)
backend.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Client requests to Dalle-Mini Backend server
2
+
3
+ import base64
4
+ from io import BytesIO
5
+
6
+ import requests
7
+ from PIL import Image
8
+
9
+
10
+ class ServiceError(Exception):
11
+ def __init__(self, status_code):
12
+ self.status_code = status_code
13
+
14
+
15
+ def get_images_from_backend(prompt, backend_url):
16
+ r = requests.post(backend_url, json={"prompt": prompt})
17
+ if r.status_code == 200:
18
+ json = r.json()
19
+ images = json["images"]
20
+ images = [Image.open(BytesIO(base64.b64decode(img))) for img in images]
21
+ version = json.get("version", "unknown")
22
+ return {"images": images, "version": version}
23
+ else:
24
+ raise ServiceError(r.status_code)
25
+
26
+
27
+ def get_model_version(url):
28
+ r = requests.get(url)
29
+ if r.status_code == 200:
30
+ version = r.json()["version"]
31
+ return version
32
+ else:
33
+ raise ServiceError(r.status_code)
index.html ADDED
File without changes