eBoreal commited on
Commit
341d1bf
·
1 Parent(s): ca246a4
Files changed (2) hide show
  1. handler.py +56 -55
  2. requirements.txt +2 -1
handler.py CHANGED
@@ -8,76 +8,77 @@ import requests
8
 
9
  class EndpointHandler:
10
  def __init__(self):
11
- self.processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
 
12
 
13
 
14
- device = 'gpu' if torch.cuda.is_available() else 'cpu'
15
-
16
- model = LlavaNextForConditionalGeneration.from_pretrained(
17
- "llava-hf/llava-v1.6-mistral-7b-hf",
18
- torch_dtype=torch.float32 if device == 'cpu' else torch.float16,
19
- low_cpu_mem_usage=True
20
- )
21
- model.to(device)
22
-
23
- self.model = model
24
- self.device = device
25
-
26
- def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
27
- """
28
- data args:
29
- inputs (:obj: `dict`)
30
- Return:
31
- A :obj:`list` | `dict`: will be serialized and returned
32
- """
33
 
 
 
 
 
 
 
 
 
 
 
 
34
  return data
35
-
36
- # get inputs
37
- inputs = data.get("inputs")
38
 
39
- if not inputs:
40
- return f"Inputs not in payload got {data}"
 
 
 
 
 
 
 
 
 
 
41
 
42
- # get additional date field0
43
- prompt = inputs.get("prompt")
44
- image_url = inputs.get("image")
45
 
46
- if image_url is None:
47
- return "You need to upload an image URL for LLaVA to work."
48
 
49
- if prompt is None:
50
- 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."
51
 
52
- if not self.model:
53
- return "Model was not initialized"
54
 
55
- if not self.processor:
56
- return "Processor was not initialized"
57
 
58
- # Create a temporary directory
59
- with TemporaryDirectory() as tmpdirname:
60
- # Download the image
61
- response = requests.get(image_url)
62
- if response.status_code != 200:
63
- return "Failed to download the image."
64
 
65
- # Define the path for the downloaded image
66
- image_path = f"{tmpdirname}/image.jpg"
67
- with open(image_path, "wb") as f:
68
- f.write(response.content)
69
 
70
- # Open the downloaded image
71
- with Image.open(image_path).convert("RGB") as image:
72
- prompt = f"[INST] <image>\n{prompt} [/INST]"
73
 
74
- inputs = self.processor(prompt, image, return_tensors="pt").to(self.device)
75
 
76
- output = self.model.generate(**inputs, max_new_tokens=100)
77
 
78
- if not output:
79
- return 'Model failed to generate'
80
 
81
- clean = self.processor.decode(output[0], skip_special_tokens=True)
82
 
83
- return clean
 
8
 
9
  class EndpointHandler:
10
  def __init__(self):
11
+ pass
12
+ # self.processor = LlavaNextProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf")
13
 
14
 
15
+ # device = 'gpu' if torch.cuda.is_available() else 'cpu'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # model = LlavaNextForConditionalGeneration.from_pretrained(
18
+ # "llava-hf/llava-v1.6-mistral-7b-hf",
19
+ # torch_dtype=torch.float32 if device == 'cpu' else torch.float16,
20
+ # low_cpu_mem_usage=True
21
+ # )
22
+ # model.to(device)
23
+
24
+ # self.model = model
25
+ # self.device = device
26
+
27
+ def __call__(self, data):
28
  return data
 
 
 
29
 
30
+ # def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
31
+ # """
32
+ # data args:
33
+ # inputs (:obj: `dict`)
34
+ # Return:
35
+ # A :obj:`list` | `dict`: will be serialized and returned
36
+ # """
37
+ # # get inputs
38
+ # inputs = data.get("inputs")
39
+
40
+ # if not inputs:
41
+ # return f"Inputs not in payload got {data}"
42
 
43
+ # # get additional date field0
44
+ # prompt = inputs.get("prompt")
45
+ # image_url = inputs.get("image")
46
 
47
+ # if image_url is None:
48
+ # return "You need to upload an image URL for LLaVA to work."
49
 
50
+ # if prompt is None:
51
+ # 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."
52
 
53
+ # if not self.model:
54
+ # return "Model was not initialized"
55
 
56
+ # if not self.processor:
57
+ # return "Processor was not initialized"
58
 
59
+ # # Create a temporary directory
60
+ # with TemporaryDirectory() as tmpdirname:
61
+ # # Download the image
62
+ # response = requests.get(image_url)
63
+ # if response.status_code != 200:
64
+ # return "Failed to download the image."
65
 
66
+ # # Define the path for the downloaded image
67
+ # image_path = f"{tmpdirname}/image.jpg"
68
+ # with open(image_path, "wb") as f:
69
+ # f.write(response.content)
70
 
71
+ # # Open the downloaded image
72
+ # with Image.open(image_path).convert("RGB") as image:
73
+ # prompt = f"[INST] <image>\n{prompt} [/INST]"
74
 
75
+ # inputs = self.processor(prompt, image, return_tensors="pt").to(self.device)
76
 
77
+ # output = self.model.generate(**inputs, max_new_tokens=100)
78
 
79
+ # if not output:
80
+ # return 'Model failed to generate'
81
 
82
+ # clean = self.processor.decode(output[0], skip_special_tokens=True)
83
 
84
+ # return clean
requirements.txt CHANGED
@@ -3,4 +3,5 @@ git+https://github.com/huggingface/transformers.git
3
  spaces
4
  pillow
5
  accelerate
6
- requests
 
 
3
  spaces
4
  pillow
5
  accelerate
6
+ requests
7
+ holidays