Spaces:
Sleeping
Sleeping
File size: 10,389 Bytes
e76f467 41eb2e8 e8606c8 c70a26e fcb27fb 41eb2e8 a827413 b7eba49 e8606c8 c11de04 298633b 931f2d6 e8606c8 931f2d6 4eaf0e2 9a4acf1 b7eba49 bfb7036 0b22058 bfb7036 cb202cc 931f2d6 4eaf0e2 bfb7036 89b7584 4c319af f13315f 4c319af f13315f bfb7036 4eaf0e2 bfb7036 d8c88bb bfb7036 0b22058 b7eba49 cb202cc 85408e4 c92760e c70a26e 1dcbd47 95fcad5 1dcbd47 c70a26e 0b22058 1b50209 9d39e91 1b50209 41eb2e8 b7eba49 41eb2e8 b7eba49 e423e8d 41eb2e8 bcf76f5 113b7ab bcf76f5 05d12ce bcf76f5 05d12ce bcf76f5 05d12ce bcf76f5 05d12ce bcf76f5 c70a26e bcf76f5 b7eba49 bcf76f5 b7eba49 bcf76f5 b7eba49 bcf76f5 41eb2e8 546365b 41eb2e8 0b22058 b7eba49 c70a26e 8409400 094b0d8 8409400 e423e8d 0b22058 b7eba49 8409400 b7eba49 0b22058 4e6797d 8409400 3e2b1f9 c70a26e 85408e4 7ea4f8f |
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
import os
import gradio as gr
import json
from gradio_client import Client, handle_file
from gradio_imageslider import ImageSlider
with open('loras.json', 'r') as f:
loras = json.load(f)
job = None
# Verificar las URLs de los modelos
custom_model_url = "https://fffiloni-sd-xl-custom-model.hf.space"
tile_upscaler_url = "https://gokaygokay-tileupscalerv2.hf.space"
client_custom_model = None
client_tile_upscaler = None
# try:
# client_custom_model = Client(custom_model_url)
# print(f"Loaded custom model from {custom_model_url}")
# except ValueError as e:
# print(f"Failed to load custom model: {e}")
# try:
# client_tile_upscaler = Client(tile_upscaler_url)
# print(f"Loaded custom model from {tile_upscaler_url}")
# except ValueError as e:
# print(f"Failed to load custom model: {e}")
def infer(selected_index, prompt, style_prompt, inf_steps, guidance_scale, width, height, seed, lora_weight, progress=gr.Progress(track_tqdm=True)):
try:
global job
if selected_index is None:
raise gr.Error("You must select a LoRA before proceeding.")
selected_lora = loras[selected_index]
custom_model = selected_lora["repo"]
trigger_word = selected_lora["trigger_word"]
global client_custom_model
if client_custom_model is None:
try:
client_custom_model = Client(custom_model_url)
print(f"Loaded custom model from {custom_model_url}")
except ValueError as e:
print(f"Failed to load custom model: {e}")
client_custom_model = None
raise gr.Error("Failed to load client for " + custom_model_url)
result = client_custom_model.submit(
custom_model=custom_model,
api_name="/load_model"
)
weight_name = result.result()[2]['value']
if trigger_word and prompt.startswith(trigger_word):
prompt = prompt[len(trigger_word+'. '):].lstrip()
if style_prompt and prompt.endswith(style_prompt):
prompt = prompt[:-len('. '+style_prompt)].rstrip()
prompt_arr = [trigger_word, prompt, style_prompt]
prompt = '. '.join([element.strip() for element in prompt_arr if element.strip() != ''])
job = client_custom_model.submit(
custom_model=custom_model,
weight_name=weight_name,
prompt=prompt,
inf_steps=inf_steps,
guidance_scale=guidance_scale,
width=width,
height=height,
seed=seed,
lora_weight=lora_weight,
api_name="/infer"
)
result = job.result()
new_result = result + (prompt, )
return new_result
except Exception as e:
gr.Warning("Error: " + str(e))
def cancel_infer():
global job
if job:
job.cancel()
return "Job has been cancelled"
return "No job to cancel"
def update_selection(evt: gr.SelectData):
selected_lora = loras[evt.index]
new_placeholder = f"Type a prompt for {selected_lora['title']}"
lora_repo = selected_lora["repo"]
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
return (
gr.update(placeholder=new_placeholder),
updated_text,
evt.index
)
def upscale_image(image, resolution, num_inference_steps, strength, hdr, guidance_scale, controlnet_strength, scheduler_name):
global client_tile_upscaler
if client_tile_upscaler is None:
try:
client_tile_upscaler = Client(tile_upscaler_url)
print(f"Loaded custom model from {tile_upscaler_url}")
except ValueError as e:
print(f"Failed to load custom model: {e}")
client_custom_model = None
raise gr.Error("Failed to load client for " + tile_upscaler_url)
result = client_tile_upscaler.predict(
param_0=handle_file(image),
param_1=resolution,
param_2=num_inference_steps,
param_3=strength,
param_4=hdr,
param_5=guidance_scale,
param_6=controlnet_strength,
param_7=scheduler_name,
api_name="/wrapper"
)
return result
css="""
"""
with gr.Blocks(css=css) as demo:
gr.Markdown("# lichorosario LoRA Portfolio")
gr.Markdown(
"### This is my portfolio.\n"
"**Note**: Generation quality may vary. For best results, adjust the parameters.\n"
"Special thanks to [@artificialguybr](https://huggingface.co/artificialguybr) and [@fffiloni](https://huggingface.co/fffiloni).\n"
"Based on [https://huggingface.co/spaces/fffiloni/sd-xl-custom-model](https://huggingface.co/spaces/fffiloni/sd-xl-custom-model) and [https://huggingface.co/spaces/gokaygokay/TileUpscalerV2](https://huggingface.co/spaces/gokaygokay/TileUpscalerV2)"
)
with gr.Row():
with gr.Column(scale=2):
prompt_in = gr.Textbox(
label="Your Prompt",
info="Don't forget to include your trigger word if necessary"
)
style_prompt_in = gr.Textbox(
label="Your Style Prompt"
)
selected_info = gr.Markdown("")
used_prompt = gr.Textbox(
label="Used prompt"
)
with gr.Column(elem_id="col-container"):
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
inf_steps = gr.Slider(
label="Inference steps",
minimum=12,
maximum=100,
step=1,
value=25
)
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.0,
maximum=50.0,
step=0.1,
value=12
)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=3072,
step=32,
value=2048,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=3072,
step=32,
value=1024,
)
examples = [
[1024,512],
[2048,512],
[3072, 512]
]
gr.Examples(
label="Presets",
examples=examples,
inputs=[width, height],
outputs=[]
)
with gr.Row():
seed = gr.Slider(
label="Seed",
info="-1 denotes a random seed",
minimum=-1,
maximum=423538377342,
step=1,
value=-1
)
last_used_seed = gr.Number(
label="Last used seed",
info="the seed used in the last generation",
)
lora_weight = gr.Slider(
label="LoRa weight",
minimum=0.0,
maximum=1.0,
step=0.01,
value=1.0
)
with gr.Column(scale=1):
gallery = gr.Gallery(
[(item["image"], item["title"]) for item in loras],
label="LoRA Gallery",
allow_preview=False,
columns=2,
height="100%"
)
submit_btn = gr.Button("Submit")
cancel_btn = gr.Button("Cancel")
with gr.Row():
def clear_output(image_slider):
image_slider[0] = None
image_slider[1] = None
return image_slider
with gr.Column():
generated_image = gr.Image(label="Input Image", type="filepath")
enhace_button = gr.Button("Enhance Image")
with gr.Column():
output_slider = ImageSlider(label="Before / After", type="numpy", show_download_button=False)
with gr.Accordion("Advanced Options", open=False):
resolution = gr.Slider(minimum=128, maximum=2048, value=1024, step=128, label="Resolution")
num_inference_steps = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of Inference Steps")
strength = gr.Slider(minimum=0, maximum=1, value=0.2, step=0.01, label="Strength")
hdr = gr.Slider(minimum=0, maximum=1, value=0, step=0.1, label="HDR Effect")
guidance_scale = gr.Slider(minimum=0, maximum=20, value=6, step=0.5, label="Guidance Scale")
controlnet_strength = gr.Slider(minimum=0.0, maximum=2.0, value=0.75, step=0.05, label="ControlNet Strength")
scheduler_name = gr.Dropdown(
choices=["DDIM", "DPM++ 3M SDE Karras", "DPM++ 3M Karras"],
value="DDIM",
label="Scheduler"
)
selected_index = gr.State(None)
submit_btn.click(
fn=infer,
inputs=[selected_index, prompt_in, style_prompt_in, inf_steps, guidance_scale, width, height, seed, lora_weight],
outputs=[generated_image, last_used_seed, used_prompt]
)
cancel_btn.click(
fn=cancel_infer,
outputs=[]
)
enhace_button.click(
fn=clear_output,
inputs=[output_slider],
outputs=[output_slider]
).then(
upscale_image,
[generated_image, resolution, num_inference_steps, strength, hdr, guidance_scale, controlnet_strength, scheduler_name],
output_slider
)
gallery.select(update_selection, outputs=[prompt_in, selected_info, selected_index])
demo.launch()
|