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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
import logging
|
| 265 |
import random
|
| 266 |
import warnings
|
| 267 |
import gradio as gr
|
| 268 |
import os
|
| 269 |
import shutil
|
|
|
|
| 270 |
import subprocess
|
| 271 |
import torch
|
| 272 |
import numpy as np
|
|
@@ -276,34 +454,14 @@ from PIL import Image
|
|
| 276 |
from huggingface_hub import snapshot_download, login
|
| 277 |
import io
|
| 278 |
import base64
|
| 279 |
-
import
|
| 280 |
|
| 281 |
# Configure logging
|
| 282 |
logging.basicConfig(level=logging.INFO)
|
| 283 |
logger = logging.getLogger(__name__)
|
| 284 |
|
| 285 |
-
#
|
| 286 |
-
|
| 287 |
-
result = subprocess.run(['df', '-h'], capture_output=True, text=True)
|
| 288 |
-
logger.info("Disk space usage:\n%s", result.stdout)
|
| 289 |
-
|
| 290 |
-
# Function to clear Hugging Face cache
|
| 291 |
-
def clear_huggingface_cache():
|
| 292 |
-
cache_dir = os.path.expanduser('~/.cache/huggingface')
|
| 293 |
-
if os.path.exists(cache_dir):
|
| 294 |
-
shutil.rmtree(cache_dir) # Removes the entire cache directory
|
| 295 |
-
logger.info("Cleared Hugging Face cache at: %s", cache_dir)
|
| 296 |
-
else:
|
| 297 |
-
logger.info("No Hugging Face cache found.")
|
| 298 |
-
|
| 299 |
-
# Check disk space
|
| 300 |
-
check_disk_space()
|
| 301 |
-
|
| 302 |
-
# Clear Hugging Face cache
|
| 303 |
-
clear_huggingface_cache()
|
| 304 |
-
|
| 305 |
-
# Add config to store base64 images
|
| 306 |
-
image_outputs = {}
|
| 307 |
|
| 308 |
# Determine the device (GPU or CPU)
|
| 309 |
if torch.cuda.is_available():
|
|
@@ -323,8 +481,8 @@ else:
|
|
| 323 |
|
| 324 |
# Download model using snapshot_download
|
| 325 |
model_path = snapshot_download(
|
| 326 |
-
repo_id="black-forest-labs/FLUX.1-dev",
|
| 327 |
-
repo_type="model",
|
| 328 |
ignore_patterns=["*.md", "*..gitattributes"],
|
| 329 |
local_dir="FLUX.1-dev",
|
| 330 |
token=huggingface_token
|
|
@@ -347,6 +505,7 @@ logger.info("Pipeline loaded successfully.")
|
|
| 347 |
MAX_SEED = 1000000
|
| 348 |
MAX_PIXEL_BUDGET = 1024 * 1024
|
| 349 |
|
|
|
|
| 350 |
def process_input(input_image, upscale_factor):
|
| 351 |
w, h = input_image.size
|
| 352 |
aspect_ratio = w / h
|
|
@@ -369,9 +528,11 @@ def process_input(input_image, upscale_factor):
|
|
| 369 |
h = h - h % 8
|
| 370 |
|
| 371 |
return input_image.resize((w, h)), was_resized
|
|
|
|
| 372 |
|
|
|
|
| 373 |
def run_inference(input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale):
|
| 374 |
-
logger.info("
|
| 375 |
input_image, was_resized = process_input(input_image, upscale_factor)
|
| 376 |
|
| 377 |
# Rescale image for ControlNet processing
|
|
@@ -382,7 +543,7 @@ def run_inference(input_image, upscale_factor, seed, num_inference_steps, contro
|
|
| 382 |
generator = torch.Generator().manual_seed(seed)
|
| 383 |
|
| 384 |
# Perform inference using the pipeline
|
| 385 |
-
logger.info("Running pipeline")
|
| 386 |
image = pipe(
|
| 387 |
prompt="",
|
| 388 |
control_image=control_image,
|
|
@@ -399,43 +560,34 @@ def run_inference(input_image, upscale_factor, seed, num_inference_steps, contro
|
|
| 399 |
original_size = (input_image.width * upscale_factor, input_image.height * upscale_factor)
|
| 400 |
image = image.resize(original_size)
|
| 401 |
|
| 402 |
-
|
| 403 |
-
buffered = io.BytesIO()
|
| 404 |
-
image.save(buffered, format="JPEG")
|
| 405 |
-
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 406 |
|
| 407 |
-
|
| 408 |
-
|
|
|
|
| 409 |
|
| 410 |
-
# Define
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
seed =
|
| 414 |
-
|
|
|
|
| 415 |
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
#
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
],
|
| 431 |
-
outputs=gr.Image(label="Output Image"),
|
| 432 |
-
title="ControlNet Image Upscaling",
|
| 433 |
-
description="Upload an image to upscale using the ControlNet model."
|
| 434 |
-
)
|
| 435 |
|
| 436 |
-
# Launch Gradio app
|
| 437 |
-
if __name__ == '__main__':
|
| 438 |
-
iface.launch()
|
| 439 |
|
| 440 |
|
| 441 |
|
|
|
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
+
# import logging
|
| 265 |
+
# import random
|
| 266 |
+
# import warnings
|
| 267 |
+
# import gradio as gr
|
| 268 |
+
# import os
|
| 269 |
+
# import shutil
|
| 270 |
+
# import subprocess
|
| 271 |
+
# import torch
|
| 272 |
+
# import numpy as np
|
| 273 |
+
# from diffusers import FluxControlNetModel
|
| 274 |
+
# from diffusers.pipelines import FluxControlNetPipeline
|
| 275 |
+
# from PIL import Image
|
| 276 |
+
# from huggingface_hub import snapshot_download, login
|
| 277 |
+
# import io
|
| 278 |
+
# import base64
|
| 279 |
+
# import threading
|
| 280 |
+
|
| 281 |
+
# # Configure logging
|
| 282 |
+
# logging.basicConfig(level=logging.INFO)
|
| 283 |
+
# logger = logging.getLogger(__name__)
|
| 284 |
+
|
| 285 |
+
# # Function to check disk usage
|
| 286 |
+
# def check_disk_space():
|
| 287 |
+
# result = subprocess.run(['df', '-h'], capture_output=True, text=True)
|
| 288 |
+
# logger.info("Disk space usage:\n%s", result.stdout)
|
| 289 |
+
|
| 290 |
+
# # Function to clear Hugging Face cache
|
| 291 |
+
# def clear_huggingface_cache():
|
| 292 |
+
# cache_dir = os.path.expanduser('~/.cache/huggingface')
|
| 293 |
+
# if os.path.exists(cache_dir):
|
| 294 |
+
# shutil.rmtree(cache_dir) # Removes the entire cache directory
|
| 295 |
+
# logger.info("Cleared Hugging Face cache at: %s", cache_dir)
|
| 296 |
+
# else:
|
| 297 |
+
# logger.info("No Hugging Face cache found.")
|
| 298 |
+
|
| 299 |
+
# # Check disk space
|
| 300 |
+
# check_disk_space()
|
| 301 |
+
|
| 302 |
+
# # Clear Hugging Face cache
|
| 303 |
+
# clear_huggingface_cache()
|
| 304 |
+
|
| 305 |
+
# # Add config to store base64 images
|
| 306 |
+
# image_outputs = {}
|
| 307 |
+
|
| 308 |
+
# # Determine the device (GPU or CPU)
|
| 309 |
+
# if torch.cuda.is_available():
|
| 310 |
+
# device = "cuda"
|
| 311 |
+
# logger.info("CUDA is available. Using GPU.")
|
| 312 |
+
# else:
|
| 313 |
+
# device = "cpu"
|
| 314 |
+
# logger.info("CUDA is not available. Using CPU.")
|
| 315 |
+
|
| 316 |
+
# # Load model from Huggingface Hub
|
| 317 |
+
# huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 318 |
+
# if huggingface_token:
|
| 319 |
+
# login(token=huggingface_token)
|
| 320 |
+
# logger.info("Hugging Face token found and logged in.")
|
| 321 |
+
# else:
|
| 322 |
+
# logger.warning("Hugging Face token not found in environment variables.")
|
| 323 |
+
|
| 324 |
+
# # Download model using snapshot_download
|
| 325 |
+
# model_path = snapshot_download(
|
| 326 |
+
# repo_id="black-forest-labs/FLUX.1-dev",
|
| 327 |
+
# repo_type="model",
|
| 328 |
+
# ignore_patterns=["*.md", "*..gitattributes"],
|
| 329 |
+
# local_dir="FLUX.1-dev",
|
| 330 |
+
# token=huggingface_token
|
| 331 |
+
# )
|
| 332 |
+
# logger.info("Model downloaded to: %s", model_path)
|
| 333 |
+
|
| 334 |
+
# # Load pipeline
|
| 335 |
+
# logger.info('Loading ControlNet model.')
|
| 336 |
+
# controlnet = FluxControlNetModel.from_pretrained(
|
| 337 |
+
# "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
|
| 338 |
+
# ).to(device)
|
| 339 |
+
# logger.info("ControlNet model loaded successfully.")
|
| 340 |
+
|
| 341 |
+
# logger.info('Loading pipeline.')
|
| 342 |
+
# pipe = FluxControlNetPipeline.from_pretrained(
|
| 343 |
+
# model_path, controlnet=controlnet, torch_dtype=torch.bfloat16
|
| 344 |
+
# ).to(device)
|
| 345 |
+
# logger.info("Pipeline loaded successfully.")
|
| 346 |
+
|
| 347 |
+
# MAX_SEED = 1000000
|
| 348 |
+
# MAX_PIXEL_BUDGET = 1024 * 1024
|
| 349 |
+
|
| 350 |
+
# def process_input(input_image, upscale_factor):
|
| 351 |
+
# w, h = input_image.size
|
| 352 |
+
# aspect_ratio = w / h
|
| 353 |
+
# was_resized = False
|
| 354 |
+
|
| 355 |
+
# # Resize if input size exceeds the maximum pixel budget
|
| 356 |
+
# if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
|
| 357 |
+
# warnings.warn(f"Requested output image is too large. Resizing to fit within pixel budget.")
|
| 358 |
+
# input_image = input_image.resize(
|
| 359 |
+
# (
|
| 360 |
+
# int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
|
| 361 |
+
# int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
|
| 362 |
+
# )
|
| 363 |
+
# )
|
| 364 |
+
# was_resized = True
|
| 365 |
+
|
| 366 |
+
# # Adjust dimensions to be a multiple of 8
|
| 367 |
+
# w, h = input_image.size
|
| 368 |
+
# w = w - w % 8
|
| 369 |
+
# h = h - h % 8
|
| 370 |
+
|
| 371 |
+
# return input_image.resize((w, h)), was_resized
|
| 372 |
+
|
| 373 |
+
# def run_inference(input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale):
|
| 374 |
+
# logger.info("Running inference")
|
| 375 |
+
# input_image, was_resized = process_input(input_image, upscale_factor)
|
| 376 |
+
|
| 377 |
+
# # Rescale image for ControlNet processing
|
| 378 |
+
# w, h = input_image.size
|
| 379 |
+
# control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
|
| 380 |
+
|
| 381 |
+
# # Set the random generator for inference
|
| 382 |
+
# generator = torch.Generator().manual_seed(seed)
|
| 383 |
+
|
| 384 |
+
# # Perform inference using the pipeline
|
| 385 |
+
# logger.info("Running pipeline")
|
| 386 |
+
# image = pipe(
|
| 387 |
+
# prompt="",
|
| 388 |
+
# control_image=control_image,
|
| 389 |
+
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 390 |
+
# num_inference_steps=num_inference_steps,
|
| 391 |
+
# guidance_scale=3.5,
|
| 392 |
+
# height=control_image.size[1],
|
| 393 |
+
# width=control_image.size[0],
|
| 394 |
+
# generator=generator,
|
| 395 |
+
# ).images[0]
|
| 396 |
+
|
| 397 |
+
# # Resize output image back to the original dimensions if needed
|
| 398 |
+
# if was_resized:
|
| 399 |
+
# original_size = (input_image.width * upscale_factor, input_image.height * upscale_factor)
|
| 400 |
+
# image = image.resize(original_size)
|
| 401 |
+
|
| 402 |
+
# # Convert the output image to base64
|
| 403 |
+
# buffered = io.BytesIO()
|
| 404 |
+
# image.save(buffered, format="JPEG")
|
| 405 |
+
# image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 406 |
+
|
| 407 |
+
# logger.info("Inference completed")
|
| 408 |
+
# return image_base64
|
| 409 |
+
|
| 410 |
+
# # Define Gradio interface
|
| 411 |
+
# def gradio_interface(input_image, upscale_factor=4, seed=42, num_inference_steps=28, controlnet_conditioning_scale=0.6):
|
| 412 |
+
# if randomize_seed:
|
| 413 |
+
# seed = random.randint(0, MAX_SEED)
|
| 414 |
+
# logger.info("Seed randomized to: %d", seed)
|
| 415 |
+
|
| 416 |
+
# # Run inference
|
| 417 |
+
# output_image_base64 = run_inference(input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale)
|
| 418 |
+
|
| 419 |
+
# return Image.open(io.BytesIO(base64.b64decode(output_image_base64)))
|
| 420 |
+
|
| 421 |
+
# # Create Gradio interface
|
| 422 |
+
# iface = gr.Interface(
|
| 423 |
+
# fn=gradio_interface,
|
| 424 |
+
# inputs=[
|
| 425 |
+
# gr.Image(type="pil", label="Input Image"),
|
| 426 |
+
# gr.Slider(min=1, max=8, step=1, label="Upscale Factor"),
|
| 427 |
+
# gr.Slider(min=0, max=MAX_SEED, step=1, label="Seed"),
|
| 428 |
+
# gr.Slider(min=1, max=100, step=1, label="Inference Steps"),
|
| 429 |
+
# gr.Slider(min=0.0, max=1.0, step=0.1, label="ControlNet Conditioning Scale")
|
| 430 |
+
# ],
|
| 431 |
+
# outputs=gr.Image(label="Output Image"),
|
| 432 |
+
# title="ControlNet Image Upscaling",
|
| 433 |
+
# description="Upload an image to upscale using the ControlNet model."
|
| 434 |
+
# )
|
| 435 |
+
|
| 436 |
+
# # Launch Gradio app
|
| 437 |
+
# if __name__ == '__main__':
|
| 438 |
+
# iface.launch()
|
| 439 |
+
|
| 440 |
+
|
| 441 |
import logging
|
| 442 |
import random
|
| 443 |
import warnings
|
| 444 |
import gradio as gr
|
| 445 |
import os
|
| 446 |
import shutil
|
| 447 |
+
import spaces
|
| 448 |
import subprocess
|
| 449 |
import torch
|
| 450 |
import numpy as np
|
|
|
|
| 454 |
from huggingface_hub import snapshot_download, login
|
| 455 |
import io
|
| 456 |
import base64
|
| 457 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 458 |
|
| 459 |
# Configure logging
|
| 460 |
logging.basicConfig(level=logging.INFO)
|
| 461 |
logger = logging.getLogger(__name__)
|
| 462 |
|
| 463 |
+
# ThreadPoolExecutor for managing image processing threads
|
| 464 |
+
executor = ThreadPoolExecutor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
# Determine the device (GPU or CPU)
|
| 467 |
if torch.cuda.is_available():
|
|
|
|
| 481 |
|
| 482 |
# Download model using snapshot_download
|
| 483 |
model_path = snapshot_download(
|
| 484 |
+
repo_id="black-forest-labs/FLUX.1-dev",
|
| 485 |
+
repo_type="model",
|
| 486 |
ignore_patterns=["*.md", "*..gitattributes"],
|
| 487 |
local_dir="FLUX.1-dev",
|
| 488 |
token=huggingface_token
|
|
|
|
| 505 |
MAX_SEED = 1000000
|
| 506 |
MAX_PIXEL_BUDGET = 1024 * 1024
|
| 507 |
|
| 508 |
+
@spaces.GPU
|
| 509 |
def process_input(input_image, upscale_factor):
|
| 510 |
w, h = input_image.size
|
| 511 |
aspect_ratio = w / h
|
|
|
|
| 528 |
h = h - h % 8
|
| 529 |
|
| 530 |
return input_image.resize((w, h)), was_resized
|
| 531 |
+
|
| 532 |
|
| 533 |
+
@spaces.GPU
|
| 534 |
def run_inference(input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale):
|
| 535 |
+
logger.info("Processing inference.")
|
| 536 |
input_image, was_resized = process_input(input_image, upscale_factor)
|
| 537 |
|
| 538 |
# Rescale image for ControlNet processing
|
|
|
|
| 543 |
generator = torch.Generator().manual_seed(seed)
|
| 544 |
|
| 545 |
# Perform inference using the pipeline
|
| 546 |
+
logger.info("Running pipeline.")
|
| 547 |
image = pipe(
|
| 548 |
prompt="",
|
| 549 |
control_image=control_image,
|
|
|
|
| 560 |
original_size = (input_image.width * upscale_factor, input_image.height * upscale_factor)
|
| 561 |
image = image.resize(original_size)
|
| 562 |
|
| 563 |
+
return image
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
+
def run_gradio_app():
|
| 566 |
+
with gr.Blocks() as app:
|
| 567 |
+
gr.Markdown("## Image Upscaler using ControlNet")
|
| 568 |
|
| 569 |
+
# Define the inputs and outputs
|
| 570 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
| 571 |
+
upscale_factor = gr.Slider(minimum=1, maximum=8, step=1, label="Upscale Factor")
|
| 572 |
+
seed = gr.Slider(minimum=0, maximum=100, step=1, label="Seed")
|
| 573 |
+
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Inference Steps")
|
| 574 |
+
controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, label="ControlNet Conditioning Scale")
|
| 575 |
|
| 576 |
+
output_image = gr.Image(type="pil", label="Output Image")
|
| 577 |
+
|
| 578 |
+
# Create a button to trigger the processing
|
| 579 |
+
submit_button = gr.Button("Upscale Image")
|
| 580 |
+
|
| 581 |
+
# Define the function to run when the button is clicked
|
| 582 |
+
submit_button.click(run_inference,
|
| 583 |
+
inputs=[input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale],
|
| 584 |
+
outputs=output_image)
|
| 585 |
+
|
| 586 |
+
app.launch()
|
| 587 |
+
|
| 588 |
+
if __name__ == "__main__":
|
| 589 |
+
run_gradio_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
|
| 593 |
|