import os import random import uuid import numpy as np from PIL import Image import spaces import torch from diffusers import DiffusionPipeline from flask import Flask, flash, request from flask_session import Session app = Flask(__name__) app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" Session(app) DESCRIPTION = """# Playground v2.5""" if not torch.cuda.is_available(): DESCRIPTION += "\n

Running on CPU 🥶 This demo may not work on CPU.

" MAX_SEED = np.iinfo(np.int32).max CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1" MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536")) USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1" ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1" device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") NUM_IMAGES_PER_PROMPT = 1 if torch.cuda.is_available(): pipe = DiffusionPipeline.from_pretrained( "playgroundai/playground-v2.5-1024px-aesthetic", torch_dtype=torch.float16, use_safetensors=True, add_watermarker=False, variant="fp16" ) if ENABLE_CPU_OFFLOAD: pipe.enable_model_cpu_offload() else: pipe.to(device) print("Loaded on Device!") if USE_TORCH_COMPILE: pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) print("Model Compiled!") def save_image(img): unique_name = str(uuid.uuid4()) + ".png" img.save(unique_name) return unique_name def randomize_seed_fn(seed: int, randomize_seed: bool) -> int: if randomize_seed: seed = random.randint(0, MAX_SEED) return seed @spaces.GPU(enable_queue=True) def generate( prompt: str, negative_prompt: str = "", use_negative_prompt: bool = False, seed: int = 0, width: int = 1024, height: int = 1024, guidance_scale: float = 3, randomize_seed: bool = False, use_resolution_binning: bool = True, ): pipe = DiffusionPipeline.from_pretrained( "playgroundai/playground-v2.5-1024px-aesthetic", torch_dtype=torch.float16, use_safetensors=True, add_watermarker=False, variant="fp16" ) if ENABLE_CPU_OFFLOAD: pipe.enable_model_cpu_offload() else: pipe.to(device) print("Loaded on Device!") if USE_TORCH_COMPILE: pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True) print("Model Compiled!") pipe.to('cpu') seed = int(randomize_seed_fn(seed, randomize_seed)) generator = torch.Generator().manual_seed(seed) if not use_negative_prompt: negative_prompt = None # type: ignore images = pipe( prompt=prompt, negative_prompt=negative_prompt, width=width, height=height, guidance_scale=guidance_scale, num_inference_steps=25, generator=generator, num_images_per_prompt=NUM_IMAGES_PER_PROMPT, use_resolution_binning=use_resolution_binning, output_type="pil", ).images image_paths = [save_image(img) for img in images] print(image_paths) return image_paths, seed examples = [ "neon holography crystal cat", "a cat eating a piece of cheese", "an astronaut riding a horse in space", "a cartoon of a boy playing with a tiger", "a cute robot artist painting on an easel, concept art", "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone" ] @app.route("/", methods=['GET', 'POST']) def index(): if request.method == 'POST': if 'file' and 'file1' not in request.files: flash('No file part') return {"status": "Failed", "message": "Please Provide file name(file)."} file = request.files['file'] file1 = request.files['file1'] image = Image.open(file) image1 = Image.open(file1) preprocess_image = generate('a boy playing with basketball') # print(preprocess_image) return {"status": "Success", "message": "You can download the 3D model.", "data": preprocess_image} else: return { "status": "Success", "message":"You can upload an image file to get the 3D model." } if "__main__" == __name__: app.run(debug=True, port=7860, host="0.0.0.0")