Spaces:
Runtime error
Runtime error
Commit ·
8637ff9
1
Parent(s): 928dc00
`app.py`: img2img
Browse files68 unmodified lines69707172737475767778798081828369 unmodified lines1531541551561571581592 unmodified lines16216316416516616716810 unmodified lines1791801811821831841 unmodified line1861871881891901911921931941 unmodified line19619719819920020149 unmodified lines25125225325425525625731 unmodified lines28929029129229329468 unmodified linesreturn responsedef image_to_base64(image_path):# Open the image with PILwith Image.open(image_path) as 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 string69 unmodified linesreturn resultprodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))model_list = prodia_client.list_models()model_names = {}2 unmodified linesname_without_ext = remove_id_and_ext(model_name)model_names[name_without_ext] = model_namedef flip_text(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):result = prodia_client.generate({"prompt": prompt,"negative_prompt": negative_prompt,10 unmodified linesreturn job["imageUrl"]css = """#generate {1 unmodified line}"""with gr.Blocks(css=css) as demo:with gr.Row():with gr.Column(scale=6):model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())1 unmodified linewith gr.Column(scale=1):gr.Markdown(elem_id="powered-by-prodia", value="AUTOMATIC1111 Stable Diffusion Web UI.<br>Powered by [Prodia](https://prodia.com).<br> For more features and faster gen times check out our [API Docs](https://docs.prodia.com/reference/getting-started-guide)")with gr.Tabs() as tabs:with gr.Tab("txt2img", id='t2i'):with gr.Row():49 unmodified lineswith gr.Column(scale=2):image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")text_button.click(flip_text, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)with gr.Tab("PNG Info"):def plaintext_to_html(text, classname=None):31 unmodified linesimage_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])demo.queue(concurrency_count=32)demo.launch()68 unmodified lines6970717273747576777879808169 unmodified lines1511521531541551562 unmodified lines15916016116216316416510 unmodified lines1761771781791801811821831841851861871881891901911921931941951961971981992001 unmodified line2022032042052062072082092102112122132142152162172182192202212222232242252262272282292301 unmodified line23223323423523623723849 unmodified lines28828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233331 unmodified lines36536636736836937068 unmodified linesreturn 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 string69 unmodified linesreturn resultprodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))model_list = prodia_client.list_models()model_names = {}2 unmodified linesname_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):result = prodia_client.generate({"prompt": prompt,"negative_prompt": negative_prompt,10 unmodified linesreturn job["imageUrl"]def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):result = prodia_client.transform({"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})job = prodia_client.wait(result)return job["imageUrl"]css = """#generate {1 unmodified line}"""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",]with gr.Blocks(css=css) as demo:with gr.Row():with gr.Column(scale=6):model = gr.Dropdown(interactive=True,value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True, label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())1 unmodified linewith gr.Column(scale=1):gr.Markdown(elem_id="powered-by-prodia", value="AUTOMATIC1111 Stable Diffusion Web UI.<br>Powered by [Prodia](https://prodia.com).<br> For more features and faster gen times check out our [API Docs](https://docs.prodia.com/reference/getting-started-guide)")with gr.Tabs() as tabs:with gr.Tab("txt2img", id='t2i'):with gr.Row():49 unmodified lineswith gr.Column(scale=2):image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed], outputs=image_output)with gr.Tab("img2img", id='i2i'):with gr.Row():with gr.Column(scale=6, min_width=600):i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)i2i_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.Column():i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")with gr.Row():with gr.Column(scale=3):with gr.Tab("Generation"):i2i_image_input = gr.Image(type="pil")with gr.Row():with gr.Column(scale=1):i2i_sampler = gr.Dropdown(value="Euler a", show_label=True, label="Sampling Method", choices=samplers)with gr.Column(scale=1):i2i_steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=30, value=25, step=1)with gr.Row():with gr.Column(scale=1):i2i_width = gr.Slider(label="Width", maximum=1024, value=512, step=8)i2i_height = gr.Slider(label="Height", maximum=1024, value=512, step=8)with gr.Column(scale=1):i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)i2i_batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)i2i_seed = gr.Number(label="Seed", value=-1)with gr.Column(scale=2):i2i_image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")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], outputs=i2i_image_output)with gr.Tab("PNG Info"):def plaintext_to_html(text, classname=None):31 unmodified linesimage_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])demo.queue(concurrency_count=32)demo.launch()
app.py+91-15