unik-ml / main.py
RoniFinTech's picture
gpu
dcb122f
raw
history blame
No virus
2.59 kB
from io import BytesIO
import torch
from diffusers import DiffusionPipeline
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
# load both base & refiner
base = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
base.to("cuda")
# base.enable_model_cpu_offload()
base.enable_attention_slicing()
refiner = DiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0",
text_encoder_2=base.text_encoder_2,
vae=base.vae,
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16",
)
refiner.to("cuda")
# refiner.enable_model_cpu_offload()
refiner.enable_attention_slicing()
# Create a new FastAPI app instance
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True,
)
@app.get("/")
async def root():
return {"message": "UNIK ML API"}
@app.get("/generate")
def generate(text: str):
"""
generate image
"""
# Define how many steps and what % of steps to be run on each experts (80/20) here
n_steps = 40
high_noise_frac = 0.8
negative = "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly. bad anatomy, disfigured, poorly drawn face, mutation, mutated, extra limb, ugly, disgusting, poorly drawn hands, missing limb, floating limbs, disconnected limbs, malformed hands, blurry, mutated hands and fingers, watermark, watermarked, oversaturated, censored, distorted hands, amputation, missing hands, obese, doubled face, double hands, two women, anime style, cartoon, toon."
prompt = "Designs should play with different textures and layering but stick to a monochrome palette. Think leather jackets over mesh tops, or satin draped over matte cotton. in a studio. zoomed-in. single model."
# run both experts
image = base(
prompt=prompt,
negative_prompt=negative,
num_inference_steps=n_steps,
denoising_end=high_noise_frac,
output_type="latent",
).images
final_image = refiner(
prompt=prompt,
negative_prompt=negative,
num_inference_steps=n_steps,
denoising_start=high_noise_frac,
image=image,
).images[0]
buffer = BytesIO()
final_image.save(buffer, format="PNG")
image_bytes = buffer.getvalue()
return StreamingResponse(BytesIO(image_bytes), media_type="image/png")