Upload handler.py with huggingface_hub
Browse files12345678910111213141516171819202122232425262728293031import torchfrom PIL import Imagefrom transformers import AutoProcessor, AutoModelForCausalLMimport requests
class EndpointHandler: def __init__(self, path=""): model_id = "PULSE-ECG/PULSE-7B" self.processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True) self.model = AutoModelForCausalLM.from_pretrained( model_id, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True )
def __call__(self, data: dict) -> dict: image_url = data.get("image_url") text = data.get("text", "Interpret this ECG image.")
if not image_url: return {"error": "No image_url provided"}
image = Image.open(requests.get(image_url, stream=True).raw).convert("RGB") inputs = self.processor(text=text, images=image, return_tensors="pt").to(self.model.device)
with torch.no_grad(): outputs = self.model.generate(**inputs, max_new_tokens=512)
result = self.processor.decode(outputs[0], skip_special_tokens=True) return {"result": result}handler.pyAdded+31