Spaces:
Running on Zero
Running on Zero
update appo
Browse files
app.py
CHANGED
|
@@ -11,7 +11,18 @@ from diffusers import FluxKontextPipeline
|
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
from aura_sr import AuraSR
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
from gradio.themes import Soft
|
| 17 |
from gradio.themes.utils import colors, fonts, sizes
|
|
@@ -36,7 +47,7 @@ class OrangeRedTheme(Soft):
|
|
| 36 |
self,
|
| 37 |
*,
|
| 38 |
primary_hue: colors.Color | str = colors.gray,
|
| 39 |
-
secondary_hue: colors.Color | str = colors.orange_red,
|
| 40 |
neutral_hue: colors.Color | str = colors.slate,
|
| 41 |
text_size: sizes.Size | str = sizes.text_lg,
|
| 42 |
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
|
@@ -85,35 +96,24 @@ class OrangeRedTheme(Soft):
|
|
| 85 |
orange_red_theme = OrangeRedTheme()
|
| 86 |
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# --- # Device and CUDA Setup Check ---
|
| 91 |
-
print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
|
| 92 |
-
print("torch.__version__ =", torch.__version__)
|
| 93 |
-
print("torch.version.cuda =", torch.version.cuda)
|
| 94 |
-
print("cuda available:", torch.cuda.is_available())
|
| 95 |
-
print("cuda device count:", torch.cuda.device_count())
|
| 96 |
-
if torch.cuda.is_available():
|
| 97 |
-
print("current device:", torch.cuda.current_device())
|
| 98 |
-
print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
|
| 99 |
-
|
| 100 |
-
print("Using device:", device)
|
| 101 |
-
|
| 102 |
MAX_SEED = np.iinfo(np.int32).max
|
| 103 |
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 104 |
|
|
|
|
| 105 |
pipe.load_lora_weights("prithivMLmods/Kontext-Top-Down-View", weight_name="Kontext-Top-Down-View.safetensors", adapter_name="top-down")
|
| 106 |
pipe.load_lora_weights("prithivMLmods/Kontext-Bottom-Up-View", weight_name="Kontext-Bottom-Up-View.safetensors", adapter_name="bottom-up")
|
| 107 |
pipe.load_lora_weights("prithivMLmods/Kontext-CAM-Left-View", weight_name="Kontext-CAM-Left-View.safetensors", adapter_name="left-view")
|
| 108 |
pipe.load_lora_weights("prithivMLmods/Kontext-CAM-Right-View", weight_name="Kontext-CAM-Right-View.safetensors", adapter_name="right-view")
|
| 109 |
pipe.load_lora_weights("starsfriday/Kontext-Remover-General-LoRA", weight_name="kontext_remove.safetensors", adapter_name="kontext-remove")
|
| 110 |
|
|
|
|
| 111 |
aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
|
| 112 |
|
| 113 |
@spaces.GPU
|
| 114 |
def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
|
| 115 |
"""
|
| 116 |
-
Perform image editing and optional upscaling, returning
|
| 117 |
"""
|
| 118 |
if not input_image:
|
| 119 |
raise gr.Error("Please upload an image for editing.")
|
|
@@ -148,15 +148,15 @@ def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_s
|
|
| 148 |
progress(0.8, desc="Upscaling image...")
|
| 149 |
image = aura_sr.upscale_4x(image)
|
| 150 |
|
| 151 |
-
return
|
| 152 |
|
| 153 |
@spaces.GPU
|
| 154 |
def infer_example(input_image, prompt, lora_adapter):
|
| 155 |
"""
|
| 156 |
-
Wrapper function for gr.Examples
|
| 157 |
"""
|
| 158 |
-
|
| 159 |
-
return
|
| 160 |
|
| 161 |
css="""
|
| 162 |
#col-container {
|
|
@@ -174,16 +174,16 @@ with gr.Blocks(css=css, theme=orange_red_theme) as demo:
|
|
| 174 |
|
| 175 |
with gr.Row():
|
| 176 |
with gr.Column():
|
| 177 |
-
input_image = gr.Image(label="Upload Image", type="pil", height=
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
with gr.Accordion("Advanced Settings", open=False):
|
| 188 |
|
| 189 |
seed = gr.Slider(
|
|
@@ -213,7 +213,7 @@ with gr.Blocks(css=css, theme=orange_red_theme) as demo:
|
|
| 213 |
)
|
| 214 |
|
| 215 |
with gr.Column():
|
| 216 |
-
|
| 217 |
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 218 |
|
| 219 |
with gr.Row():
|
|
@@ -235,7 +235,7 @@ with gr.Blocks(css=css, theme=orange_red_theme) as demo:
|
|
| 235 |
["examples/5.jpg", "[photo content], generate the right-side perspective of the scene. Ensure natural lighting, accurate geometry, and realistic textures. Maintain harmony with the original image’s environment, shadows, and visual tone while providing the right-side visual continuation.", "Kontext-CAM-Right-View"],
|
| 236 |
],
|
| 237 |
inputs=[input_image, prompt, lora_adapter],
|
| 238 |
-
outputs=[
|
| 239 |
fn=infer_example,
|
| 240 |
cache_examples="lazy",
|
| 241 |
label="Examples"
|
|
@@ -245,12 +245,12 @@ with gr.Blocks(css=css, theme=orange_red_theme) as demo:
|
|
| 245 |
triggers=[run_button.click, prompt.submit],
|
| 246 |
fn=infer,
|
| 247 |
inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
|
| 248 |
-
outputs=[
|
| 249 |
)
|
| 250 |
|
| 251 |
reuse_button.click(
|
| 252 |
-
fn=lambda
|
| 253 |
-
inputs=[
|
| 254 |
outputs=[input_image]
|
| 255 |
)
|
| 256 |
|
|
|
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
from aura_sr import AuraSR
|
| 14 |
+
|
| 15 |
+
# --- # Device and CUDA Setup Check ---
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
|
| 18 |
+
print("torch.__version__ =", torch.__version__)
|
| 19 |
+
print("torch.version.cuda =", torch.version.cuda)
|
| 20 |
+
print("cuda available:", torch.cuda.is_available())
|
| 21 |
+
print("cuda device count:", torch.cuda.device_count())
|
| 22 |
+
if torch.cuda.is_available():
|
| 23 |
+
print("current device:", torch.cuda.current_device())
|
| 24 |
+
print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
|
| 25 |
+
print("Using device:", device)
|
| 26 |
|
| 27 |
from gradio.themes import Soft
|
| 28 |
from gradio.themes.utils import colors, fonts, sizes
|
|
|
|
| 47 |
self,
|
| 48 |
*,
|
| 49 |
primary_hue: colors.Color | str = colors.gray,
|
| 50 |
+
secondary_hue: colors.Color | str = colors.orange_red,
|
| 51 |
neutral_hue: colors.Color | str = colors.slate,
|
| 52 |
text_size: sizes.Size | str = sizes.text_lg,
|
| 53 |
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
|
|
|
| 96 |
orange_red_theme = OrangeRedTheme()
|
| 97 |
|
| 98 |
|
| 99 |
+
# --- Main Model Initialization ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
MAX_SEED = np.iinfo(np.int32).max
|
| 101 |
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 102 |
|
| 103 |
+
# --- Load Adapters ---
|
| 104 |
pipe.load_lora_weights("prithivMLmods/Kontext-Top-Down-View", weight_name="Kontext-Top-Down-View.safetensors", adapter_name="top-down")
|
| 105 |
pipe.load_lora_weights("prithivMLmods/Kontext-Bottom-Up-View", weight_name="Kontext-Bottom-Up-View.safetensors", adapter_name="bottom-up")
|
| 106 |
pipe.load_lora_weights("prithivMLmods/Kontext-CAM-Left-View", weight_name="Kontext-CAM-Left-View.safetensors", adapter_name="left-view")
|
| 107 |
pipe.load_lora_weights("prithivMLmods/Kontext-CAM-Right-View", weight_name="Kontext-CAM-Right-View.safetensors", adapter_name="right-view")
|
| 108 |
pipe.load_lora_weights("starsfriday/Kontext-Remover-General-LoRA", weight_name="kontext_remove.safetensors", adapter_name="kontext-remove")
|
| 109 |
|
| 110 |
+
# --- Upscaler Initialization ---
|
| 111 |
aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
|
| 112 |
|
| 113 |
@spaces.GPU
|
| 114 |
def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
|
| 115 |
"""
|
| 116 |
+
Perform image editing and optional upscaling, returning the final image.
|
| 117 |
"""
|
| 118 |
if not input_image:
|
| 119 |
raise gr.Error("Please upload an image for editing.")
|
|
|
|
| 148 |
progress(0.8, desc="Upscaling image...")
|
| 149 |
image = aura_sr.upscale_4x(image)
|
| 150 |
|
| 151 |
+
return image, seed, gr.Button(visible=True)
|
| 152 |
|
| 153 |
@spaces.GPU
|
| 154 |
def infer_example(input_image, prompt, lora_adapter):
|
| 155 |
"""
|
| 156 |
+
Wrapper function for gr.Examples.
|
| 157 |
"""
|
| 158 |
+
image, seed, _ = infer(input_image, prompt, lora_adapter, upscale_image=False)
|
| 159 |
+
return image, seed
|
| 160 |
|
| 161 |
css="""
|
| 162 |
#col-container {
|
|
|
|
| 174 |
|
| 175 |
with gr.Row():
|
| 176 |
with gr.Column():
|
| 177 |
+
input_image = gr.Image(label="Upload Image", type="pil", height=290)
|
| 178 |
+
|
| 179 |
+
prompt = gr.Text(
|
| 180 |
+
label="Edit Prompt",
|
| 181 |
+
show_label=True,
|
| 182 |
+
placeholder="e.g., transform into anime..",
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
run_button = gr.Button("Edit Image", variant="primary")
|
| 186 |
+
|
| 187 |
with gr.Accordion("Advanced Settings", open=False):
|
| 188 |
|
| 189 |
seed = gr.Slider(
|
|
|
|
| 213 |
)
|
| 214 |
|
| 215 |
with gr.Column():
|
| 216 |
+
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=355)
|
| 217 |
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 218 |
|
| 219 |
with gr.Row():
|
|
|
|
| 235 |
["examples/5.jpg", "[photo content], generate the right-side perspective of the scene. Ensure natural lighting, accurate geometry, and realistic textures. Maintain harmony with the original image’s environment, shadows, and visual tone while providing the right-side visual continuation.", "Kontext-CAM-Right-View"],
|
| 236 |
],
|
| 237 |
inputs=[input_image, prompt, lora_adapter],
|
| 238 |
+
outputs=[output_image, seed],
|
| 239 |
fn=infer_example,
|
| 240 |
cache_examples="lazy",
|
| 241 |
label="Examples"
|
|
|
|
| 245 |
triggers=[run_button.click, prompt.submit],
|
| 246 |
fn=infer,
|
| 247 |
inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
|
| 248 |
+
outputs=[output_image, seed, reuse_button]
|
| 249 |
)
|
| 250 |
|
| 251 |
reuse_button.click(
|
| 252 |
+
fn=lambda x: x,
|
| 253 |
+
inputs=[output_image],
|
| 254 |
outputs=[input_image]
|
| 255 |
)
|
| 256 |
|