Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -10,32 +10,65 @@ import asyncio | |
| 10 | 
             
            from PIL import Image
         | 
| 11 | 
             
            from gradio_client import Client, handle_file
         | 
| 12 |  | 
|  | |
| 13 | 
             
            translator = Translator()
         | 
| 14 | 
             
            HF_TOKEN = os.environ.get("HF_TOKEN", None)
         | 
| 15 | 
             
            basemodel = "black-forest-labs/FLUX.1-schnell"
         | 
| 16 | 
             
            MAX_SEED = np.iinfo(np.int32).max
         | 
| 17 |  | 
|  | |
| 18 | 
             
            CSS = "footer {visibility: hidden;}"
         | 
| 19 | 
             
            JS = "function () {gradioURL = window.location.href;if (!gradioURL.endsWith('?__theme=dark')) {window.location.replace(gradioURL + '?__theme=dark');}}"
         | 
| 20 |  | 
|  | |
| 21 | 
             
            def enable_lora(lora_add):
         | 
| 22 | 
            -
                if not lora_add: | 
| 23 | 
            -
             | 
|  | |
|  | |
|  | |
| 24 |  | 
| 25 | 
             
            def get_upscale_finegrain(prompt, img_path, upscale_factor):
         | 
| 26 | 
             
                client = Client("finegrain/finegrain-image-enhancer")
         | 
| 27 | 
            -
                result = client.predict( | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 28 | 
             
                return result[1]
         | 
| 29 |  | 
|  | |
| 30 | 
             
            async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
         | 
| 31 | 
            -
                if seed == -1: | 
|  | |
| 32 | 
             
                seed = int(seed)
         | 
| 33 | 
             
                text = str(translator.translate(prompt, 'English')) + "," + lora_word
         | 
| 34 | 
            -
                 | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 37 | 
             
                return image, seed
         | 
| 38 |  | 
|  | |
| 39 | 
             
            async def gen(prompt, lora_add, lora_word, width, height, scales, steps, seed, upscale_factor):
         | 
| 40 | 
             
                model = enable_lora(lora_add)
         | 
| 41 | 
             
                image, seed = await generate_image(prompt, model, lora_word, width, height, scales, steps, seed)
         | 
| @@ -45,14 +78,19 @@ async def gen(prompt, lora_add, lora_word, width, height, scales, steps, seed, u | |
| 45 | 
             
                    combined_image.paste(image, (0, 0))
         | 
| 46 | 
             
                    combined_image.paste(upscaled_image, (image.width, 0))
         | 
| 47 | 
             
                    return combined_image, seed
         | 
| 48 | 
            -
                else: | 
|  | |
|  | |
| 49 |  | 
| 50 | 
             
            with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
         | 
| 51 | 
             
                gr.HTML("<h1><center>Flux Lab Light</center></h1>")
         | 
| 52 | 
             
                with gr.Row():
         | 
| 53 | 
             
                    with gr.Column(scale=4):
         | 
| 54 | 
            -
                        with gr.Row(): | 
| 55 | 
            -
             | 
|  | |
|  | |
|  | |
| 56 | 
             
                    with gr.Accordion("Advanced Options", open=True):
         | 
| 57 | 
             
                        with gr.Column(scale=1):
         | 
| 58 | 
             
                            width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=768)
         | 
| @@ -63,4 +101,19 @@ with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo: | |
| 63 | 
             
                        lora_add = gr.Textbox(label="Add Flux LoRA", info="Copy the HF LoRA model name here", lines=1, placeholder="Please use Warm status model")
         | 
| 64 | 
             
                        lora_word = gr.Textbox(label="Add Flux LoRA Trigger Word", info="Add the Trigger Word", lines=1, value="")
         | 
| 65 | 
             
                        upscale_factor = gr.Radio(label="UpScale Factor", choices=[0, 2, 3, 4], value=0, scale=2)
         | 
| 66 | 
            -
                gr.on( | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 10 | 
             
            from PIL import Image
         | 
| 11 | 
             
            from gradio_client import Client, handle_file
         | 
| 12 |  | 
| 13 | 
            +
             | 
| 14 | 
             
            translator = Translator()
         | 
| 15 | 
             
            HF_TOKEN = os.environ.get("HF_TOKEN", None)
         | 
| 16 | 
             
            basemodel = "black-forest-labs/FLUX.1-schnell"
         | 
| 17 | 
             
            MAX_SEED = np.iinfo(np.int32).max
         | 
| 18 |  | 
| 19 | 
            +
             | 
| 20 | 
             
            CSS = "footer {visibility: hidden;}"
         | 
| 21 | 
             
            JS = "function () {gradioURL = window.location.href;if (!gradioURL.endsWith('?__theme=dark')) {window.location.replace(gradioURL + '?__theme=dark');}}"
         | 
| 22 |  | 
| 23 | 
            +
             | 
| 24 | 
             
            def enable_lora(lora_add):
         | 
| 25 | 
            +
                if not lora_add:
         | 
| 26 | 
            +
                    return basemodel
         | 
| 27 | 
            +
                else:
         | 
| 28 | 
            +
                    return lora_add
         | 
| 29 | 
            +
             | 
| 30 |  | 
| 31 | 
             
            def get_upscale_finegrain(prompt, img_path, upscale_factor):
         | 
| 32 | 
             
                client = Client("finegrain/finegrain-image-enhancer")
         | 
| 33 | 
            +
                result = client.predict(
         | 
| 34 | 
            +
                    input_image=handle_file(img_path),
         | 
| 35 | 
            +
                    prompt=prompt,
         | 
| 36 | 
            +
                    negative_prompt="",
         | 
| 37 | 
            +
                    seed=42,
         | 
| 38 | 
            +
                    upscale_factor=upscale_factor,
         | 
| 39 | 
            +
                    controlnet_scale=0.6,
         | 
| 40 | 
            +
                    controlnet_decay=1,
         | 
| 41 | 
            +
                    condition_scale=6,
         | 
| 42 | 
            +
                    tile_width=112,
         | 
| 43 | 
            +
                    tile_height=144,
         | 
| 44 | 
            +
                    denoise_strength=0.35,
         | 
| 45 | 
            +
                    num_inference_steps=18,
         | 
| 46 | 
            +
                    solver="DDIM",
         | 
| 47 | 
            +
                    api_name="/process"
         | 
| 48 | 
            +
                )
         | 
| 49 | 
             
                return result[1]
         | 
| 50 |  | 
| 51 | 
            +
             | 
| 52 | 
             
            async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
         | 
| 53 | 
            +
                if seed == -1:
         | 
| 54 | 
            +
                    seed = random.randint(0, MAX_SEED)
         | 
| 55 | 
             
                seed = int(seed)
         | 
| 56 | 
             
                text = str(translator.translate(prompt, 'English')) + "," + lora_word
         | 
| 57 | 
            +
                async with AsyncInferenceClient() as client:
         | 
| 58 | 
            +
                    try:
         | 
| 59 | 
            +
                        image = await client.text_to_image(
         | 
| 60 | 
            +
                            prompt=text,
         | 
| 61 | 
            +
                            height=height,
         | 
| 62 | 
            +
                            width=width,
         | 
| 63 | 
            +
                            guidance_scale=scales,
         | 
| 64 | 
            +
                            num_inference_steps=steps,
         | 
| 65 | 
            +
                            model=model,
         | 
| 66 | 
            +
                        )
         | 
| 67 | 
            +
                    except Exception as e:
         | 
| 68 | 
            +
                        raise gr.Error(f"Error in {e}")
         | 
| 69 | 
             
                return image, seed
         | 
| 70 |  | 
| 71 | 
            +
             | 
| 72 | 
             
            async def gen(prompt, lora_add, lora_word, width, height, scales, steps, seed, upscale_factor):
         | 
| 73 | 
             
                model = enable_lora(lora_add)
         | 
| 74 | 
             
                image, seed = await generate_image(prompt, model, lora_word, width, height, scales, steps, seed)
         | 
|  | |
| 78 | 
             
                    combined_image.paste(image, (0, 0))
         | 
| 79 | 
             
                    combined_image.paste(upscaled_image, (image.width, 0))
         | 
| 80 | 
             
                    return combined_image, seed
         | 
| 81 | 
            +
                else:
         | 
| 82 | 
            +
                    return image, seed
         | 
| 83 | 
            +
             | 
| 84 |  | 
| 85 | 
             
            with gr.Blocks(css=CSS, js=JS, theme="Nymbo/Nymbo_Theme") as demo:
         | 
| 86 | 
             
                gr.HTML("<h1><center>Flux Lab Light</center></h1>")
         | 
| 87 | 
             
                with gr.Row():
         | 
| 88 | 
             
                    with gr.Column(scale=4):
         | 
| 89 | 
            +
                        with gr.Row():
         | 
| 90 | 
            +
                            img = gr.Image(type="filepath", label='Comparison Image', height=600)
         | 
| 91 | 
            +
                        with gr.Row():
         | 
| 92 | 
            +
                            prompt = gr.Textbox(label='Enter Your Prompt (Multi-Languages)', placeholder="Enter prompt...", scale=6)
         | 
| 93 | 
            +
                            sendBtn = gr.Button(scale=1, variant='primary')
         | 
| 94 | 
             
                    with gr.Accordion("Advanced Options", open=True):
         | 
| 95 | 
             
                        with gr.Column(scale=1):
         | 
| 96 | 
             
                            width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=768)
         | 
|  | |
| 101 | 
             
                        lora_add = gr.Textbox(label="Add Flux LoRA", info="Copy the HF LoRA model name here", lines=1, placeholder="Please use Warm status model")
         | 
| 102 | 
             
                        lora_word = gr.Textbox(label="Add Flux LoRA Trigger Word", info="Add the Trigger Word", lines=1, value="")
         | 
| 103 | 
             
                        upscale_factor = gr.Radio(label="UpScale Factor", choices=[0, 2, 3, 4], value=0, scale=2)
         | 
| 104 | 
            +
                gr.on(
         | 
| 105 | 
            +
                    triggers=[prompt.submit, sendBtn.click],
         | 
| 106 | 
            +
                    fn=gen,
         | 
| 107 | 
            +
                    inputs=[
         | 
| 108 | 
            +
                        prompt,
         | 
| 109 | 
            +
                        lora_add,
         | 
| 110 | 
            +
                        lora_word,
         | 
| 111 | 
            +
                        width,
         | 
| 112 | 
            +
                        height,
         | 
| 113 | 
            +
                        scales,
         | 
| 114 | 
            +
                        steps,
         | 
| 115 | 
            +
                        seed,
         | 
| 116 | 
            +
                        upscale_factor
         | 
| 117 | 
            +
                    ],
         | 
| 118 | 
            +
                    outputs=[img, seed]
         | 
| 119 | 
            +
                )
         | 

