Spaces:
Running
Running
File size: 7,660 Bytes
dda0f09 355874e dda0f09 63323a0 dda0f09 63323a0 e7753bf dda0f09 0cbc722 355874e dda0f09 63323a0 f0f38f6 34c37ad 63323a0 5938d29 690a721 2af302a dda0f09 34c37ad 63323a0 34c37ad b91b7ca e2b3807 63323a0 dda0f09 690a721 ad458d7 616e3a4 690a721 dda0f09 09ff0b3 0cbc722 dda0f09 0cbc722 dda0f09 274b2e9 dda0f09 0cbc722 dda0f09 5938d29 dda0f09 5938d29 dda0f09 |
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 |
import os
import numpy as np
from prodiapy import Prodia
import gradio as gr
import json
import requests
import base64
import random
import time
STYLE_PRESETS = [None, "3d-model", "analog-film", "anime", "cinematic", "comic-book", "digital-art", "enhance", "fantasy-art", "isometric", "line-art", "low-poly",
"neon-punk", "origami", "photographic", "pixel-art", "texture", "craft-clay"]
MAX_SEED = np.iinfo(np.int32).max
class Prodia:
def __init__(self, api_key=os.getenv("PRODIA_API_KEY"), base=None):
self.base = base or "https://api.prodia.com/v1"
self.headers = {
"X-Prodia-Key": api_key
}
def photomaker(self, params):
print(params)
response = self._post(f"{self.base}/photomaker", 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 = job
while job_result['status'] not in ['succeeded', 'failed']:
time.sleep(0.25)
job_result = self.get_job(job['job'])
return job_result
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 response
def _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 response
client = Prodia()
def generate_image(upload_images, prompt, negative_prompt, style_preset, steps, cfg_scale, strength, seed, progress=gr.Progress(track_tqdm=True)):
error_if_no_img(prompt)
print(upload_images)
params = {
"imageData": [file_to_base64(img) for img in upload_images],
"prompt": prompt,
"negative_prompt": negative_prompt,
"steps": steps,
"cfg_scale": cfg_scale,
"strength": strength,
"seed": seed if seed != 0 else random.randint(1, MAX_SEED)
}
if style_preset is not None and style_preset in STYLE_PRESETS:
params['style_preset'] = style_preset
job = client.photomaker(params)
res = client.wait(job)
if res['status'] == "failed":
return
return res['imageUrl']
def error_if_no_img(prompt):
if "img" not in prompt:
raise gr.Error("Prompt must contain 'img'")
def swap_to_gallery(images):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def upload_example_to_gallery(images, prompt, style, negative_prompt):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def remove_back_to_files():
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
def get_image_path_list(folder_name):
image_basename_list = os.listdir(folder_name)
image_path_list = sorted([os.path.join(folder_name, basename) for basename in image_basename_list])
return image_path_list
def file_to_base64(file_path):
with open(file_path, "rb") as file:
file_data = file.read()
base64_string = base64.b64encode(file_data).decode('utf-8')
return base64_string
def get_example():
case = [
[
get_image_path_list('./examples/scarletthead_woman'),
"instagram photo, portrait photo of a woman img , colorful, perfect face, natural skin, hard shadows, film grain",
None,
"(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
],
[
get_image_path_list('./examples/newton_man'),
"sci-fi, closeup portrait photo of a man img wearing the sunglasses in Iron man suit, face, slim body, high quality, film grain",
None,
"(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
],
]
return case
title = r"""
<h1 align="center">PhotoMaker: Generate images with facial consistency to input images</h1>
"""
css = '''
.gradio-container {width: 85% !important}
'''
with gr.Blocks(css=css) as demo:
gr.Markdown(title)
with gr.Row():
with gr.Column():
files = gr.File(
label="Drag (Select) 1 or more photos of your face",
file_types=["image"],
file_count="multiple"
)
uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200)
with gr.Column(visible=False) as clear_button:
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
prompt = gr.Textbox(label="Prompt",
info="Try something like 'a photo of a man/woman img', 'img' is the trigger word.",
placeholder="A photo of a [man/woman img]...")
style = gr.Dropdown(label="Style template", choices=STYLE_PRESETS, value=None)
submit = gr.Button("Submit")
with gr.Accordion(open=False, label="Advanced Options"):
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="low quality",
value="nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
)
with gr.Row():
steps = gr.Slider(
label="Number of sample steps",
minimum=20,
maximum=50,
step=1,
value=40,
)
cfg_scale = gr.Slider(
label="CFG Scale",
minimum=5,
maximum=20,
value=7,
)
with gr.Row():
strength_ratio = gr.Slider(
label="Strength (%)",
minimum=15,
maximum=50,
step=1,
value=20,
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
with gr.Column():
result_image = gr.Image(label="Generated Image")
files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
submit.click(
fn=generate_image,
inputs=[files, prompt, negative_prompt, style, steps, cfg_scale, strength_ratio, seed],
outputs=[result_image]
)
gr.Examples(
examples=get_example(),
inputs=[files, prompt, style, negative_prompt],
run_on_click=True,
fn=upload_example_to_gallery,
outputs=[uploaded_files, clear_button, files],
)
demo.queue(max_size=20).launch(show_api=False)
|