Hey yall, I was trying to run `bentoml deploy .`to...
# ask-for-help
n
Hey yall, I was trying to run `bentoml deploy .`to deploy to bento cloud, but it just keeps running forever without any logs. Here is my current
bentofile.yaml
Copy code
service: 'service:InferenceService'
labels:
  owner: minhpham
  project: background-replacer
include:
  - '*.py'
  - 'models/*.py'
python:
  packages:
    - torch
    - torchvision
    - wheel
  requirements_txt: './requirements.txt'
and this is my
service.py
script:
Copy code
import bentoml
from upscaler_service import UpscalerService
from captioner_service import CaptionerService
from segmenter_service import SegmenterService
from depth_estimator_service import DepthEstimatorService
from sdxl_service import SDXLService
from PIL import Image, ImageFilter
from typing import List
from tqdm import tqdm
import torch
import numpy as np
from scipy.ndimage import binary_dilation
import os
import datetime

@bentoml.service(resources={"gpu": 2, "memory": "2Gi"}, traffic={"timeout": 600},)
class InferenceService:
		developer_mode = os.getenv('DEV_MODE', False)

		POSITIVE_PROMPT_SUFFIX = "commercial product photography, 24mm lens f/8"
		NEGATIVE_PROMPT_SUFFIX = "cartoon, drawing, anime, semi-realistic, illustration, painting, art, text, greyscale, (black and white), lens flare, watermark, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, floating, levitating"

		MEGAPIXELS = 1.0
  
		def __init__(self):
				self.upscaler_service = bentoml.depends(UpscalerService)
				self.captioner_service = bentoml.depends(CaptionerService)	
				self.segmenter_service = bentoml.depends(SegmenterService)
				self.depth_estimator_service = bentoml.depends(DepthEstimatorService)
				self.sdxl_service = bentoml.depends(SDXLService)

		@bentoml.api
		async def inference(self, image: Image.Image, positive_prompt, negative_prompt, options) -> List[Image.Image]:
				torch.cuda.empty_cache()
    
				print('Running inference...')	

				pbar = tqdm(total=7)
				caption = await self.captioner_service.to_async.derive_caption(image)
				pbar.update(1)
    
				torch.cuda.empty_cache()
    
				print('Original image size: ', image.size)
				resized_image = self.upscaler_service.ensure_resolution(image, mega_pixels=self.MEGAPIXELS)
				pbar.update(1)
    
				torch.cuda.empty_cache()
				[cropped, crop_mask]= self.segmenter_service.segment(resized_image)
				pbar.update(1)
    
				torch.cuda.empty_cache()
				depth_map = self.depth_estimator_service.get_depth_map(resized_image)
				pbar.update(1)

				torch.cuda.empty_cache()
    
				print("Feathering the depth map...")
				crop_mask_np = np.array(crop_mask.convert('L'))

				# Convert to binary and dilate (grow) the edges
				# adjust threshold as needed
				crop_mask_binary = crop_mask_np > options.get(
						'depth_map_feather_threshold')
				# adjust iterations as needed
				dilated_mask = binary_dilation(
						crop_mask_binary, iterations=options.get('depth_map_dilation_iterations'))

				# Convert back to PIL Image
				dilated_mask = Image.fromarray((dilated_mask * 255).astype(np.uint8))

				# Apply Gaussian blur and normalize
				dilated_mask_blurred = dilated_mask.filter(
						ImageFilter.GaussianBlur(radius=options.get('depth_map_blur_radius')))
				dilated_mask_blurred_np = np.array(dilated_mask_blurred) / 255.0

				# Normalize depth map, apply blurred, dilated mask, and scale back
				depth_map_np = np.array(depth_map.convert('L')) / 255.0
				masked_depth_map_np = depth_map_np * dilated_mask_blurred_np
				masked_depth_map_np = (masked_depth_map_np * 255).astype(np.uint8)

				# Convert back to PIL Image
				masked_depth_map = Image.fromarray(masked_depth_map_np).convert('RGB')

				pbar.update(1)

				final_positive_prompt = f"{caption}, {positive_prompt}, {self.POSITIVE_PROMPT_SUFFIX}"
				final_negative_prompt = f"{negative_prompt}, {self.NEGATIVE_PROMPT_SUFFIX}"

				print("Final positive prompt:", final_positive_prompt)
				print("Final negative prompt:", final_negative_prompt)

				print("Generating...")
				torch.cuda.empty_cache()
				generated_images = self.sdxl_service.run_pipeline(
						num_inference_steps=options.get('num_inference_steps'),
						num_images_per_prompt=int(options.get('num_images_per_prompt')),
						controlnet_conditioning_scale=options.get('controlnet_conditioning_scale'),
						guidance_scale=options.get('guidance_scale'),
						num_images_to_generate=options.get('num_images_to_generate'),
						positive_prompt=final_positive_prompt,
						negative_prompt=final_negative_prompt,
						image=[masked_depth_map],
						seed=options.get('seed')
				)
				pbar.update(1)

				torch.cuda.empty_cache()

				print("Compositing...")

				composited_images = [
						Image.alpha_composite(
								generated_image.convert('RGBA'),
								self.upscaler_service.crop_centered(cropped, generated_image.size)
						) for generated_image in generated_images
				]
				pbar.update(1)
				pbar.close()
				if not os.path.exists("outputs"):
						os.makedirs("outputs")
				for img in composited_images:
						timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f')
						img.save(f"outputs/{timestamp}.png")
				print("Done!")

				if self.developer_mode:
						pre_processing_images = [
								[resized_image, "Resized"],
								[crop_mask, "Crop mask"],
								[cropped, "Cropped"],
								[depth_map, "Depth map"],
								[dilated_mask, "Dilated mask"],
								[dilated_mask_blurred, "Dilated mask blurred"],
								[masked_depth_map, "Masked depth map"]
						]
						return [
								composited_images,
								generated_images,
								pre_processing_images,
								caption,
						]
				else:
						return [composited_images, None, None, None]
j
Hello, can you share which organization are you in? As well as which stage was it stuck on?
n
my org is
minhpham
I haven't set up the stage, so not sure how it attributes to the error
j
can you show the output of the logs
n
there was no output. the terminal just keeps hanging...
CleanShot 2024-05-07 at 17.38.35.png
Seems like the service has been deployed, but the status of the deployment is failed. Not sure what cause the error. Here is the logs from the service:
Copy code
Error: [31m[serve] `serve` failed: Failed loading Bento from directory /home/bentoml/bento: Failed to import module "service": libgthread-2.0.so.0: cannot open shared object file: No such file or directory[0m
j
do you manage do run the service locally?
n
I'm working on Mac M1, which don't have CUDA enabled. So I didn't try to run it