Spaces:
				
			
			
	
			
			
		Running
		
			on 
			
			Zero
	
	
	
			
			
	
	
	
	
		
		
		Running
		
			on 
			
			Zero
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -7,11 +7,21 @@ from PIL import Image | |
| 7 | 
             
            import spaces
         | 
| 8 | 
             
            from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
         | 
| 9 | 
             
            from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
         | 
| 10 | 
            -
             | 
| 11 | 
             
            from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
         | 
| 12 | 
             
            import copy
         | 
| 13 | 
             
            import random
         | 
| 14 | 
             
            import time
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 15 |  | 
| 16 | 
             
            # Load LoRAs from JSON file
         | 
| 17 | 
             
            with open('loras.json', 'r') as f:
         | 
| @@ -48,9 +58,9 @@ class calculateDuration: | |
| 48 |  | 
| 49 | 
             
            def update_selection(evt: gr.SelectData, width, height):
         | 
| 50 | 
             
                selected_lora = loras[evt.index]
         | 
| 51 | 
            -
                new_placeholder = f" | 
| 52 | 
             
                lora_repo = selected_lora["repo"]
         | 
| 53 | 
            -
                updated_text = f"###  | 
| 54 | 
             
                if "aspect" in selected_lora:
         | 
| 55 | 
             
                    if selected_lora["aspect"] == "portrait":
         | 
| 56 | 
             
                        width = 768
         | 
| @@ -73,7 +83,7 @@ def update_selection(evt: gr.SelectData, width, height): | |
| 73 | 
             
            def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress):
         | 
| 74 | 
             
                pipe.to("cuda")
         | 
| 75 | 
             
                generator = torch.Generator(device="cuda").manual_seed(seed)
         | 
| 76 | 
            -
                with calculateDuration(" | 
| 77 | 
             
                    # Generate image
         | 
| 78 | 
             
                    for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
         | 
| 79 | 
             
                        prompt=prompt_mash,
         | 
| @@ -90,33 +100,36 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal | |
| 90 |  | 
| 91 | 
             
            def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
         | 
| 92 | 
             
                if selected_index is None:
         | 
| 93 | 
            -
                    raise gr.Error(" | 
|  | |
|  | |
|  | |
| 94 | 
             
                selected_lora = loras[selected_index]
         | 
| 95 | 
             
                lora_path = selected_lora["repo"]
         | 
| 96 | 
             
                trigger_word = selected_lora["trigger_word"]
         | 
| 97 | 
             
                if(trigger_word):
         | 
| 98 | 
             
                    if "trigger_position" in selected_lora:
         | 
| 99 | 
             
                        if selected_lora["trigger_position"] == "prepend":
         | 
| 100 | 
            -
                            prompt_mash = f"{trigger_word} { | 
| 101 | 
             
                        else:
         | 
| 102 | 
            -
                            prompt_mash = f"{ | 
| 103 | 
             
                    else:
         | 
| 104 | 
            -
                        prompt_mash = f"{trigger_word} { | 
| 105 | 
             
                else:
         | 
| 106 | 
            -
                    prompt_mash =  | 
| 107 |  | 
| 108 | 
            -
                with calculateDuration(" | 
| 109 | 
             
                    pipe.unload_lora_weights()
         | 
| 110 |  | 
| 111 | 
             
                # Load LoRA weights
         | 
| 112 | 
            -
                with calculateDuration(f" | 
| 113 | 
             
                    if "weights" in selected_lora:
         | 
| 114 | 
             
                        pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
         | 
| 115 | 
             
                    else:
         | 
| 116 | 
             
                        pipe.load_lora_weights(lora_path)
         | 
| 117 |  | 
| 118 | 
             
                # Set random seed for reproducibility
         | 
| 119 | 
            -
                with calculateDuration(" | 
| 120 | 
             
                    if randomize_seed:
         | 
| 121 | 
             
                        seed = random.randint(0, MAX_SEED)
         | 
| 122 |  | 
| @@ -129,9 +142,10 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid | |
| 129 | 
             
                    step_counter+=1
         | 
| 130 | 
             
                    final_image = image
         | 
| 131 | 
             
                    progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
         | 
| 132 | 
            -
                    yield image, seed, gr.update(value=progress_bar, visible=True)
         | 
| 133 |  | 
| 134 | 
            -
                yield final_image, seed, gr.update(value=progress_bar, visible=False)
         | 
|  | |
| 135 |  | 
| 136 | 
             
            def get_huggingface_safetensors(link):
         | 
| 137 | 
             
              split_link = link.split("/")
         | 
| @@ -216,48 +230,52 @@ footer { | |
| 216 | 
             
            }
         | 
| 217 | 
             
            """
         | 
| 218 |  | 
|  | |
| 219 | 
             
            with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as app:
         | 
| 220 |  | 
| 221 | 
             
                selected_index = gr.State(None)
         | 
| 222 | 
             
                with gr.Row():
         | 
| 223 | 
             
                    with gr.Column(scale=3):
         | 
| 224 | 
            -
                        prompt = gr.Textbox(label=" | 
| 225 | 
             
                    with gr.Column(scale=1, elem_id="gen_column"):
         | 
| 226 | 
            -
                        generate_button = gr.Button(" | 
| 227 | 
             
                with gr.Row():
         | 
| 228 | 
             
                    with gr.Column():
         | 
| 229 | 
             
                        selected_info = gr.Markdown("")
         | 
| 230 | 
             
                        gallery = gr.Gallery(
         | 
| 231 | 
             
                            [(item["image"], item["title"]) for item in loras],
         | 
| 232 | 
            -
                            label="LoRA  | 
| 233 | 
             
                            allow_preview=False,
         | 
| 234 | 
             
                            columns=3,
         | 
| 235 | 
             
                            elem_id="gallery"
         | 
| 236 | 
             
                        )
         | 
| 237 | 
             
                        with gr.Group():
         | 
| 238 | 
            -
                            custom_lora = gr.Textbox(label=" | 
| 239 | 
            -
                            gr.Markdown("[ | 
| 240 | 
             
                        custom_lora_info = gr.HTML(visible=False)
         | 
| 241 | 
            -
                        custom_lora_button = gr.Button(" | 
| 242 | 
             
                    with gr.Column():
         | 
| 243 | 
             
                        progress_bar = gr.Markdown(elem_id="progress",visible=False)
         | 
| 244 | 
            -
                        result = gr.Image(label=" | 
|  | |
|  | |
| 245 |  | 
| 246 | 
             
                with gr.Row():
         | 
| 247 | 
            -
                    with gr.Accordion(" | 
| 248 | 
             
                        with gr.Column():
         | 
| 249 | 
             
                            with gr.Row():
         | 
| 250 | 
            -
                                cfg_scale = gr.Slider(label="CFG  | 
| 251 | 
            -
                                steps = gr.Slider(label=" | 
| 252 |  | 
| 253 | 
             
                            with gr.Row():
         | 
| 254 | 
            -
                                width = gr.Slider(label=" | 
| 255 | 
            -
                                height = gr.Slider(label=" | 
| 256 |  | 
| 257 | 
             
                            with gr.Row():
         | 
| 258 | 
            -
                                randomize_seed = gr.Checkbox(True, label=" | 
| 259 | 
            -
                                seed = gr.Slider(label=" | 
| 260 | 
            -
                                lora_scale = gr.Slider(label="LoRA  | 
|  | |
| 261 |  | 
| 262 | 
             
                gallery.select(
         | 
| 263 | 
             
                    update_selection,
         | 
| @@ -273,11 +291,12 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as app: | |
| 273 | 
             
                    remove_custom_lora,
         | 
| 274 | 
             
                    outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
         | 
| 275 | 
             
                )
         | 
|  | |
| 276 | 
             
                gr.on(
         | 
| 277 | 
             
                    triggers=[generate_button.click, prompt.submit],
         | 
| 278 | 
             
                    fn=run_lora,
         | 
| 279 | 
             
                    inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
         | 
| 280 | 
            -
                    outputs=[result, seed, progress_bar]
         | 
| 281 | 
             
                )
         | 
| 282 |  | 
| 283 | 
             
            app.queue()
         | 
|  | |
| 7 | 
             
            import spaces
         | 
| 8 | 
             
            from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
         | 
| 9 | 
             
            from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
         | 
|  | |
| 10 | 
             
            from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
         | 
| 11 | 
             
            import copy
         | 
| 12 | 
             
            import random
         | 
| 13 | 
             
            import time
         | 
| 14 | 
            +
            from transformers import pipeline
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            # ๋ฒ์ญ ๋ชจ๋ธ ์ด๊ธฐํ
         | 
| 17 | 
            +
            translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            # ํ๋กฌํํธ ์ฒ๋ฆฌ ํจ์ ์ถ๊ฐ
         | 
| 20 | 
            +
            def process_prompt(prompt):
         | 
| 21 | 
            +
                if any('\u3131' <= char <= '\u3163' or '\uac00' <= char <= '\ud7a3' for char in prompt):
         | 
| 22 | 
            +
                    translated = translator(prompt)[0]['translation_text']
         | 
| 23 | 
            +
                    return prompt, translated
         | 
| 24 | 
            +
                return prompt, prompt
         | 
| 25 |  | 
| 26 | 
             
            # Load LoRAs from JSON file
         | 
| 27 | 
             
            with open('loras.json', 'r') as f:
         | 
|  | |
| 58 |  | 
| 59 | 
             
            def update_selection(evt: gr.SelectData, width, height):
         | 
| 60 | 
             
                selected_lora = loras[evt.index]
         | 
| 61 | 
            +
                new_placeholder = f"{selected_lora['title']}๋ฅผ ์ํ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์"
         | 
| 62 | 
             
                lora_repo = selected_lora["repo"]
         | 
| 63 | 
            +
                updated_text = f"### ์ ํ๋จ: [{lora_repo}](https://huggingface.co/{lora_repo}) โจ"
         | 
| 64 | 
             
                if "aspect" in selected_lora:
         | 
| 65 | 
             
                    if selected_lora["aspect"] == "portrait":
         | 
| 66 | 
             
                        width = 768
         | 
|  | |
| 83 | 
             
            def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress):
         | 
| 84 | 
             
                pipe.to("cuda")
         | 
| 85 | 
             
                generator = torch.Generator(device="cuda").manual_seed(seed)
         | 
| 86 | 
            +
                with calculateDuration("์ด๋ฏธ์ง ์์ฑ"):
         | 
| 87 | 
             
                    # Generate image
         | 
| 88 | 
             
                    for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
         | 
| 89 | 
             
                        prompt=prompt_mash,
         | 
|  | |
| 100 |  | 
| 101 | 
             
            def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
         | 
| 102 | 
             
                if selected_index is None:
         | 
| 103 | 
            +
                    raise gr.Error("์งํํ๊ธฐ ์ ์ LoRA๋ฅผ ์ ํํด์ผ ํฉ๋๋ค.")
         | 
| 104 | 
            +
                
         | 
| 105 | 
            +
                original_prompt, english_prompt = process_prompt(prompt)
         | 
| 106 | 
            +
                
         | 
| 107 | 
             
                selected_lora = loras[selected_index]
         | 
| 108 | 
             
                lora_path = selected_lora["repo"]
         | 
| 109 | 
             
                trigger_word = selected_lora["trigger_word"]
         | 
| 110 | 
             
                if(trigger_word):
         | 
| 111 | 
             
                    if "trigger_position" in selected_lora:
         | 
| 112 | 
             
                        if selected_lora["trigger_position"] == "prepend":
         | 
| 113 | 
            +
                            prompt_mash = f"{trigger_word} {english_prompt}"
         | 
| 114 | 
             
                        else:
         | 
| 115 | 
            +
                            prompt_mash = f"{english_prompt} {trigger_word}"
         | 
| 116 | 
             
                    else:
         | 
| 117 | 
            +
                        prompt_mash = f"{trigger_word} {english_prompt}"
         | 
| 118 | 
             
                else:
         | 
| 119 | 
            +
                    prompt_mash = english_prompt
         | 
| 120 |  | 
| 121 | 
            +
                with calculateDuration("LoRA ์ธ๋ก๋"):
         | 
| 122 | 
             
                    pipe.unload_lora_weights()
         | 
| 123 |  | 
| 124 | 
             
                # Load LoRA weights
         | 
| 125 | 
            +
                with calculateDuration(f"{selected_lora['title']}์ LoRA ๊ฐ์ค์น ๋ก๋"):
         | 
| 126 | 
             
                    if "weights" in selected_lora:
         | 
| 127 | 
             
                        pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
         | 
| 128 | 
             
                    else:
         | 
| 129 | 
             
                        pipe.load_lora_weights(lora_path)
         | 
| 130 |  | 
| 131 | 
             
                # Set random seed for reproducibility
         | 
| 132 | 
            +
                with calculateDuration("์๋ ๋ฌด์์ํ"):
         | 
| 133 | 
             
                    if randomize_seed:
         | 
| 134 | 
             
                        seed = random.randint(0, MAX_SEED)
         | 
| 135 |  | 
|  | |
| 142 | 
             
                    step_counter+=1
         | 
| 143 | 
             
                    final_image = image
         | 
| 144 | 
             
                    progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
         | 
| 145 | 
            +
                    yield image, seed, gr.update(value=progress_bar, visible=True), original_prompt, english_prompt
         | 
| 146 |  | 
| 147 | 
            +
                yield final_image, seed, gr.update(value=progress_bar, visible=False), original_prompt, english_prompt
         | 
| 148 | 
            +
             | 
| 149 |  | 
| 150 | 
             
            def get_huggingface_safetensors(link):
         | 
| 151 | 
             
              split_link = link.split("/")
         | 
|  | |
| 230 | 
             
            }
         | 
| 231 | 
             
            """
         | 
| 232 |  | 
| 233 | 
            +
             | 
| 234 | 
             
            with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as app:
         | 
| 235 |  | 
| 236 | 
             
                selected_index = gr.State(None)
         | 
| 237 | 
             
                with gr.Row():
         | 
| 238 | 
             
                    with gr.Column(scale=3):
         | 
| 239 | 
            +
                        prompt = gr.Textbox(label="ํ๋กฌํํธ", lines=1, placeholder="LoRA๋ฅผ ์ ํํ ํ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์ (ํ๊ธ ๋๋ ์์ด)")
         | 
| 240 | 
             
                    with gr.Column(scale=1, elem_id="gen_column"):
         | 
| 241 | 
            +
                        generate_button = gr.Button("์์ฑ", variant="primary", elem_id="gen_btn")
         | 
| 242 | 
             
                with gr.Row():
         | 
| 243 | 
             
                    with gr.Column():
         | 
| 244 | 
             
                        selected_info = gr.Markdown("")
         | 
| 245 | 
             
                        gallery = gr.Gallery(
         | 
| 246 | 
             
                            [(item["image"], item["title"]) for item in loras],
         | 
| 247 | 
            +
                            label="LoRA ๊ฐค๋ฌ๋ฆฌ",
         | 
| 248 | 
             
                            allow_preview=False,
         | 
| 249 | 
             
                            columns=3,
         | 
| 250 | 
             
                            elem_id="gallery"
         | 
| 251 | 
             
                        )
         | 
| 252 | 
             
                        with gr.Group():
         | 
| 253 | 
            +
                            custom_lora = gr.Textbox(label="์ปค์คํ
 LoRA", info="LoRA Hugging Face ๊ฒฝ๋ก", placeholder="multimodalart/vintage-ads-flux")
         | 
| 254 | 
            +
                            gr.Markdown("[FLUX LoRA ๋ชฉ๋ก ํ์ธ](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
         | 
| 255 | 
             
                        custom_lora_info = gr.HTML(visible=False)
         | 
| 256 | 
            +
                        custom_lora_button = gr.Button("์ปค์คํ
 LoRA ์ ๊ฑฐ", visible=False)
         | 
| 257 | 
             
                    with gr.Column():
         | 
| 258 | 
             
                        progress_bar = gr.Markdown(elem_id="progress",visible=False)
         | 
| 259 | 
            +
                        result = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง")
         | 
| 260 | 
            +
                        original_prompt_display = gr.Textbox(label="์๋ณธ ํ๋กฌํํธ")
         | 
| 261 | 
            +
                        english_prompt_display = gr.Textbox(label="์์ด ํ๋กฌํํธ")
         | 
| 262 |  | 
| 263 | 
             
                with gr.Row():
         | 
| 264 | 
            +
                    with gr.Accordion("๊ณ ๊ธ ์ค์ ", open=False):
         | 
| 265 | 
             
                        with gr.Column():
         | 
| 266 | 
             
                            with gr.Row():
         | 
| 267 | 
            +
                                cfg_scale = gr.Slider(label="CFG ์ค์ผ์ผ", minimum=1, maximum=20, step=0.5, value=3.5)
         | 
| 268 | 
            +
                                steps = gr.Slider(label="์คํ
", minimum=1, maximum=50, step=1, value=28)
         | 
| 269 |  | 
| 270 | 
             
                            with gr.Row():
         | 
| 271 | 
            +
                                width = gr.Slider(label="๋๋น", minimum=256, maximum=1536, step=64, value=1024)
         | 
| 272 | 
            +
                                height = gr.Slider(label="๋์ด", minimum=256, maximum=1536, step=64, value=1024)
         | 
| 273 |  | 
| 274 | 
             
                            with gr.Row():
         | 
| 275 | 
            +
                                randomize_seed = gr.Checkbox(True, label="์๋ ๋ฌด์์ํ")
         | 
| 276 | 
            +
                                seed = gr.Slider(label="์๋", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
         | 
| 277 | 
            +
                                lora_scale = gr.Slider(label="LoRA ์ค์ผ์ผ", minimum=0, maximum=3, step=0.01, value=0.95)
         | 
| 278 | 
            +
             | 
| 279 |  | 
| 280 | 
             
                gallery.select(
         | 
| 281 | 
             
                    update_selection,
         | 
|  | |
| 291 | 
             
                    remove_custom_lora,
         | 
| 292 | 
             
                    outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
         | 
| 293 | 
             
                )
         | 
| 294 | 
            +
             | 
| 295 | 
             
                gr.on(
         | 
| 296 | 
             
                    triggers=[generate_button.click, prompt.submit],
         | 
| 297 | 
             
                    fn=run_lora,
         | 
| 298 | 
             
                    inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
         | 
| 299 | 
            +
                    outputs=[result, seed, progress_bar, original_prompt_display, english_prompt_display]
         | 
| 300 | 
             
                )
         | 
| 301 |  | 
| 302 | 
             
            app.queue()
         | 
 
			

