Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
from huggingface_hub import AsyncInferenceClient
|
| 6 |
from translatepy import Translator
|
|
|
|
|
|
|
| 7 |
import asyncio
|
| 8 |
from PIL import Image
|
| 9 |
from gradio_client import Client, handle_file
|
| 10 |
-
import
|
|
|
|
| 11 |
|
| 12 |
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
|
| 14 |
-
# Initialize the AsyncInferenceClient globally
|
| 15 |
-
client = AsyncInferenceClient()
|
| 16 |
|
| 17 |
def enable_lora(lora_add, basemodel):
|
| 18 |
return basemodel if not lora_add else lora_add
|
|
@@ -23,132 +24,67 @@ async def generate_image(prompt, model, lora_word, width, height, scales, steps,
|
|
| 23 |
seed = random.randint(0, MAX_SEED)
|
| 24 |
seed = int(seed)
|
| 25 |
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
image = await client.text_to_image(
|
| 29 |
-
prompt=text,
|
| 30 |
-
height=height,
|
| 31 |
-
width=width,
|
| 32 |
-
guidance_scale=scales,
|
| 33 |
-
num_inference_steps=steps,
|
| 34 |
-
model=model
|
| 35 |
-
)
|
| 36 |
return image, seed
|
| 37 |
except Exception as e:
|
| 38 |
-
print(f"Error
|
| 39 |
return None, None
|
| 40 |
|
| 41 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
| 42 |
try:
|
| 43 |
client = Client("finegrain/finegrain-image-enhancer")
|
| 44 |
-
result = client.predict(
|
| 45 |
-
input_image=handle_file(img_path),
|
| 46 |
-
prompt=prompt,
|
| 47 |
-
negative_prompt="",
|
| 48 |
-
seed=42,
|
| 49 |
-
upscale_factor=upscale_factor,
|
| 50 |
-
controlnet_scale=0.6,
|
| 51 |
-
controlnet_decay=1,
|
| 52 |
-
condition_scale=6,
|
| 53 |
-
tile_width=112,
|
| 54 |
-
tile_height=144,
|
| 55 |
-
denoise_strength=0.35,
|
| 56 |
-
num_inference_steps=18,
|
| 57 |
-
solver="DDIM",
|
| 58 |
-
api_name="/process"
|
| 59 |
-
)
|
| 60 |
return result[1]
|
| 61 |
except Exception as e:
|
| 62 |
-
print(f"Error
|
| 63 |
return None
|
| 64 |
|
| 65 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
| 66 |
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# Process upscale if required
|
| 82 |
-
if process_upscale:
|
| 83 |
-
upscale_result_path = get_upscale_finegrain(prompt, temp_image_path, upscale_factor)
|
| 84 |
-
if upscale_result_path is not None:
|
| 85 |
-
upscale_image = Image.open(upscale_result_path)
|
| 86 |
-
upscale_image.save(upscale_image_path, format="JPEG")
|
| 87 |
-
return [temp_image_path, upscale_image_path]
|
| 88 |
-
else:
|
| 89 |
-
return ["Upscale failed", temp_image_path]
|
| 90 |
else:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
return [
|
| 95 |
-
finally:
|
| 96 |
-
# Cleanup temporary files
|
| 97 |
-
try:
|
| 98 |
-
if os.path.exists(temp_image_path):
|
| 99 |
-
os.remove(temp_image_path)
|
| 100 |
-
if os.path.exists(upscale_image_path):
|
| 101 |
-
os.remove(upscale_image_path)
|
| 102 |
-
except Exception as cleanup_error:
|
| 103 |
-
print(f"Error during cleanup: {cleanup_error}")
|
| 104 |
|
| 105 |
css = """
|
| 106 |
#col-container{ margin: 0 auto; max-width: 1024px;}
|
| 107 |
"""
|
| 108 |
|
| 109 |
-
with gr.Blocks(css=css
|
| 110 |
with gr.Column(elem_id="col-container"):
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=3):
|
| 113 |
-
output_res =
|
| 114 |
with gr.Column(scale=2):
|
| 115 |
prompt = gr.Textbox(label="Image Description")
|
| 116 |
-
basemodel_choice = gr.Dropdown(
|
| 117 |
-
|
| 118 |
-
choices=[
|
| 119 |
-
"black-forest-labs/FLUX.1-schnell",
|
| 120 |
-
"black-forest-labs/FLUX.1-DEV",
|
| 121 |
-
"enhanceaiteam/Flux-uncensored",
|
| 122 |
-
"Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro",
|
| 123 |
-
"Shakker-Labs/FLUX.1-dev-LoRA-add-details",
|
| 124 |
-
"city96/FLUX.1-dev-gguf"
|
| 125 |
-
],
|
| 126 |
-
value="black-forest-labs/FLUX.1-schnell"
|
| 127 |
-
)
|
| 128 |
-
lora_model_choice = gr.Dropdown(
|
| 129 |
-
label="LoRA",
|
| 130 |
-
choices=[
|
| 131 |
-
"Shakker-Labs/FLUX.1-dev-LoRA-add-details",
|
| 132 |
-
"XLabs-AI/flux-RealismLora",
|
| 133 |
-
"enhanceaiteam/Flux-uncensored"
|
| 134 |
-
],
|
| 135 |
-
value="XLabs-AI/flux-RealismLora"
|
| 136 |
-
)
|
| 137 |
process_lora = gr.Checkbox(label="LoRA Process")
|
| 138 |
process_upscale = gr.Checkbox(label="Scale Process")
|
| 139 |
upscale_factor = gr.Radio(label="Scaling Factor", choices=[2, 4, 8], value=2)
|
| 140 |
-
|
| 141 |
with gr.Accordion(label="Advanced Options", open=False):
|
| 142 |
width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=1280)
|
| 143 |
height = gr.Slider(label="Height", minimum=512, maximum=1280, step=8, value=768)
|
| 144 |
scales = gr.Slider(label="Scale", minimum=1, maximum=20, step=1, value=8)
|
| 145 |
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=8)
|
| 146 |
seed = gr.Number(label="Seed", value=-1)
|
| 147 |
-
|
| 148 |
btn = gr.Button("Generate")
|
| 149 |
-
btn.click(
|
| 150 |
-
|
| 151 |
-
inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora],
|
| 152 |
-
outputs=output_res,
|
| 153 |
-
)
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
from huggingface_hub import AsyncInferenceClient
|
| 6 |
from translatepy import Translator
|
| 7 |
+
import requests
|
| 8 |
+
import re
|
| 9 |
import asyncio
|
| 10 |
from PIL import Image
|
| 11 |
from gradio_client import Client, handle_file
|
| 12 |
+
from huggingface_hub import login
|
| 13 |
+
from gradio_imageslider import ImageSlider
|
| 14 |
|
| 15 |
MAX_SEED = np.iinfo(np.int32).max
|
| 16 |
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def enable_lora(lora_add, basemodel):
|
| 19 |
return basemodel if not lora_add else lora_add
|
|
|
|
| 24 |
seed = random.randint(0, MAX_SEED)
|
| 25 |
seed = int(seed)
|
| 26 |
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
| 27 |
+
client = AsyncInferenceClient()
|
| 28 |
+
image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return image, seed
|
| 30 |
except Exception as e:
|
| 31 |
+
print(f"Error generando imagen: {e}")
|
| 32 |
return None, None
|
| 33 |
|
| 34 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
| 35 |
try:
|
| 36 |
client = Client("finegrain/finegrain-image-enhancer")
|
| 37 |
+
result = client.predict(input_image=handle_file(img_path), prompt=prompt, negative_prompt="", seed=42, upscale_factor=upscale_factor, controlnet_scale=0.6, controlnet_decay=1, condition_scale=6, tile_width=112, tile_height=144, denoise_strength=0.35, num_inference_steps=18, solver="DDIM", api_name="/process")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return result[1]
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"Error escalando imagen: {e}")
|
| 41 |
return None
|
| 42 |
|
| 43 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
| 44 |
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
| 45 |
+
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
| 46 |
+
if image is None:
|
| 47 |
+
return [None, None]
|
| 48 |
+
|
| 49 |
+
image_path = "temp_image.jpg"
|
| 50 |
+
image.save(image_path, format="JPEG")
|
| 51 |
+
|
| 52 |
+
if process_upscale:
|
| 53 |
+
upscale_image_path = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
| 54 |
+
if upscale_image_path is not None:
|
| 55 |
+
upscale_image = Image.open(upscale_image_path)
|
| 56 |
+
upscale_image.save("upscale_image.jpg", format="JPEG")
|
| 57 |
+
return [image_path, "upscale_image.jpg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
+
print("Error: The scaled image path is None")
|
| 60 |
+
return [image_path, image_path]
|
| 61 |
+
else:
|
| 62 |
+
return [image_path, image_path]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
css = """
|
| 65 |
#col-container{ margin: 0 auto; max-width: 1024px;}
|
| 66 |
"""
|
| 67 |
|
| 68 |
+
with gr.Blocks(css=css) as demo:
|
| 69 |
with gr.Column(elem_id="col-container"):
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column(scale=3):
|
| 72 |
+
output_res = ImageSlider(label="Flux / Upscaled")
|
| 73 |
with gr.Column(scale=2):
|
| 74 |
prompt = gr.Textbox(label="Image Description")
|
| 75 |
+
basemodel_choice = gr.Dropdown(label="Model", choices=["black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-DEV", "enhanceaiteam/Flux-uncensored", "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro", "Shakker-Labs/FLUX.1-dev-LoRA-add-details", "city96/FLUX.1-dev-gguf"], value="black-forest-labs/FLUX.1-schnell")
|
| 76 |
+
lora_model_choice = gr.Dropdown(label="LoRA", choices=["Shakker-Labs/FLUX.1-dev-LoRA-add-details", "XLabs-AI/flux-RealismLora", "enhanceaiteam/Flux-uncensored"], value="XLabs-AI/flux-RealismLora")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
process_lora = gr.Checkbox(label="LoRA Process")
|
| 78 |
process_upscale = gr.Checkbox(label="Scale Process")
|
| 79 |
upscale_factor = gr.Radio(label="Scaling Factor", choices=[2, 4, 8], value=2)
|
| 80 |
+
|
| 81 |
with gr.Accordion(label="Advanced Options", open=False):
|
| 82 |
width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=1280)
|
| 83 |
height = gr.Slider(label="Height", minimum=512, maximum=1280, step=8, value=768)
|
| 84 |
scales = gr.Slider(label="Scale", minimum=1, maximum=20, step=1, value=8)
|
| 85 |
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=8)
|
| 86 |
seed = gr.Number(label="Seed", value=-1)
|
| 87 |
+
|
| 88 |
btn = gr.Button("Generate")
|
| 89 |
+
btn.click(fn=gen, inputs=[prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora], outputs=output_res,)
|
| 90 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|