Update app.py
Browse files
app.py
CHANGED
@@ -438,13 +438,165 @@
|
|
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,12 +606,19 @@ from PIL import Image
|
|
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 |
|
@@ -528,7 +687,6 @@ def process_input(input_image, upscale_factor):
|
|
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):
|
@@ -560,7 +718,29 @@ def run_inference(input_image, upscale_factor, seed, num_inference_steps, contro
|
|
560 |
original_size = (input_image.width * upscale_factor, input_image.height * upscale_factor)
|
561 |
image = image.resize(original_size)
|
562 |
|
563 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
|
565 |
def run_gradio_app():
|
566 |
with gr.Blocks() as app:
|
@@ -574,6 +754,7 @@ def run_gradio_app():
|
|
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")
|
@@ -581,7 +762,7 @@ def run_gradio_app():
|
|
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 |
|
|
|
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
|
451 |
+
# from diffusers import FluxControlNetModel
|
452 |
+
# from diffusers.pipelines import FluxControlNetPipeline
|
453 |
+
# from PIL import Image
|
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():
|
468 |
+
# device = "cuda"
|
469 |
+
# logger.info("CUDA is available. Using GPU.")
|
470 |
+
# else:
|
471 |
+
# device = "cpu"
|
472 |
+
# logger.info("CUDA is not available. Using CPU.")
|
473 |
+
|
474 |
+
# # Load model from Huggingface Hub
|
475 |
+
# huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
476 |
+
# if huggingface_token:
|
477 |
+
# login(token=huggingface_token)
|
478 |
+
# logger.info("Hugging Face token found and logged in.")
|
479 |
+
# else:
|
480 |
+
# logger.warning("Hugging Face token not found in environment variables.")
|
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
|
489 |
+
# )
|
490 |
+
# logger.info("Model downloaded to: %s", model_path)
|
491 |
+
|
492 |
+
# # Load pipeline
|
493 |
+
# logger.info('Loading ControlNet model.')
|
494 |
+
# controlnet = FluxControlNetModel.from_pretrained(
|
495 |
+
# "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
|
496 |
+
# ).to(device)
|
497 |
+
# logger.info("ControlNet model loaded successfully.")
|
498 |
+
|
499 |
+
# logger.info('Loading pipeline.')
|
500 |
+
# pipe = FluxControlNetPipeline.from_pretrained(
|
501 |
+
# model_path, controlnet=controlnet, torch_dtype=torch.bfloat16
|
502 |
+
# ).to(device)
|
503 |
+
# logger.info("Pipeline loaded successfully.")
|
504 |
+
|
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
|
512 |
+
# was_resized = False
|
513 |
+
|
514 |
+
# # Resize if input size exceeds the maximum pixel budget
|
515 |
+
# if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
|
516 |
+
# warnings.warn(f"Requested output image is too large. Resizing to fit within pixel budget.")
|
517 |
+
# input_image = input_image.resize(
|
518 |
+
# (
|
519 |
+
# int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
|
520 |
+
# int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
|
521 |
+
# )
|
522 |
+
# )
|
523 |
+
# was_resized = True
|
524 |
+
|
525 |
+
# # Adjust dimensions to be a multiple of 8
|
526 |
+
# w, h = input_image.size
|
527 |
+
# w = w - w % 8
|
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
|
539 |
+
# w, h = input_image.size
|
540 |
+
# control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
|
541 |
+
|
542 |
+
# # Set the random generator for inference
|
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,
|
550 |
+
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
551 |
+
# num_inference_steps=num_inference_steps,
|
552 |
+
# guidance_scale=3.5,
|
553 |
+
# height=control_image.size[1],
|
554 |
+
# width=control_image.size[0],
|
555 |
+
# generator=generator,
|
556 |
+
# ).images[0]
|
557 |
+
|
558 |
+
# # Resize output image back to the original dimensions if needed
|
559 |
+
# if was_resized:
|
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 |
+
|
594 |
import logging
|
595 |
import random
|
596 |
import warnings
|
597 |
import gradio as gr
|
598 |
import os
|
599 |
+
import shutil,spaces
|
|
|
600 |
import subprocess
|
601 |
import torch
|
602 |
import numpy as np
|
|
|
606 |
from huggingface_hub import snapshot_download, login
|
607 |
import io
|
608 |
import base64
|
609 |
+
from fastapi import FastAPI, File, UploadFile
|
610 |
+
from fastapi.responses import JSONResponse
|
611 |
+
from fastapi.middleware.cors import CORSMiddleware
|
612 |
from concurrent.futures import ThreadPoolExecutor
|
613 |
|
614 |
# Configure logging
|
615 |
logging.basicConfig(level=logging.INFO)
|
616 |
logger = logging.getLogger(__name__)
|
617 |
|
618 |
+
# FastAPI app for image processing
|
619 |
+
app = FastAPI()
|
620 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
621 |
+
|
622 |
# ThreadPoolExecutor for managing image processing threads
|
623 |
executor = ThreadPoolExecutor()
|
624 |
|
|
|
687 |
h = h - h % 8
|
688 |
|
689 |
return input_image.resize((w, h)), was_resized
|
|
|
690 |
|
691 |
@spaces.GPU
|
692 |
def run_inference(input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale):
|
|
|
718 |
original_size = (input_image.width * upscale_factor, input_image.height * upscale_factor)
|
719 |
image = image.resize(original_size)
|
720 |
|
721 |
+
# Convert the output image to base64
|
722 |
+
buffered = io.BytesIO()
|
723 |
+
image.save(buffered, format="JPEG")
|
724 |
+
image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
725 |
+
|
726 |
+
return image_base64
|
727 |
+
|
728 |
+
@app.post("/infer")
|
729 |
+
async def infer(input_image: UploadFile = File(...),
|
730 |
+
upscale_factor: int = 4,
|
731 |
+
seed: int = 42,
|
732 |
+
num_inference_steps: int = 28,
|
733 |
+
controlnet_conditioning_scale: float = 0.6):
|
734 |
+
logger.info("Received request for inference.")
|
735 |
+
|
736 |
+
# Read the uploaded image
|
737 |
+
contents = await input_image.read()
|
738 |
+
image = Image.open(io.BytesIO(contents))
|
739 |
+
|
740 |
+
# Run inference in a separate thread
|
741 |
+
base64_image = await executor.submit(run_inference, image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale)
|
742 |
+
|
743 |
+
return JSONResponse(content={"base64_image": base64_image})
|
744 |
|
745 |
def run_gradio_app():
|
746 |
with gr.Blocks() as app:
|
|
|
754 |
controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, label="ControlNet Conditioning Scale")
|
755 |
|
756 |
output_image = gr.Image(type="pil", label="Output Image")
|
757 |
+
output_base64 = gr.Textbox(label="Base64 String", interactive=False)
|
758 |
|
759 |
# Create a button to trigger the processing
|
760 |
submit_button = gr.Button("Upscale Image")
|
|
|
762 |
# Define the function to run when the button is clicked
|
763 |
submit_button.click(run_inference,
|
764 |
inputs=[input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale],
|
765 |
+
outputs=[output_image, output_base64])
|
766 |
|
767 |
app.launch()
|
768 |
|