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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fetch import get_values
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv()
|
| 5 |
+
import prodia
|
| 6 |
+
import requests
|
| 7 |
+
import random
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
prodia_key = os.getenv('PRODIA_X_KEY', None)
|
| 12 |
+
if prodia_key is None:
|
| 13 |
+
print("Please set PRODIA_X_KEY in .env, closing...")
|
| 14 |
+
exit()
|
| 15 |
+
client = prodia.Client(api_key=prodia_key)
|
| 16 |
+
|
| 17 |
+
def process_input_text2img(prompt, negative_prompt, steps, cfg_scale, number, seed, model, sampler, aspect_ratio, upscale, save):
|
| 18 |
+
images = []
|
| 19 |
+
for image in range(number):
|
| 20 |
+
result = client.txt2img(prompt=prompt, negative_prompt=negative_prompt, model=model, sampler=sampler,
|
| 21 |
+
steps=steps, cfg_scale=cfg_scale, seed=seed, aspect_ratio=aspect_ratio, upscale=upscale)
|
| 22 |
+
images.append(result.url)
|
| 23 |
+
if save:
|
| 24 |
+
date = datetime.now()
|
| 25 |
+
if not os.path.isdir(f'./outputs/{date.year}-{date.month}-{date.day}'):
|
| 26 |
+
os.mkdir(f'./outputs/{date.year}-{date.month}-{date.day}')
|
| 27 |
+
img_data = requests.get(result.url).content
|
| 28 |
+
with open(f"./outputs/{date.year}-{date.month}-{date.day}/{random.randint(1, 10000000000000)}_{result.seed}.png", "wb") as f:
|
| 29 |
+
f.write(img_data)
|
| 30 |
+
return images
|
| 31 |
+
|
| 32 |
+
def process_input_img2img(init, prompt, negative_prompt, steps, cfg_scale, number, seed, model, sampler, ds, upscale, save):
|
| 33 |
+
images = []
|
| 34 |
+
for image in range(number):
|
| 35 |
+
result = client.img2img(imageUrl=init, prompt=prompt, negative_prompt=negative_prompt, model=model, sampler=sampler,
|
| 36 |
+
steps=steps, cfg_scale=cfg_scale, seed=seed, denoising_strength=ds, upscale=upscale)
|
| 37 |
+
images.append(result.url)
|
| 38 |
+
if save:
|
| 39 |
+
date = datetime.now()
|
| 40 |
+
if not os.path.isdir(f'./outputs/{date.year}-{date.month}-{date.day}'):
|
| 41 |
+
os.mkdir(f'./outputs/{date.year}-{date.month}-{date.day}')
|
| 42 |
+
img_data = requests.get(result.url).content
|
| 43 |
+
with open(f"./outputs/{date.year}-{date.month}-{date.day}/{random.randint(1, 10000000000000)}_{result.seed}.png", "wb") as f:
|
| 44 |
+
f.write(img_data)
|
| 45 |
+
return images
|
| 46 |
+
|
| 47 |
+
"""
|
| 48 |
+
def process_input_control(init, prompt, negative_prompt, steps, cfg_scale, number, seed, model, control_model, sampler):
|
| 49 |
+
images = []
|
| 50 |
+
for image in range(number):
|
| 51 |
+
result = client.controlnet(imageUrl=init, prompt=prompt, negative_prompt=negative_prompt, model=model, sampler=sampler,
|
| 52 |
+
steps=steps, cfg_scale=cfg_scale, seed=seed, controlnet_model=control_model)
|
| 53 |
+
images.append(result.url)
|
| 54 |
+
return images
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("""
|
| 59 |
+
# Prodia API web-ui by @zenafey
|
| 60 |
+
|
| 61 |
+
This is simple web-gui for using Prodia API easily, build on Python, gradio, prodiapy
|
| 62 |
+
""")
|
| 63 |
+
with gr.Tab(label="text2img"):
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column():
|
| 66 |
+
prompt = gr.Textbox(label="Prompt", lines=2)
|
| 67 |
+
negative = gr.Textbox(label="Negative Prompt", lines=3, placeholder="badly drawn")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
steps = gr.Slider(label="Steps", value=30, step=1, maximum=50, minimum=1, interactive=True)
|
| 71 |
+
cfg = gr.Slider(label="CFG Scale", maximum=20, minimum=1, value=7, interactive=True)
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
num = gr.Slider(label="Number of images", value=1, step=1, minimum=1, interactive=True)
|
| 75 |
+
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=4294967295, interactive=True)
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
model = gr.Dropdown(label="Model", choices=get_values()[0], value="v1-5-pruned-emaonly.ckpt [81761151]", interactive=True)
|
| 79 |
+
sampler = gr.Dropdown(label="Sampler", choices=get_values()[1], value="DDIM", interactive=True)
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
ar = gr.Radio(label="Aspect Ratio", choices=["square", "portrait", "landscape"], value="square", interactive=True)
|
| 83 |
+
with gr.Column():
|
| 84 |
+
upscale = gr.Checkbox(label="upscale", interactive=True)
|
| 85 |
+
save = gr.Checkbox(label="auto save", interactive=True)
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
run_btn = gr.Button("Run", variant="primary")
|
| 89 |
+
with gr.Column():
|
| 90 |
+
result_image = gr.Gallery(label="Result Image(s)")
|
| 91 |
+
run_btn.click(
|
| 92 |
+
process_input_text2img,
|
| 93 |
+
inputs=[
|
| 94 |
+
prompt,
|
| 95 |
+
negative,
|
| 96 |
+
steps,
|
| 97 |
+
cfg,
|
| 98 |
+
num,
|
| 99 |
+
seed,
|
| 100 |
+
model,
|
| 101 |
+
sampler,
|
| 102 |
+
ar,
|
| 103 |
+
upscale,
|
| 104 |
+
save
|
| 105 |
+
],
|
| 106 |
+
outputs=[result_image],
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with gr.Tab(label="img2img"):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column():
|
| 112 |
+
prompt = gr.Textbox(label="Prompt", lines=2)
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
negative = gr.Textbox(label="Negative Prompt", lines=3, placeholder="badly drawn")
|
| 116 |
+
init_image = gr.Textbox(label="Init Image Url", lines=2, placeholder="https://cdn.openai.com/API/images/guides/image_generation_simple.webp")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
steps = gr.Slider(label="Steps", value=30, step=1, maximum=50, minimum=1, interactive=True)
|
| 121 |
+
cfg = gr.Slider(label="CFG Scale", maximum=20, minimum=1, value=7, interactive=True)
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
num = gr.Slider(label="Number of images", value=1, step=1, minimum=1, interactive=True)
|
| 125 |
+
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=4294967295, interactive=True)
|
| 126 |
+
|
| 127 |
+
with gr.Row():
|
| 128 |
+
model = gr.Dropdown(label="Model", choices=get_values()[0], value="v1-5-pruned-emaonly.ckpt [81761151]", interactive=True)
|
| 129 |
+
sampler = gr.Dropdown(label="Sampler", choices=get_values()[1], value="DDIM", interactive=True)
|
| 130 |
+
|
| 131 |
+
with gr.Row():
|
| 132 |
+
ds = gr.Slider(label="Denoising strength", maximum=0.9, minimum=0.1, value=0.5, interactive=True)
|
| 133 |
+
with gr.Column():
|
| 134 |
+
upscale = gr.Checkbox(label="upscale", interactive=True)
|
| 135 |
+
save = gr.Checkbox(label="auto save", interactive=True)
|
| 136 |
+
|
| 137 |
+
with gr.Row():
|
| 138 |
+
run_btn = gr.Button("Run", variant="primary")
|
| 139 |
+
with gr.Column():
|
| 140 |
+
result_image = gr.Gallery(label="Result Image(s)")
|
| 141 |
+
run_btn.click(
|
| 142 |
+
process_input_img2img,
|
| 143 |
+
inputs=[
|
| 144 |
+
init_image,
|
| 145 |
+
prompt,
|
| 146 |
+
negative,
|
| 147 |
+
steps,
|
| 148 |
+
cfg,
|
| 149 |
+
num,
|
| 150 |
+
seed,
|
| 151 |
+
model,
|
| 152 |
+
sampler,
|
| 153 |
+
ds,
|
| 154 |
+
upscale,
|
| 155 |
+
save
|
| 156 |
+
],
|
| 157 |
+
outputs=[result_image],
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
with gr.Tab(label="controlnet(coming soon)"):
|
| 161 |
+
gr.Button(label="lol")
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
demo.launch(show_api=True)
|
| 166 |
+
|