Skip to content

Commit

Permalink
try to fix img2img
Browse files Browse the repository at this point in the history
  • Loading branch information
JEMeyer committed May 16, 2024
1 parent 500c859 commit f2d6adc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ COPY . .

# Install the stablediffusion_fastapi_multigpu package
RUN pip3 install .
# RUN pip3 install torch==2.1.0+cu118 --index-url https://download.pytorch.org/

# Export the port
EXPOSE 8000
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"Pillow",
"xformers",
"torch",
"torchvision",
],
)
16 changes: 9 additions & 7 deletions stablediffusion_fastapi_multigpu/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uuid
import os
from PIL import Image
from torchvision import transforms

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -39,7 +40,7 @@ async def log_duration(request: Request, call_next):
num_gpus = torch.cuda.device_count()
txt2img_pipes = [
AutoPipelineForText2Image.from_pretrained(
model_name, torch_dtype=torch.float16, variant="fp16", safety_checker=None
model_name, torch_dtype=torch.float16, variant="fp16"
).to(f"cuda:{i}")
for i in range(num_gpus)
]
Expand Down Expand Up @@ -140,15 +141,16 @@ async def img2img(input_data: Img2ImgInput):
# Ensure to use the selected GPU for computations
pipe = img2img_pipes[gpu_id]

init_image = Image.open(BytesIO(await input_data.image.file))

from torchvision import transforms
init_image = Image.open(BytesIO(await input_data.image.read())).convert(
"RGB"
)

# Convert the PIL image to a PyTorch tensor
init_image = transforms.ToTensor()(init_image).unsqueeze(0)
init_image = transforms.ToTensor()(init_image).unsqueeze(0).to(device)

# Cast the init_image tensor to float16
init_image = init_image.to(dtype=torch.float16)
# Cast the init_image tensor to float16 if the model is using float16
if pipe.device.type == "cuda" and pipe.device.index is not None:
init_image = init_image.to(dtype=torch.float16)

image = pipe(
prompt=input_data.prompt,
Expand Down

0 comments on commit f2d6adc

Please sign in to comment.