Spaces:
Sleeping
Sleeping
Update my_model/captioner/image_captioning.py
Browse files
my_model/captioner/image_captioning.py
CHANGED
|
@@ -41,7 +41,7 @@ class ImageCaptioningModel:
|
|
| 41 |
torch_dtype=self.torch_dtype,
|
| 42 |
device_map=self.device_map
|
| 43 |
)
|
| 44 |
-
|
| 45 |
self.model = InstructBlipForConditionalGeneration.from_pretrained(self.model_path,
|
| 46 |
load_in_8bit=self.load_in_8bit,
|
| 47 |
load_in_4bit=self.load_in_4bit,
|
|
@@ -50,7 +50,9 @@ class ImageCaptioningModel:
|
|
| 50 |
device_map=self.device_map
|
| 51 |
)
|
| 52 |
|
|
|
|
| 53 |
|
|
|
|
| 54 |
def resize_image(self, image, max_image_size=None):
|
| 55 |
if max_image_size is None:
|
| 56 |
max_image_size = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
|
|
@@ -66,7 +68,8 @@ class ImageCaptioningModel:
|
|
| 66 |
|
| 67 |
|
| 68 |
def generate_caption(self, image_path):
|
| 69 |
-
|
|
|
|
| 70 |
if isinstance(image_path, str) or isinstance(image_path, io.IOBase):
|
| 71 |
# If it's a file path or file-like object, open it as a PIL Image
|
| 72 |
image = Image.open(image_path)
|
|
@@ -78,7 +81,8 @@ class ImageCaptioningModel:
|
|
| 78 |
inputs = self.processor(image, self.prompt, return_tensors="pt").to("cuda", self.torch_dtype)
|
| 79 |
outputs = self.model.generate(**inputs, min_length=self.min_length, max_new_tokens=self.max_new_tokens)
|
| 80 |
caption = self.processor.decode(outputs[0], skip_special_tokens=self.skip_secial_tokens).strip()
|
| 81 |
-
|
|
|
|
| 82 |
return caption
|
| 83 |
|
| 84 |
def generate_captions_for_multiple_images(self, image_paths):
|
|
@@ -88,12 +92,11 @@ class ImageCaptioningModel:
|
|
| 88 |
|
| 89 |
def get_caption(img):
|
| 90 |
captioner = ImageCaptioningModel()
|
|
|
|
| 91 |
captioner.load_model()
|
|
|
|
| 92 |
caption = captioner.generate_caption(img)
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
-
return caption
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
if __name__ == "__main__":
|
| 99 |
-
pass
|
|
|
|
| 41 |
torch_dtype=self.torch_dtype,
|
| 42 |
device_map=self.device_map
|
| 43 |
)
|
| 44 |
+
free_gpu_resources()
|
| 45 |
self.model = InstructBlipForConditionalGeneration.from_pretrained(self.model_path,
|
| 46 |
load_in_8bit=self.load_in_8bit,
|
| 47 |
load_in_4bit=self.load_in_4bit,
|
|
|
|
| 50 |
device_map=self.device_map
|
| 51 |
)
|
| 52 |
|
| 53 |
+
free_gpu_resources()
|
| 54 |
|
| 55 |
+
|
| 56 |
def resize_image(self, image, max_image_size=None):
|
| 57 |
if max_image_size is None:
|
| 58 |
max_image_size = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
def generate_caption(self, image_path):
|
| 71 |
+
free_gpu_resources()
|
| 72 |
+
free_gpu_resources()
|
| 73 |
if isinstance(image_path, str) or isinstance(image_path, io.IOBase):
|
| 74 |
# If it's a file path or file-like object, open it as a PIL Image
|
| 75 |
image = Image.open(image_path)
|
|
|
|
| 81 |
inputs = self.processor(image, self.prompt, return_tensors="pt").to("cuda", self.torch_dtype)
|
| 82 |
outputs = self.model.generate(**inputs, min_length=self.min_length, max_new_tokens=self.max_new_tokens)
|
| 83 |
caption = self.processor.decode(outputs[0], skip_special_tokens=self.skip_secial_tokens).strip()
|
| 84 |
+
free_gpu_resources()
|
| 85 |
+
free_gpu_resources()
|
| 86 |
return caption
|
| 87 |
|
| 88 |
def generate_captions_for_multiple_images(self, image_paths):
|
|
|
|
| 92 |
|
| 93 |
def get_caption(img):
|
| 94 |
captioner = ImageCaptioningModel()
|
| 95 |
+
free_gpu_resources()
|
| 96 |
captioner.load_model()
|
| 97 |
+
free_gpu_resources()
|
| 98 |
caption = captioner.generate_caption(img)
|
| 99 |
+
free_gpu_resources()
|
| 100 |
|
| 101 |
|
| 102 |
+
return caption
|
|
|
|
|
|
|
|
|
|
|
|