-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_create_v2.py
289 lines (227 loc) · 11.1 KB
/
app_create_v2.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# app.py : Multimodal Chatbot
import logging
import time
from pprint import pprint
from dotenv import load_dotenv
import gradio as gr
from utils import parse_message, format_to_message, get_temp_file_name
################################################################
# Load .env and logging
################################################################
load_dotenv() # take environment variables from .env.
logging.basicConfig(level=logging.WARN, format='%(asctime)-15s] %(message)s', datefmt="%m/%d/%Y %I:%M:%S %p %Z")
def print(*args, **kwargs):
sep = kwargs['sep'] if 'sep' in kwargs else ' '
logging.warning(sep.join([str(val) for val in args])) # use level WARN for print, as gradio level INFO print unwanted messages
################################################################
# Global variables
################################################################
TITLE = "AI Create (v2)"
DESCRIPTION = """
# AI Create
## Generate Image from Text
* Enter description in text box and hit ENTER
## Image Editing
* Upload an image or use an image generated from text for the initial image to work on
* Enter editing instruction in text box and hit ENTER
## Inpainting
* Upload an image or drag and drop generated image to both "Image work on" and "Draw mask"
* Draw mask region for inpainting
* Leave text box empty and hit ENTER
## Local Image Editing
* Upload an image or drag and drop generated image to both "Image work on" and "Draw mask"
* Draw mask region for local editing
* Enter editing instruction in text box and hit ENTER
## TIPS:
* **Drag and drop** an image from Chatbot to Workspace to quickly change the image to work on
* make sure "Image work on" and "Draw mask" background are synced if you want to do inpainting or local image editing
* Use `prompt_strength` to balance authenticity and creativity
* Adjust `gaussian_blur_radius` to better integrate with background if mask is used
## NOTE:
* "Image work on" and "Draw mask" should be merged, but due to a bug in gradio, currently you have to make sure that they are synced for inpainting and local image editing.
"""
IMAGE_KWARGS = {} # dict(height=512)
MASK_INVERT_CHAT_ENGINES = ['stabilityai']
ATTACHMENTS = {
'image': dict(cls='Image', type="filepath", label="Image work on", **IMAGE_KWARGS),
'draw_mask': dict(cls='Image', source="upload", tool="sketch", interactive=True,
type='pil', label='Draw mask', **IMAGE_KWARGS),
'feathered_mask': dict(cls='Image',
interactive=False, label="Feathered mask", **IMAGE_KWARGS),
}
SETTINGS = {
'chat_engine': dict(cls='Radio', choices=['auto', 'stabilityai', 'dalle2'],
value='auto', interactive=True, label="Chat engine"),
}
PARAMETERS = {
'translate': dict(cls='Checkbox', interactive=True,
label="Translate", info="Translate into English may generate better results"),
'prompt_strength': dict(cls='Slider', minimum=0, maximum=1, value=0.6, step=0.05, interactive=True,
label="Prompt strength", info="Low strength for authenticity; high strength for creativity"),
'gaussian_blur_radius': dict(cls='Slider', minimum=0, maximum=100, value=10, step=1, interactive=True,
label="Gaussian blur radius", info="Gaussian blur radius for mask"),
}
KWARGS = {} # use for chatbot additional_inputs, do NOT change
################################################################
# utils
################################################################
def _create_from_dict(PARAMS, tabbed=False):
params = {}
for name, kwargs in PARAMS.items():
cls_ = kwargs['cls']; del kwargs['cls']
if not tabbed:
params[name] = getattr(gr, cls_)(**kwargs)
else:
tab_name = kwargs['label'] if 'label' in kwargs else name
with gr.Tab(tab_name):
params[name] = getattr(gr, cls_)(**kwargs)
return params
def _process_mask_image(mask_image, invert=True, radius=5):
if mask_image is None:
return None
from PIL import ImageOps
from PIL import ImageFilter
mask_image = _assure_pil_image(mask_image)
if invert:
mask_image = ImageOps.invert(mask_image.convert('RGB'))
if radius > 0:
mask_image = mask_image.filter(ImageFilter.GaussianBlur(radius=radius))
return mask_image
def _assure_pil_image(img):
if isinstance(img, str):
from PIL import Image
img = Image.open(img)
return img
def bot_fn(message, history, *args):
kwargs = {name: value for name, value in zip(KWARGS.keys(), args)}
kwargs['chat_engine'] = 'stabilityai' if kwargs['chat_engine'] == 'auto' else kwargs['chat_engine']
user_message = parse_message(message)['text']
chat_engine = kwargs['chat_engine']
try:
bot_message = None
mask = kwargs['draw_mask']
image = kwargs['image']
mask_image = mask['mask'] if isinstance(mask, dict) else mask
if chat_engine == 'stabilityai':
if kwargs['translate']:
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(
model_name="gpt-3.5-turbo",
temperature=0,
verbose=True,
)
_user_message = llm.predict(f'Translate the following sentence into English (return original text if it is already in English): {user_message}')
else:
_user_message = user_message
import stability_ai
image = stability_ai.generate(_user_message,
init_image=_assure_pil_image(image), # not arbitrary resolution
mask_image=_process_mask_image(_assure_pil_image(mask_image), radius=kwargs['gaussian_blur_radius'],
invert=True),
start_schedule=kwargs['prompt_strength'])
fname = get_temp_file_name(prefix='gradio/stabilityai-', suffix='.png')
image.save(fname)
if kwargs['translate']:
bot_message = format_to_message(dict(images=[fname], text=f'Translated prompt: {_user_message}'))
else:
bot_message = format_to_message(dict(images=[fname]))
elif chat_engine == 'dalle2':
from PIL import Image
import requests
from io import BytesIO
import dalle2
image_url = dalle2.generate(user_message,
image=_assure_pil_image(image),
mask=_process_mask_image(_assure_pil_image(mask_image), radius=0,
invert=True), # NOTE: dalle2 should not blur on mask image, prompt_strength not work
prompt_strength=kwargs['prompt_strength'])
# NOTE: image_url can not be shown in Image component
fname = get_temp_file_name(prefix='gradio/dalle2-', suffix='.png')
image = Image.open(BytesIO(requests.get(image_url).content)) # for return
image.save(fname)
bot_message = format_to_message(dict(images=[fname]))
except Exception as e:
import traceback
bot_message = traceback.format_exc()
# bot_message = 'ERROR: ' + str(e)
return bot_message, fname
################################################################
# Gradio app
################################################################
def get_demo():
# use css and elem_id to format
css="""#chatbot {
min-height: 600px;
}"""
# NOTE: can not be inside another gr.Blocks
# _chatbot = gr.Chatbot(elem_id="chatbot", avatar_images = ("assets/user.png", "assets/bot.png"))
# _textbox = gr.Textbox(container=False, show_label=False, placeholder="Type a message...", scale=10, elem_id='inputTextBox', min_width=300)
with gr.Blocks(css=css) as demo:
# title
gr.HTML(f"<center><h1>{TITLE}</h1></center>")
# description
with gr.Accordion("Expand to see Introduction and Usage", open=False):
gr.Markdown(f"{DESCRIPTION}")
with gr.Row():
# attachements, settings, and parameters
with gr.Column(scale=1):
attachments = _create_from_dict(ATTACHMENTS, tabbed=True)
with gr.Accordion("Settings", open=False) as settings_accordin:
settings = _create_from_dict(SETTINGS)
with gr.Accordion("Parameters", open=False) as parameters_accordin:
parameters = _create_from_dict(PARAMETERS)
with gr.Column(scale=9):
# chatbot
global KWARGS
KWARGS = {**attachments, **settings, **parameters}
import chat_interface
chatbot = chat_interface.ChatInterface(bot_fn, # chatbot=_chatbot, textbox=_textbox,
additional_inputs=list(KWARGS.values()),
additional_outputs=[KWARGS['image']],
upload_btn="📁",
retry_btn="Retry", undo_btn="Undo", clear_btn="Clear",
)
chatbot.chatbot.elem_id = 'chatbot' # for css
# chatbot.textbox.elem_id = 'inputTextBox' # for buttons
chatbot.chatbot.avatar_images = ("assets/user.png", "assets/bot.png")
# examples
with gr.Accordion("Examples", open=False) as examples_accordin:
create_examples = gr.Examples(
['rocket ship launching from forest with flower garden under a blue sky, masterful, ghibli',
'crayon drawing of rocket ship launching from forest',
],
inputs=chatbot.textbox, label="AI Create Examples",
)
# additional handlers
# for name, attach in attachments.items():
# if hasattr(chatbot, '_upload_fn') and isinstance(attach, gr.Image):
# attach.change(chatbot._upload_fn,
# [chatbot.textbox, attach],
# [chatbot.textbox], queue=False, api_name=False)
mask_preview = attachments['feathered_mask']
mask = attachments['draw_mask']
mask.edit(lambda x, r: _process_mask_image(x['mask'], radius=r, invert=True),
inputs=[mask, parameters['gaussian_blur_radius']], outputs=mask_preview)
parameters['gaussian_blur_radius'].change(lambda x, r: _process_mask_image(x['mask'], radius=r, invert=True),
inputs=[mask, parameters['gaussian_blur_radius']], outputs=mask_preview)
return demo
def parse_args():
"""Parse input arguments."""
import argparse
parser = argparse.ArgumentParser(
description='Multimodal Chatbot',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-p', '--port', default=7860, type=int,
help='port number.')
parser.add_argument(
'--debug', action='store_true',
help='debug mode.')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
demo = get_demo()
from utils import reload_javascript
reload_javascript()
demo.queue().launch(share=True, server_port=args.port)