-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
181 lines (156 loc) · 5.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import io
import json
import os
import sys
import gradio as gr
import boto3
import numpy as np
import torch
from PIL import Image
from botocore.exceptions import ClientError
from dotenv import load_dotenv
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
load_dotenv()
model_id = "stabilityai/stable-diffusion-2"
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, safety_checker=None)
pipe = pipe.to(os.environ['ARCH'])
# Recommended if your computer has < 64 GB of RAM
pipe.enable_attention_slicing()
svc = boto3.client(
service_name='s3',
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
endpoint_url=os.getenv('AWS_ENDPOINT_URL_S3'),
)
DEFAULT_PROMPT = "frankie with tigris"
DEFAULT_IMAGE = Image.open("Tigris-fly-sticker.png")
BUCKET_NAME = os.getenv('BUCKET_NAME')
def gen_image(prompt: str) -> Image.Image:
"""
tentatively flips the image
:param prompt: prompt for image generation
:return: generated image
"""
if not prompt:
prompt = DEFAULT_PROMPT
print(f"generating images for {prompt}", file=sys.stderr)
return pipe(prompt).images[0]
def load_image(key: str):
"""
saves images back to the repository
:param key:
:return:
"""
print(f"begin load image {key}", file=sys.stdout)
if key == "":
raise gr.Error("key name is empty")
try:
response = svc.get_object(
Bucket=BUCKET_NAME,
Key=key,
)
return Image.open(io.BytesIO(response['Body'].read())), response['Metadata']
except ClientError as e:
error_code = e.response["Error"]["Code"]
if error_code == "NotFound":
raise gr.Error("key " + key + "not found")
raise gr.Error("get object error " + error_code)
def save_image(inp_img: np.ndarray, key: str, metadata: str, prompt: str):
"""
saves images back to the repository
:param inp_img:
:param key:
:param metadata:
:param prompt:
:return:
"""
print(f"begin save image under {key} {metadata} {prompt}", file=sys.stdout)
if key == "":
raise gr.Error("key name is empty")
metadata = {
"prompt": prompt,
"comment": metadata,
}
print(f"metadata is {json.dumps(metadata, indent=2)}", file=sys.stdout)
byte_writer = io.BytesIO()
inp_img.save(byte_writer, 'png')
try:
response = svc.put_object(
Bucket=BUCKET_NAME,
Key=key,
Metadata=metadata,
Body=byte_writer.getvalue(),
ContentType="image/png",
)
gr.Info("Image " + key + " saved in Tigris")
except ClientError as e:
error_code = e.response["Error"]["Code"]
raise gr.Error("put object error " + error_code)
return
with gr.Blocks(
title="Text-to-Image",
theme=gr.themes.Default(font=["sans-serif"])) as demo:
with gr.Row():
with gr.Column(scale=3):
gr.Markdown(
"""
# Text-to-Image Generator with Hugging Face model, Fly GPUs and Tigris as storage
"""
)
with gr.Column(scale=0):
pass
with gr.Row():
with gr.Column(scale=0):
pass
with gr.Column(scale=3):
image_output = gr.Image(label="Image",
show_label=False,
value=DEFAULT_IMAGE,
width=600,
height=600,
type="pil",
interactive=False)
with gr.Column(scale=2):
gr.Markdown(
"""
## Enter a phrase
Generate images with huggingface [stable diffusion 2](https://huggingface.co/stabilityai/stable-diffusion-2)
model.
"""
)
gen_prompt_input = gr.Textbox(show_label=False, placeholder=DEFAULT_PROMPT)
gen_btn = gr.Button("Generate image", variant="primary")
with gr.Row():
with gr.Column(scale=0):
pass
with gr.Column(scale=2):
gr.Markdown(
"""
## Save your image in Tigris or load an existing image from Tigris
"""
)
img_key_input = gr.Textbox(
label="", interactive=True, show_label=False,
placeholder="key name for this image")
metadata_input = gr.Textbox(
label="", show_label=False,
placeholder="Optional: Store metadata with the image")
with gr.Row():
save_image_btn = gr.Button("Save image", variant="primary")
load_image_btn = gr.Button("Load image", variant="primary")
clear = gr.ClearButton(
[gen_prompt_input, img_key_input, metadata_input],
value="Clear")
with gr.Column(scale=2):
pass
# set generate button to populate the image on the right side
gen_btn.click(fn=gen_image, inputs=gen_prompt_input, outputs=[image_output], api_name="generate_image")
gen_prompt_input.submit(fn=gen_image, inputs=gen_prompt_input, outputs=[image_output])
save_image_btn.click(fn=save_image,
inputs=[image_output, img_key_input, metadata_input, gen_prompt_input],
api_name="save_image")
load_image_btn.click(fn=load_image, inputs=[img_key_input],
outputs=[image_output, metadata_input],
api_name="load_image")
demo.launch(server_name='0.0.0.0', max_threads=4, server_port=8888)