File size: 6,901 Bytes
5d09ff5 c93cbfc 5d09ff5 c93cbfc 5d09ff5 88b8c75 5d09ff5 c93cbfc 0055997 d9ca905 0055997 d9ca905 0055997 c93cbfc 1e50263 c93cbfc 5d09ff5 4de3b89 4b892a4 5d09ff5 ff60762 88b8c75 c93cbfc 5d09ff5 c93cbfc 5d09ff5 c93cbfc 5d09ff5 c93cbfc 5d09ff5 0055997 5d09ff5 c93cbfc 5d09ff5 d1c6074 5d09ff5 9aa7219 5d09ff5 9aa7219 5d09ff5 c93cbfc 0055997 c93cbfc 5d09ff5 c93cbfc 5d09ff5 0055997 5d09ff5 34ff48f 5d09ff5 d1c6074 5d09ff5 0055997 2209df5 5d09ff5 147c862 | 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 | import os
import math
import gradio as gr
from Helper_functions import *
from Kaggle_API import API_Connection
from GoogleDrive_API import GoogleDrive_API
DEFAULT_VALUES = {
"input_image": None,
"edit_instruction": "",
"steps": 100,
"randomize_seed": "Fix Seed",
"seed": 1371,
"randomize_cfg": "Fix CFG",
"text_cfg_scale": 7.5,
"image_cfg_scale": 1.5,
"resolution": 512,
"edited_image": None
}
HELP_TEXT = """
If you're not getting what you want, there may be a few reasons:
1. Is the image not changing enough? Your Image CFG weight may be too high. This value dictates how similar the output should be to the input. It's possible your edit requires larger changes from the original image, and your Image CFG weight isn't allowing that. Alternatively, your Text CFG weight may be too low. This value dictates how much to listen to the text instruction. The default Image CFG of 1.5 and Text CFG of 7.5 are a good starting point, but aren't necessarily optimal for each edit. Try:
* Decreasing the Image CFG weight
* Incerasing the Text CFG weight
2. Conversely, is the image changing too much, such that the details in the original image aren't preserved? Try:
* Increasing the Image CFG weight
* Decreasing the Text CFG weight
3. Try generating results with different random seeds by setting "Randomize Seed" and running generation multiple times. You can also try setting "Randomize CFG" to sample new Text CFG and Image CFG values each time.
4. Rephrasing the instruction sometimes improves results (e.g., "turn him into a dog" vs. "make him a dog" vs. "as a dog").
5. Increasing the number of steps sometimes improves results.
6. Do faces look weird? The Stable Diffusion autoencoder has a hard time with faces that are small in the image. Try:
* Cropping the image so the face takes up a larger portion of the frame.
"""
def generate_button_clicked(*args):
# set kaggle-api variables
kaggle_username = os.environ["kaggle_username"]
kaggle_key = os.environ["kaggle_key"]
input_keys = list(DEFAULT_VALUES.keys())
values = dict(zip(input_keys, list(args)))
for key in values:
if values[key] is None:
values[key] = DEFAULT_VALUES[key]
if values["randomize_seed"]:
values["seed"] = random.randint(1, 100000)
if values["randomize_cfg"]:
values["text_cfg_scale"] = round(random.uniform(6.0, 9.0), ndigits=2)
values["image_cfg_scale"] = round(random.uniform(1.2, 1.8), ndigits=2)
# parameters for the model
input_image = values["input_image"]
edit_instruction = values["edit_instruction"]
steps = values["steps"]
seed = values["seed"]
cfgtext = values["text_cfg_scale"]
cfgimage = values["image_cfg_scale"]
resolution = 2 ** int(math.log2(values["resolution"]))
if input_image is None:
raise gr.Error("Missing Input: input_image")
if len(edit_instruction) == 0: # perform no edit
return [input_image, seed, cfgtext, cfgimage]
GoogleDrive_connection_Path = ""
# GoogleDrive_connection_Path = "service_account.json"
GoogleDrive_connection = GoogleDrive_API(GoogleDrive_connection_Path)
api_connection = API_Connection(GoogleDrive_connection, kaggle_username, kaggle_key)
create_folder("local_dataset")
image_ID = get_random_str(4)
input_image_name = rf"input_image_{image_ID}.png"
output_image_name = rf"output_image_{image_ID}.png"
input_image.save(rf"local_dataset\{input_image_name}")
status, img = api_connection.generate_image(
input_image_name, edit_instruction, output_image_name,
steps, seed, cfgtext, cfgimage, resolution
)
print(rf"End Time : {get_current_time()}")
if not status:
raise gr.Error(img)
return [img, seed, cfgtext, cfgimage]
def reset_button_clicked():
return list(DEFAULT_VALUES.values())
def main():
with gr.Blocks(theme="AmirMoris/GP_Themes") as demo:
toggle_theme = gr.Button(value="Toggle Theme")
with gr.Row():
input_image = gr.Image(label="Input Image", type="pil", interactive=True)
edited_image = gr.Image(label=f"Edited Image", type="pil", interactive=False)
with gr.Row():
with gr.Column(scale=3):
instruction = gr.Textbox(lines=1, label="Edit Instruction", interactive=True)
with gr.Column(scale=1, min_width=100):
with gr.Row():
generate_button = gr.Button("Generate")
with gr.Row():
reset_button = gr.Button("Reset")
with gr.Row():
steps = gr.Number(value=DEFAULT_VALUES["steps"], precision=0, label="Steps", interactive=True)
randomize_seed = gr.Radio(
["Fix Seed", "Randomize Seed"],
value=DEFAULT_VALUES["randomize_seed"],
type="index",
show_label=False,
interactive=True,
)
seed = gr.Number(value=DEFAULT_VALUES["seed"], precision=0, label="Seed", interactive=True)
randomize_cfg = gr.Radio(
["Fix CFG", "Randomize CFG"],
value=DEFAULT_VALUES["randomize_cfg"],
type="index",
show_label=False,
interactive=True,
)
text_cfg_scale = gr.Number(value=DEFAULT_VALUES["text_cfg_scale"], label=f"Text CFG", interactive=True)
image_cfg_scale = gr.Number(value=DEFAULT_VALUES["image_cfg_scale"], label=f"Image CFG", interactive=True)
resolution = gr.Number(value=DEFAULT_VALUES["resolution"], label=f"Resolution", interactive=True)
gr.Markdown(HELP_TEXT)
generate_button.click(
fn=generate_button_clicked,
inputs=[
input_image,
instruction,
steps,
randomize_seed,
seed,
randomize_cfg,
text_cfg_scale,
image_cfg_scale,
resolution
],
outputs=[edited_image, seed, text_cfg_scale, image_cfg_scale],
)
reset_button.click(
fn=reset_button_clicked,
outputs=[
input_image,
instruction,
steps,
randomize_seed,
seed,
randomize_cfg,
text_cfg_scale,
image_cfg_scale,
resolution,
edited_image
],
)
toggle_theme.click(
None,
js=
"""
() => {
document.body.classList.toggle('dark');
}
""",
)
# Launch Gradio interface
demo.queue(max_size=1)
demo.launch(share=True)
if __name__ == "__main__":
main() |