Group17WPIMLDO24/Case-Study-1
0
1# external imports2from transformers import pipeline3from huggingface_hub import InferenceClient4 5# local imports6import config7 8class Blip_Image_Caption_Large:9 def __init__(self):10 pass11 12 def caption_image(self, image_path, use_local_caption):13 if use_local_caption:14 return self.caption_image_local_pipeline(image_path)15 else:16 return self.caption_image_api(image_path)17 18 def caption_image_local_pipeline(self, image_path):19 self.local_pipeline = pipeline("image-to-text", model=config.IMAGE_CAPTION_MODEL)20 result = self.local_pipeline(image_path)[0]['generated_text']21 return result22 23 def caption_image_api(self, image_path):24 client = InferenceClient(config.IMAGE_CAPTION_MODEL, token=config.HF_API_TOKEN)25 try:26 result = client.image_to_text(image_path).generated_text27 except Exception as e:28 result = f"Error: {e}"29 return result