Update app.py
Browse files
app.py
CHANGED
@@ -591,12 +591,193 @@
|
|
591 |
|
592 |
|
593 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
594 |
import logging
|
595 |
import random
|
596 |
import warnings
|
597 |
import gradio as gr
|
598 |
import os
|
599 |
-
import shutil
|
600 |
import subprocess
|
601 |
import torch
|
602 |
import numpy as np
|
@@ -610,6 +791,8 @@ 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)
|
@@ -672,7 +855,7 @@ def process_input(input_image, upscale_factor):
|
|
672 |
|
673 |
# Resize if input size exceeds the maximum pixel budget
|
674 |
if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
|
675 |
-
warnings.warn(
|
676 |
input_image = input_image.resize(
|
677 |
(
|
678 |
int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
|
@@ -753,7 +936,6 @@ def run_gradio_app():
|
|
753 |
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Inference Steps")
|
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
|
@@ -762,12 +944,19 @@ def run_gradio_app():
|
|
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=[
|
766 |
|
767 |
app.launch()
|
768 |
|
769 |
if __name__ == "__main__":
|
770 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
771 |
|
772 |
|
773 |
|
|
|
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
|
603 |
+
# from diffusers import FluxControlNetModel
|
604 |
+
# from diffusers.pipelines import FluxControlNetPipeline
|
605 |
+
# from PIL import Image
|
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 |
+
|
625 |
+
# # Determine the device (GPU or CPU)
|
626 |
+
# if torch.cuda.is_available():
|
627 |
+
# device = "cuda"
|
628 |
+
# logger.info("CUDA is available. Using GPU.")
|
629 |
+
# else:
|
630 |
+
# device = "cpu"
|
631 |
+
# logger.info("CUDA is not available. Using CPU.")
|
632 |
+
|
633 |
+
# # Load model from Huggingface Hub
|
634 |
+
# huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
635 |
+
# if huggingface_token:
|
636 |
+
# login(token=huggingface_token)
|
637 |
+
# logger.info("Hugging Face token found and logged in.")
|
638 |
+
# else:
|
639 |
+
# logger.warning("Hugging Face token not found in environment variables.")
|
640 |
+
|
641 |
+
# # Download model using snapshot_download
|
642 |
+
# model_path = snapshot_download(
|
643 |
+
# repo_id="black-forest-labs/FLUX.1-dev",
|
644 |
+
# repo_type="model",
|
645 |
+
# ignore_patterns=["*.md", "*..gitattributes"],
|
646 |
+
# local_dir="FLUX.1-dev",
|
647 |
+
# token=huggingface_token
|
648 |
+
# )
|
649 |
+
# logger.info("Model downloaded to: %s", model_path)
|
650 |
+
|
651 |
+
# # Load pipeline
|
652 |
+
# logger.info('Loading ControlNet model.')
|
653 |
+
# controlnet = FluxControlNetModel.from_pretrained(
|
654 |
+
# "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
|
655 |
+
# ).to(device)
|
656 |
+
# logger.info("ControlNet model loaded successfully.")
|
657 |
+
|
658 |
+
# logger.info('Loading pipeline.')
|
659 |
+
# pipe = FluxControlNetPipeline.from_pretrained(
|
660 |
+
# model_path, controlnet=controlnet, torch_dtype=torch.bfloat16
|
661 |
+
# ).to(device)
|
662 |
+
# logger.info("Pipeline loaded successfully.")
|
663 |
+
|
664 |
+
# MAX_SEED = 1000000
|
665 |
+
# MAX_PIXEL_BUDGET = 1024 * 1024
|
666 |
+
|
667 |
+
# @spaces.GPU
|
668 |
+
# def process_input(input_image, upscale_factor):
|
669 |
+
# w, h = input_image.size
|
670 |
+
# aspect_ratio = w / h
|
671 |
+
# was_resized = False
|
672 |
+
|
673 |
+
# # Resize if input size exceeds the maximum pixel budget
|
674 |
+
# if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
|
675 |
+
# warnings.warn(f"Requested output image is too large. Resizing to fit within pixel budget.")
|
676 |
+
# input_image = input_image.resize(
|
677 |
+
# (
|
678 |
+
# int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
|
679 |
+
# int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
|
680 |
+
# )
|
681 |
+
# )
|
682 |
+
# was_resized = True
|
683 |
+
|
684 |
+
# # Adjust dimensions to be a multiple of 8
|
685 |
+
# w, h = input_image.size
|
686 |
+
# w = w - w % 8
|
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):
|
693 |
+
# logger.info("Processing inference.")
|
694 |
+
# input_image, was_resized = process_input(input_image, upscale_factor)
|
695 |
+
|
696 |
+
# # Rescale image for ControlNet processing
|
697 |
+
# w, h = input_image.size
|
698 |
+
# control_image = input_image.resize((w * upscale_factor, h * upscale_factor))
|
699 |
+
|
700 |
+
# # Set the random generator for inference
|
701 |
+
# generator = torch.Generator().manual_seed(seed)
|
702 |
+
|
703 |
+
# # Perform inference using the pipeline
|
704 |
+
# logger.info("Running pipeline.")
|
705 |
+
# image = pipe(
|
706 |
+
# prompt="",
|
707 |
+
# control_image=control_image,
|
708 |
+
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
709 |
+
# num_inference_steps=num_inference_steps,
|
710 |
+
# guidance_scale=3.5,
|
711 |
+
# height=control_image.size[1],
|
712 |
+
# width=control_image.size[0],
|
713 |
+
# generator=generator,
|
714 |
+
# ).images[0]
|
715 |
+
|
716 |
+
# # Resize output image back to the original dimensions if needed
|
717 |
+
# if was_resized:
|
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:
|
747 |
+
# gr.Markdown("## Image Upscaler using ControlNet")
|
748 |
+
|
749 |
+
# # Define the inputs and outputs
|
750 |
+
# input_image = gr.Image(type="pil", label="Input Image")
|
751 |
+
# upscale_factor = gr.Slider(minimum=1, maximum=8, step=1, label="Upscale Factor")
|
752 |
+
# seed = gr.Slider(minimum=0, maximum=100, step=1, label="Seed")
|
753 |
+
# num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Inference Steps")
|
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")
|
761 |
+
|
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 |
+
|
769 |
+
# if __name__ == "__main__":
|
770 |
+
# run_gradio_app()
|
771 |
+
|
772 |
+
|
773 |
+
|
774 |
+
|
775 |
import logging
|
776 |
import random
|
777 |
import warnings
|
778 |
import gradio as gr
|
779 |
import os
|
780 |
+
import shutil
|
781 |
import subprocess
|
782 |
import torch
|
783 |
import numpy as np
|
|
|
791 |
from fastapi.responses import JSONResponse
|
792 |
from fastapi.middleware.cors import CORSMiddleware
|
793 |
from concurrent.futures import ThreadPoolExecutor
|
794 |
+
import uvicorn
|
795 |
+
import spaces
|
796 |
|
797 |
# Configure logging
|
798 |
logging.basicConfig(level=logging.INFO)
|
|
|
855 |
|
856 |
# Resize if input size exceeds the maximum pixel budget
|
857 |
if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
|
858 |
+
warnings.warn("Requested output image is too large. Resizing to fit within pixel budget.")
|
859 |
input_image = input_image.resize(
|
860 |
(
|
861 |
int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
|
|
|
936 |
num_inference_steps = gr.Slider(minimum=1, maximum=100, step=1, label="Inference Steps")
|
937 |
controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, label="ControlNet Conditioning Scale")
|
938 |
|
|
|
939 |
output_base64 = gr.Textbox(label="Base64 String", interactive=False)
|
940 |
|
941 |
# Create a button to trigger the processing
|
|
|
944 |
# Define the function to run when the button is clicked
|
945 |
submit_button.click(run_inference,
|
946 |
inputs=[input_image, upscale_factor, seed, num_inference_steps, controlnet_conditioning_scale],
|
947 |
+
outputs=[output_base64])
|
948 |
|
949 |
app.launch()
|
950 |
|
951 |
if __name__ == "__main__":
|
952 |
+
# Run Gradio app in a separate thread
|
953 |
+
import threading
|
954 |
+
gradio_thread = threading.Thread(target=run_gradio_app)
|
955 |
+
gradio_thread.start()
|
956 |
+
|
957 |
+
# Start FastAPI server
|
958 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
959 |
+
|
960 |
|
961 |
|
962 |
|