Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,15 @@ 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 |
-
|
| 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,42 +23,84 @@ async def generate_image(prompt, model, lora_word, width, height, scales, steps,
|
|
| 24 |
seed = random.randint(0, MAX_SEED)
|
| 25 |
seed = int(seed)
|
| 26 |
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return image, seed
|
| 30 |
except Exception as e:
|
| 31 |
-
print(f"Error
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return result[1]
|
| 39 |
except Exception as e:
|
| 40 |
-
print(f"Error
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
return [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
css = """
|
| 65 |
#col-container{ margin: 0 auto; max-width: 1024px;}
|
|
@@ -69,22 +110,45 @@ with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
|
|
| 69 |
with gr.Column(elem_id="col-container"):
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column(scale=3):
|
| 72 |
-
output_res =
|
| 73 |
with gr.Column(scale=2):
|
| 74 |
prompt = gr.Textbox(label="Image Description")
|
| 75 |
-
basemodel_choice = gr.Dropdown(
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 uuid
|
|
|
|
| 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 |
seed = random.randint(0, MAX_SEED)
|
| 24 |
seed = int(seed)
|
| 25 |
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
| 26 |
+
|
| 27 |
+
# Generate the image
|
| 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 generating image: {e}")
|
| 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 upscaling image: {e}")
|
| 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 |
+
# Generate a unique file name for temporary files
|
| 69 |
+
temp_image_path = f"temp_image_{uuid.uuid4().hex}.jpg"
|
| 70 |
+
upscale_image_path = f"upscale_image_{uuid.uuid4().hex}.jpg"
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
# Generate the image
|
| 74 |
+
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
| 75 |
+
if image is None:
|
| 76 |
+
return ["Generation failed", None]
|
| 77 |
+
|
| 78 |
+
# Save the image locally
|
| 79 |
+
image.save(temp_image_path, format="JPEG")
|
| 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 |
+
return [temp_image_path, temp_image_path]
|
| 92 |
+
except Exception as e:
|
| 93 |
+
print(f"Error in generation pipeline: {e}")
|
| 94 |
+
return ["Error", None]
|
| 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;}
|
|
|
|
| 110 |
with gr.Column(elem_id="col-container"):
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column(scale=3):
|
| 113 |
+
output_res = gr.Image(label="Generated Image / Upscaled Image")
|
| 114 |
with gr.Column(scale=2):
|
| 115 |
prompt = gr.Textbox(label="Image Description")
|
| 116 |
+
basemodel_choice = gr.Dropdown(
|
| 117 |
+
label="Model",
|
| 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 |
+
fn=gen,
|
| 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()
|