Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424 unmodified lines44744844945045145245345436 unmodified lines49149249349449549649738 unmodified lines5365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725732 unmodified lines576577578579580581582583584585586587588589590591592593594595596597598599600601602import numpy as npimport gradio as grimport requestsimport timeimport jsonimport base64import osfrom io import BytesIOimport PILfrom PIL.ExifTags import TAGSimport htmlimport refrom threading import Threadclass Prodia:def __init__(self, api_key, base=None):self.base = base or "https://api.prodia.com/v1"self.headers = {"X-Prodia-Key": api_key}def generate(self, params):response = self._post(f"{self.base}/sd/generate", params)return response.json()def transform(self, params):response = self._post(f"{self.base}/sd/transform", params)return response.json()def controlnet(self, params):response = self._post(f"{self.base}/sd/controlnet", params)return response.json()def upscale(self, params):response = self._post(f"{self.base}/upscale", params)return response.json()def get_job(self, job_id):response = self._get(f"{self.base}/job/{job_id}")return response.json()def wait(self, job):job_result = jobwhile job_result['status'] not in ['succeeded', 'failed']:time.sleep(0.5)job_result = self.get_job(job['job'])return job_resultdef list_models(self):response = self._get(f"{self.base}/sd/models")return response.json()def list_loras(self):response = self._get(f"{self.base}/sd/loras")return response.json()def _post(self, url, params):headers = {**self.headers,"Content-Type": "application/json"}response = requests.post(url, headers=headers, data=json.dumps(params))if response.status_code != 200:raise Exception(f"Bad Prodia Response: {response.status_code}")return responsedef _get(self, url):response = requests.get(url, headers=self.headers)if response.status_code != 200:raise Exception(f"Bad Prodia Response: {response.status_code}")return responsedef image_to_base64(image):# Convert the image to bytesbuffered = BytesIO()image.save(buffered, format="PNG") # You can change format to PNG if needed# Encode the bytes to base64img_str = base64.b64encode(buffered.getvalue())return img_str.decode('utf-8') # Convert bytes to stringdef remove_id_and_ext(text):text = re.sub(r'\[.*\]$', '', text)extension = text[-12:].strip()if extension == "safetensors":text = text[:-13]elif extension == "ckpt":text = text[:-4]return textdef get_data(text):results = {}patterns = {'prompt': r'(.*)','negative_prompt': r'Negative prompt: (.*)','steps': r'Steps: (\d+),','seed': r'Seed: (\d+),','sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)','model': r'Model:\s*([^\s,]+)','cfg_scale': r'CFG scale:\s*([\d\.]+)','size': r'Size:\s*([0-9]+x[0-9]+)'}for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']:match = re.search(patterns[key], text)if match:results[key] = match.group(1)else:results[key] = Noneif results['size'] is not None:w, h = results['size'].split("x")results['w'] = wresults['h'] = helse:results['w'] = Noneresults['h'] = Nonereturn resultsdef send_to_txt2img(image):result = {tabs: gr.Tabs.update(selected="t2i")}try:text = image.info['parameters']data = get_data(text)result[prompt] = gr.update(value=data['prompt'])result[negative_prompt] = gr.update(value=data['negative_prompt']) if data['negative_prompt'] is not None else gr.update()result[steps] = gr.update(value=int(data['steps'])) if data['steps'] is not None else gr.update()result[seed] = gr.update(value=int(data['seed'])) if data['seed'] is not None else gr.update()result[cfg_scale] = gr.update(value=float(data['cfg_scale'])) if data['cfg_scale'] is not None else gr.update()result[width] = gr.update(value=int(data['w'])) if data['w'] is not None else gr.update()result[height] = gr.update(value=int(data['h'])) if data['h'] is not None else gr.update()result[sampler] = gr.update(value=data['sampler']) if data['sampler'] is not None else gr.update()if data['model'] in model_names:result[model] = gr.update(value=model_names[data['model']])else:result[model] = gr.update()return resultexcept Exception as e:print(e)result[prompt] = gr.update()result[negative_prompt] = gr.update()result[steps] = gr.update()result[seed] = gr.update()result[cfg_scale] = gr.update()result[width] = gr.update()result[height] = gr.update()result[sampler] = gr.update()result[model] = gr.update()return resultdef place_lora(current_prompt, lora_name):pattern = r"<lora:" + lora_name + r":.*?>"if re.search(pattern, current_prompt):yield re.sub(pattern, "", current_prompt)else:yield current_prompt + " <lora:" + lora_name + ":1> "prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))model_list = prodia_client.list_models()lora_list = prodia_client.list_loras()model_names = {}for model_name in model_list:name_without_ext = remove_id_and_ext(model_name)model_names[name_without_ext] = model_namedef txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, batch_count, gallery):yield {text_button: gr.update(visible=False),stop_btn: gr.update(visible=True),}data = {"prompt": prompt,"negative_prompt": negative_prompt,"model": model,"steps": steps,"sampler": sampler,"cfg_scale": cfg_scale,"width": width,"height": height,"seed": seed}total_images = []threads = []def generate_one_image():result = prodia_client.generate(data)job = prodia_client.wait(result)total_images.append(job['imageUrl'])for x in range(batch_count):t = Thread(target=generate_one_image)threads.append(t)t.start()for t in threads:t.join()new_images_list = [img['name'] for img in gallery]for image in total_images:new_images_list.insert(0, image)if batch_count > 1:results = gr.update(value=total_images, preview=False)else:results = gr.update(value=total_images, preview=True)yield {text_button: gr.update(visible=True),stop_btn: gr.update(visible=False),image_output: results,gallery_obj: gr.update(value=new_images_list),}def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed,batch_count, gallery):if input_image is None:returnyield {i2i_text_button: gr.update(visible=False),i2i_stop_btn: gr.update(visible=True),}data = {"imageData": image_to_base64(input_image),"denoising_strength": denoising,"prompt": prompt,"negative_prompt": negative_prompt,"model": model,"steps": steps,"sampler": sampler,"cfg_scale": cfg_scale,"width": width,"height": height,"seed": seed}total_images = []threads = []def generate_one_image():result = prodia_client.transform(data)job = prodia_client.wait(result)total_images.append(job['imageUrl'])for x in range(batch_count):t = Thread(target=generate_one_image)threads.append(t)t.start()for t in threads:t.join()new_images_list = [img['name'] for img in gallery]for image in total_images:new_images_list.insert(0, image)if batch_count > 1:results = gr.update(value=total_images, preview=False)else:results = gr.update(value=total_images, preview=True)yield {i2i_text_button: gr.update(visible=True),i2i_stop_btn: gr.update(visible=False),i2i_image_output: results,gallery_obj: gr.update(value=new_images_list),}def upscale_fn(image, scale):if image is None:returnyield {upscale_btn: gr.update(visible=False),upscale_stop: gr.update(visible=True),}job = prodia_client.upscale({'imageData': image_to_base64(image),'resize': scale})result = prodia_client.wait(job)yield {upscale_output: result['imageUrl'],upscale_btn: gr.update(visible=True),upscale_stop: gr.update(visible=False)}def stop_upscale():return {upscale_btn: gr.update(visible=True),upscale_stop: gr.update(visible=False)}def stop_t2i():return {text_button: gr.update(visible=True),stop_btn: gr.update(visible=False)}def stop_i2i():return {i2i_text_button: gr.update(visible=True),i2i_stop_btn: gr.update(visible=False)}samplers = ["Euler","Euler a","LMS","Heun","DPM2","DPM2 a","DPM++ 2S a","DPM++ 2M","DPM++ SDE","DPM fast","DPM adaptive","LMS Karras","DPM2 Karras","DPM2 a Karras","DPM++ 2S a Karras","DPM++ 2M Karras","DPM++ SDE Karras","DDIM","PLMS",]css = """:root, .dark{--checkbox-label-gap: 0.25em 0.1em;--section-header-text-size: 12pt;--block-background-fill: transparent;}.block.padded:not(.gradio-accordion) {padding: 0 !important;}div.gradio-container{max-width: unset !important;}.compact{background: transparent !important;padding: 0 !important;}div.form{border-width: 0;box-shadow: none;background: transparent;overflow: visible;gap: 0.5em;}.block.gradio-dropdown,.block.gradio-slider,.block.gradio-checkbox,.block.gradio-textbox,.block.gradio-radio,.block.gradio-checkboxgroup,.block.gradio-number,.block.gradio-colorpicker {border-width: 0 !important;box-shadow: none !important;}.gradio-dropdown label span:not(.has-info),.gradio-textbox label span:not(.has-info),.gradio-number label span:not(.has-info){margin-bottom: 0;}.gradio-dropdown ul.options{z-index: 3000;min-width: fit-content;max-width: inherit;white-space: nowrap;}.gradio-dropdown ul.options li.item {padding: 0.05em 0;}.gradio-dropdown ul.options li.item.selected {background-color: var(--neutral-100);}.dark .gradio-dropdown ul.options li.item.selected {background-color: var(--neutral-900);}.gradio-dropdown div.wrap.wrap.wrap.wrap{box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);}.gradio-dropdown:not(.multiselect) .wrap-inner.wrap-inner.wrap-inner{flex-wrap: unset;}.gradio-dropdown .single-select{white-space: nowrap;overflow: hidden;}.gradio-dropdown .token-remove.remove-all.remove-all{display: none;}.gradio-dropdown.multiselect .token-remove.remove-all.remove-all{display: flex;}.gradio-slider input[type="number"]{width: 6em;}.block.gradio-checkbox {margin: 0.75em 1.5em 0 0;}.gradio-html div.wrap{height: 100%;}div.gradio-html.min{min-height: 0;}#model_dd {width: 16%;}"""with gr.Blocks(css=css) as demo:model = gr.Dropdown(interactive=True, value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True,label="Stable Diffusion Checkpoint", choices=prodia_client.list_models(), elem_id="model_dd")with gr.Tabs() as tabs:with gr.Tab("txt2img", id='t2i'):4 unmodified linesnegative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3,value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")with gr.Row():text_button = gr.Button("Generate", variant='primary', elem_id="generate")stop_btn = gr.Button("Cancel", variant="stop", elem_id="generate", visible=False)with gr.Row():with gr.Column():36 unmodified linesi2i_negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3,value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")with gr.Row():i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")i2i_stop_btn = gr.Button("Cancel", variant="stop", elem_id="generate", visible=False)with gr.Row():38 unmodified lineswith gr.Column():upscale_image_input = gr.Image(type="pil")upscale_btn = gr.Button("Generate", variant="primary")upscale_stop = gr.Button("Stop", variant="stop", visible=False)with gr.Tab("Scale by"):scale_by = gr.Radio([2, 4], value=2, label="Resize")upscale_output = gr.Image()with gr.Tab("PNG Info"):def plaintext_to_html(text, classname=None):content = "<br>\n".join(html.escape(x) for x in text.split('\n'))return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"def get_exif_data(image):items = image.infoinfo = ''for key, text in items.items():info += f"""<div><p><b>{plaintext_to_html(str(key))}</b></p><p>{plaintext_to_html(str(text))}</p></div>""".strip() + "\n"if len(info) == 0:message = "Nothing found in the image."info = f"<div><p>{message}<p></div>"return infowith gr.Row():with gr.Column():image_input = gr.Image(type="pil")2 unmodified linesexif_output = gr.HTML(label="EXIF Data")send_to_txt2img_btn = gr.Button("Send to txt2img")with gr.Tab("Gallery"):gallery_obj = gr.Gallery(height=1000, columns=6)t2i_event = text_button.click(txt2img,inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height,seed, batch_count, gallery_obj], outputs=[image_output, gallery_obj, text_button, stop_btn])stop_btn.click(fn=stop_t2i, outputs=[text_button, stop_btn], cancels=[t2i_event])image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input],outputs=[tabs, prompt, negative_prompt, steps, seed, model, sampler, width, height,cfg_scale])i2i_event = i2i_text_button.click(img2img,inputs=[i2i_image_input, i2i_denoising, i2i_prompt, i2i_negative_prompt,model, i2i_steps, i2i_sampler, i2i_cfg_scale, i2i_width, i2i_height,i2i_seed, i2i_batch_count, gallery_obj],outputs=[i2i_image_output, gallery_obj, i2i_text_button, i2i_stop_btn])i2i_stop_btn.click(fn=stop_i2i, outputs=[i2i_text_button, i2i_stop_btn], cancels=[i2i_event])upscale_event = upscale_btn.click(fn=upscale_fn, inputs=[upscale_image_input, scale_by], outputs=[upscale_output, upscale_btn, upscale_stop])upscale_stop.click(fn=stop_upscale, outputs=[upscale_btn, upscale_stop], cancels=[upscale_event])demo.queue().launch(max_threads=256)1234567891011121314151617184 unmodified lines23242526272829303136 unmodified lines6869707172737438 unmodified lines1131141151161171181191201211221231241252 unmodified lines128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192# original code by zenafeyfrom utils import place_lora, get_exif_datafrom css import cssfrom grutils import *import inferencelora_list = pipe.constant("/sd/loras")samplers = pipe.constant("/sd/samplers")with gr.Blocks(css=css, theme="zenafey/prodia-web") as demo:model = gr.Dropdown(interactive=True, value=model_list[0], show_label=True, label="Stable Diffusion Checkpoint",choices=model_list, elem_id="model_dd")with gr.Tabs() as tabs:with gr.Tab("txt2img", id='t2i'):4 unmodified linesnegative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3,value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")with gr.Row():t2i_generate_btn = gr.Button("Generate", variant='primary', elem_id="generate")t2i_stop_btn = gr.Button("Cancel", variant="stop", elem_id="generate", visible=False)with gr.Row():with gr.Column():36 unmodified linesi2i_negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3,value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")with gr.Row():i2i_generate_btn = gr.Button("Generate", variant='primary', elem_id="generate")i2i_stop_btn = gr.Button("Cancel", variant="stop", elem_id="generate", visible=False)with gr.Row():38 unmodified lineswith gr.Column():upscale_image_input = gr.Image(type="pil")upscale_btn = gr.Button("Generate", variant="primary")upscale_stop_btn = gr.Button("Stop", variant="stop", visible=False)with gr.Tab("Scale by"):upscale_scale = gr.Radio([2, 4], value=2, label="Resize")upscale_output = gr.Image()with gr.Tab("PNG Info"):with gr.Row():with gr.Column():image_input = gr.Image(type="pil")2 unmodified linesexif_output = gr.HTML(label="EXIF Data")send_to_txt2img_btn = gr.Button("Send to txt2img")with gr.Tab("Past generations"):inference.gr_user_history.render()t2i_event_start = t2i_generate_btn.click(update_btn_start,outputs=[t2i_generate_btn, t2i_stop_btn])t2i_event = t2i_event_start.then(inference.txt2img,inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, batch_count],outputs=[image_output])t2i_event_end = t2i_event.then(update_btn_end,outputs=[t2i_generate_btn, t2i_stop_btn])t2i_stop_btn.click(fn=update_btn_end, outputs=[t2i_generate_btn, t2i_stop_btn], cancels=[t2i_event])image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)send_to_txt2img_btn.click(fn=switch_to_t2i,outputs=[tabs]).then(fn=send_to_txt2img,inputs=[image_input],outputs=[prompt, negative_prompt, steps, seed, model, sampler, width, height, cfg_scale])i2i_event_start = i2i_generate_btn.click(update_btn_start,outputs=[i2i_generate_btn, i2i_stop_btn])i2i_event = i2i_event_start.then(inference.img2img,inputs=[i2i_image_input, i2i_denoising, i2i_prompt, i2i_negative_prompt,model, i2i_steps, i2i_sampler, i2i_cfg_scale, i2i_width, i2i_height,i2i_seed, i2i_batch_count],outputs=[i2i_image_output])i2i_event_end = i2i_event.then(update_btn_end,outputs=[i2i_generate_btn, i2i_stop_btn])i2i_stop_btn.click(fn=update_btn_end, outputs=[i2i_generate_btn, i2i_stop_btn], cancels=[i2i_event])upscale_event_start = upscale_btn.click(fn=update_btn_start,outputs=[upscale_btn, upscale_stop_btn])upscale_event = upscale_event_start.then(fn=inference.upscale,inputs=[upscale_image_input, upscale_scale],outputs=[upscale_output])upscale_event_end = upscale_event.then(fn=update_btn_end,outputs=[upscale_btn, upscale_stop_btn])upscale_stop_btn.click(fn=update_btn_end, outputs=[upscale_btn, upscale_stop_btn], cancels=[upscale_event])demo.queue().launch(max_threads=256)
app.py+80-490