Upload handler.py
Browse files- handler.py +75 -0
handler.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import StableDiffusionXLInstantIDPipeline
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load the model
|
| 10 |
+
pretrained_model_name_or_path = "wangqixun/YamerMIX_v8"
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
|
| 13 |
+
pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
|
| 14 |
+
pretrained_model_name_or_path,
|
| 15 |
+
controlnet=[controlnet_identitynet],
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
safety_checker=None,
|
| 18 |
+
feature_extractor=None,
|
| 19 |
+
).to(device)
|
| 20 |
+
|
| 21 |
+
pipe.scheduler = diffusers.EulerDiscreteScheduler.from_config(
|
| 22 |
+
pipe.scheduler.config
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
|
| 26 |
+
pipe.disable_lora()
|
| 27 |
+
|
| 28 |
+
pipe.cuda()
|
| 29 |
+
pipe.load_ip_adapter_instantid(face_adapter)
|
| 30 |
+
pipe.image_proj_model.to("cuda")
|
| 31 |
+
pipe.unet.to("cuda")
|
| 32 |
+
|
| 33 |
+
class GenerateImageRequest(BaseModel):
|
| 34 |
+
prompt: str
|
| 35 |
+
negative_prompt: str
|
| 36 |
+
style: str
|
| 37 |
+
num_steps: int
|
| 38 |
+
identitynet_strength_ratio: float
|
| 39 |
+
adapter_strength_ratio: float
|
| 40 |
+
pose_strength: float
|
| 41 |
+
canny_strength: float
|
| 42 |
+
depth_strength: float
|
| 43 |
+
controlnet_selection: str
|
| 44 |
+
guidance_scale: float
|
| 45 |
+
seed: int
|
| 46 |
+
enable_LCM: bool
|
| 47 |
+
enhance_face_region: bool
|
| 48 |
+
|
| 49 |
+
@app.post("/generate-image/")
|
| 50 |
+
async def generate_image(request: GenerateImageRequest, face_file: UploadFile = File(...), pose_file: UploadFile = File(...)):
|
| 51 |
+
try:
|
| 52 |
+
face_image = Image.open(face_file.file)
|
| 53 |
+
pose_image = Image.open(pose_file.file)
|
| 54 |
+
output = pipe(
|
| 55 |
+
prompt=request.prompt,
|
| 56 |
+
negative_prompt=request.negative_prompt,
|
| 57 |
+
image=face_image,
|
| 58 |
+
control_image=pose_image,
|
| 59 |
+
controlnet_strength=request.pose_strength,
|
| 60 |
+
num_inference_steps=request.num_steps,
|
| 61 |
+
guidance_scale=request.guidance_scale,
|
| 62 |
+
eta=0.0,
|
| 63 |
+
generator=torch.manual_seed(request.seed),
|
| 64 |
+
output_type="pil",
|
| 65 |
+
return_dict=True,
|
| 66 |
+
callback=None,
|
| 67 |
+
callback_steps=1,
|
| 68 |
+
).images
|
| 69 |
+
return {"image": output[0]}
|
| 70 |
+
except Exception as e:
|
| 71 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
import uvicorn
|
| 75 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|