test data
Browse filesFiles changed (2)
- handler.py+56-55
- requirements.txt+2-1
7 unmodified lines8910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182837 unmodified linesclass EndpointHandler:def __init__(self):self.processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")device = 'gpu' if torch.cuda.is_available() else 'cpu'model = LlavaNextForConditionalGeneration.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf",torch_dtype=torch.float32 if device == 'cpu' else torch.float16,low_cpu_mem_usage=True)model.to(device)self.model = modelself.device = devicedef __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:"""data args:inputs (:obj: `dict`)Return:A :obj:`list` | `dict`: will be serialized and returned"""return data# get inputsinputs = data.get("inputs")if not inputs:return f"Inputs not in payload got {data}"# get additional date field0prompt = inputs.get("prompt")image_url = inputs.get("image")if image_url is None:return "You need to upload an image URL for LLaVA to work."if prompt is None:prompt = "Can you describe this picture focusing on specifics visual artifacts and ambiance (objects, colors, person, athmosphere..). Please stay concise only output keywords and concepts detected."if not self.model:return "Model was not initialized"if not self.processor:return "Processor was not initialized"# Create a temporary directorywith TemporaryDirectory() as tmpdirname:# Download the imageresponse = requests.get(image_url)if response.status_code != 200:return "Failed to download the image."# Define the path for the downloaded imageimage_path = f"{tmpdirname}/image.jpg"with open(image_path, "wb") as f:f.write(response.content)# Open the downloaded imagewith Image.open(image_path).convert("RGB") as image:prompt = f"[INST] <image>\n{prompt} [/INST]"inputs = self.processor(prompt, image, return_tensors="pt").to(self.device)output = self.model.generate(**inputs, max_new_tokens=100)if not output:return 'Model failed to generate'clean = self.processor.decode(output[0], skip_special_tokens=True)return clean7 unmodified lines891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283847 unmodified linesclass EndpointHandler:def __init__(self):pass# self.processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")# device = 'gpu' if torch.cuda.is_available() else 'cpu'# model = LlavaNextForConditionalGeneration.from_pretrained(# "llava-hf/llava-v1.6-mistral-7b-hf",# torch_dtype=torch.float32 if device == 'cpu' else torch.float16,# low_cpu_mem_usage=True# )# model.to(device)# self.model = model# self.device = devicedef __call__(self, data):return data# def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:# """# data args:# inputs (:obj: `dict`)# Return:# A :obj:`list` | `dict`: will be serialized and returned# """# # get inputs# inputs = data.get("inputs")# if not inputs:# return f"Inputs not in payload got {data}"# # get additional date field0# prompt = inputs.get("prompt")# image_url = inputs.get("image")# if image_url is None:# return "You need to upload an image URL for LLaVA to work."# if prompt is None:# prompt = "Can you describe this picture focusing on specifics visual artifacts and ambiance (objects, colors, person, athmosphere..). Please stay concise only output keywords and concepts detected."# if not self.model:# return "Model was not initialized"# if not self.processor:# return "Processor was not initialized"# # Create a temporary directory# with TemporaryDirectory() as tmpdirname:# # Download the image# response = requests.get(image_url)# if response.status_code != 200:# return "Failed to download the image."# # Define the path for the downloaded image# image_path = f"{tmpdirname}/image.jpg"# with open(image_path, "wb") as f:# f.write(response.content)# # Open the downloaded image# with Image.open(image_path).convert("RGB") as image:# prompt = f"[INST] <image>\n{prompt} [/INST]"# inputs = self.processor(prompt, image, return_tensors="pt").to(self.device)# output = self.model.generate(**inputs, max_new_tokens=100)# if not output:# return 'Model failed to generate'# clean = self.processor.decode(output[0], skip_special_tokens=True)# return clean
2 unmodified lines34562 unmodified linesspacespillowacceleraterequests2 unmodified lines345672 unmodified linesspacespillowacceleraterequestsholidays
handler.py+56-55
requirements.txt+2-1