Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,16 +40,16 @@ if torch.cuda.is_available():
|
|
| 40 |
|
| 41 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 42 |
|
| 43 |
-
def inference(model, img, strength, prompt, guidance, steps, seed):
|
| 44 |
|
| 45 |
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
| 46 |
|
| 47 |
if img is not None:
|
| 48 |
-
return
|
| 49 |
else:
|
| 50 |
-
return
|
| 51 |
|
| 52 |
-
def
|
| 53 |
|
| 54 |
global current_model
|
| 55 |
global pipe
|
|
@@ -63,14 +63,15 @@ def text_inference(model, prompt, guidance, steps, generator=None):
|
|
| 63 |
prompt = prompt_prefixes[current_model] + prompt
|
| 64 |
image = pipe(
|
| 65 |
prompt,
|
|
|
|
| 66 |
num_inference_steps=int(steps),
|
| 67 |
guidance_scale=guidance,
|
| 68 |
-
width=
|
| 69 |
-
height=
|
| 70 |
generator=generator).images[0]
|
| 71 |
return image
|
| 72 |
|
| 73 |
-
def
|
| 74 |
|
| 75 |
global current_model
|
| 76 |
global pipe
|
|
@@ -82,16 +83,17 @@ def img_inference(model, prompt, img, strength, guidance, steps, generator):
|
|
| 82 |
pipe = pipe.to("cuda")
|
| 83 |
|
| 84 |
prompt = prompt_prefixes[current_model] + prompt
|
| 85 |
-
ratio = min(
|
| 86 |
img = img.resize((int(img.width * ratio), int(img.height * ratio)))
|
| 87 |
image = pipe(
|
| 88 |
prompt,
|
|
|
|
| 89 |
init_image=img,
|
| 90 |
num_inference_steps=int(steps),
|
| 91 |
strength=strength,
|
| 92 |
guidance_scale=guidance,
|
| 93 |
-
width=
|
| 94 |
-
height=
|
| 95 |
generator=generator).images[0]
|
| 96 |
return image
|
| 97 |
|
|
@@ -139,32 +141,35 @@ with gr.Blocks(css=css) as demo:
|
|
| 139 |
with gr.Row():
|
| 140 |
|
| 141 |
with gr.Column():
|
| 142 |
-
|
| 143 |
model = gr.Dropdown(label="Model", choices=models, value=models[0])
|
| 144 |
prompt = gr.Textbox(label="Prompt", placeholder="Style prefix is applied automatically")
|
| 145 |
-
with gr.
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
run = gr.Button(value="Run")
|
| 155 |
-
gr.Markdown(f"Running on: {device}")
|
| 156 |
with gr.Column():
|
| 157 |
image_out = gr.Image(height=512)
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 161 |
gr.Examples([
|
| 162 |
[models[0], "jason bateman disassembling the demon core", 7.5, 50],
|
| 163 |
[models[3], "portrait of dwayne johnson", 7.0, 75],
|
| 164 |
[models[4], "portrait of a beautiful alyx vance half life", 10, 50],
|
| 165 |
[models[5], "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7, 45],
|
| 166 |
[models[4], "fantasy portrait painting, digital art", 4, 30],
|
| 167 |
-
], [model, prompt, guidance, steps], image_out,
|
| 168 |
gr.Markdown('''
|
| 169 |
Models by [@nitrosocke](https://huggingface.co/nitrosocke), [@Helixngc7293](https://twitter.com/DGSpitzer) and others. ❤️<br>
|
| 170 |
Space by: [](https://twitter.com/hahahahohohe)
|
|
|
|
| 40 |
|
| 41 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 42 |
|
| 43 |
+
def inference(model, img, strength, prompt, neg_prompt, guidance, steps, width, height, seed):
|
| 44 |
|
| 45 |
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
| 46 |
|
| 47 |
if img is not None:
|
| 48 |
+
return txt_to_img(model, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator)
|
| 49 |
else:
|
| 50 |
+
return img_to_img(model, prompt, neg_prompt, guidance, steps, width, height, generator)
|
| 51 |
|
| 52 |
+
def img_to_img(model, prompt, neg_prompt, guidance, steps, width, height, generator=None):
|
| 53 |
|
| 54 |
global current_model
|
| 55 |
global pipe
|
|
|
|
| 63 |
prompt = prompt_prefixes[current_model] + prompt
|
| 64 |
image = pipe(
|
| 65 |
prompt,
|
| 66 |
+
negative_prompt=neg_prompt,
|
| 67 |
num_inference_steps=int(steps),
|
| 68 |
guidance_scale=guidance,
|
| 69 |
+
width=width,
|
| 70 |
+
height=height,
|
| 71 |
generator=generator).images[0]
|
| 72 |
return image
|
| 73 |
|
| 74 |
+
def txt_to_img(model, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
| 75 |
|
| 76 |
global current_model
|
| 77 |
global pipe
|
|
|
|
| 83 |
pipe = pipe.to("cuda")
|
| 84 |
|
| 85 |
prompt = prompt_prefixes[current_model] + prompt
|
| 86 |
+
ratio = min(height / img.height, width / img.width)
|
| 87 |
img = img.resize((int(img.width * ratio), int(img.height * ratio)))
|
| 88 |
image = pipe(
|
| 89 |
prompt,
|
| 90 |
+
negative_prompt=neg_prompt,
|
| 91 |
init_image=img,
|
| 92 |
num_inference_steps=int(steps),
|
| 93 |
strength=strength,
|
| 94 |
guidance_scale=guidance,
|
| 95 |
+
width=width,
|
| 96 |
+
height=height,
|
| 97 |
generator=generator).images[0]
|
| 98 |
return image
|
| 99 |
|
|
|
|
| 141 |
with gr.Row():
|
| 142 |
|
| 143 |
with gr.Column():
|
|
|
|
| 144 |
model = gr.Dropdown(label="Model", choices=models, value=models[0])
|
| 145 |
prompt = gr.Textbox(label="Prompt", placeholder="Style prefix is applied automatically")
|
| 146 |
+
with gr.Tab("Options"):
|
| 147 |
+
|
| 148 |
+
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
| 149 |
+
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
| 150 |
+
steps = gr.Slider(label="Steps", value=50, maximum=100, minimum=2)
|
| 151 |
+
width = gr.Slider(label="Width", value=512, maximum=1024, minimum=64)
|
| 152 |
+
height = gr.Slider(label="Height", value=512, maximum=1024, minimum=64)
|
| 153 |
+
seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
|
| 154 |
+
with gr.Tab("Image to image"):
|
| 155 |
+
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
| 156 |
+
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
| 157 |
|
|
|
|
|
|
|
| 158 |
with gr.Column():
|
| 159 |
image_out = gr.Image(height=512)
|
| 160 |
+
run = gr.Button(value="Run")
|
| 161 |
+
gr.Markdown(f"Running on: {device}")
|
| 162 |
|
| 163 |
+
inputs = [model, image, strength, prompt, neg_prompt, guidance, steps, width, height, seed]
|
| 164 |
+
prompt.submit(inference, inputs=inputs, outputs=image_out)
|
| 165 |
+
run.click(inference, inputs=inputs, outputs=image_out)
|
| 166 |
gr.Examples([
|
| 167 |
[models[0], "jason bateman disassembling the demon core", 7.5, 50],
|
| 168 |
[models[3], "portrait of dwayne johnson", 7.0, 75],
|
| 169 |
[models[4], "portrait of a beautiful alyx vance half life", 10, 50],
|
| 170 |
[models[5], "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7, 45],
|
| 171 |
[models[4], "fantasy portrait painting, digital art", 4, 30],
|
| 172 |
+
], [model, prompt, guidance, steps], image_out, img_to_img, cache_examples=False)
|
| 173 |
gr.Markdown('''
|
| 174 |
Models by [@nitrosocke](https://huggingface.co/nitrosocke), [@Helixngc7293](https://twitter.com/DGSpitzer) and others. ❤️<br>
|
| 175 |
Space by: [](https://twitter.com/hahahahohohe)
|