Drazcat-AI commited on
Commit
bb612f5
·
verified ·
1 Parent(s): a9d8b81

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +54 -54
handler.py CHANGED
@@ -1,55 +1,55 @@
1
- import requests
2
-
3
- import torch
4
- from PIL import Image
5
- from transformers import AutoProcessor, AutoModelForCausalLM
6
-
7
- class EndpointHandler():
8
- def __init__(self, path=""):
9
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
11
-
12
- model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
13
- processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
14
-
15
-
16
- def predict_image(self, url, prompt):
17
- url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
18
- image = Image.open(requests.get(url, stream=True).raw)
19
-
20
- inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
21
-
22
- generated_ids = model.generate(
23
- input_ids=inputs["input_ids"],
24
- pixel_values=inputs["pixel_values"],
25
- max_new_tokens=4096,
26
- num_beams=3,
27
- do_sample=False
28
- )
29
- generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
30
-
31
- parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
32
- return parsed_answer
33
-
34
-
35
- def __call__(self, event):
36
- if "url" not in event:
37
- return {
38
- "statusCode": 400,
39
- "body": json.dumps("Error: Please provide an 'url' parameter."),
40
- }
41
-
42
- if "prompt" not in event:
43
- return {
44
- "statusCode": 400,
45
- "body": json.dumps("Error: Please provide an 'prompt' parameter."),
46
- }
47
-
48
- url = event["url"]
49
- prompt = event["prompt"]
50
- parsed_answer = self.predict_image(self, url, prompt)
51
-
52
- return {
53
- "statusCode": 200,
54
- "body": json.dumps(parsed_answer),
55
  }
 
1
+ import requests
2
+ import json
3
+ import torch
4
+ from PIL import Image
5
+ from transformers import AutoProcessor, AutoModelForCausalLM
6
+
7
+ class EndpointHandler():
8
+ def __init__(self, path=""):
9
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
11
+
12
+ model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
13
+ processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
14
+
15
+
16
+ def predict_image(self, url, prompt):
17
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
18
+ image = Image.open(requests.get(url, stream=True).raw)
19
+
20
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
21
+
22
+ generated_ids = model.generate(
23
+ input_ids=inputs["input_ids"],
24
+ pixel_values=inputs["pixel_values"],
25
+ max_new_tokens=4096,
26
+ num_beams=3,
27
+ do_sample=False
28
+ )
29
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
30
+
31
+ parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
32
+ return parsed_answer
33
+
34
+
35
+ def __call__(self, event):
36
+ if "url" not in event:
37
+ return {
38
+ "statusCode": 400,
39
+ "body": json.dumps("Error: Please provide an 'url' parameter."),
40
+ }
41
+
42
+ if "prompt" not in event:
43
+ return {
44
+ "statusCode": 400,
45
+ "body": json.dumps("Error: Please provide an 'prompt' parameter."),
46
+ }
47
+
48
+ url = event["url"]
49
+ prompt = event["prompt"]
50
+ parsed_answer = self.predict_image(self, url, prompt)
51
+
52
+ return {
53
+ "statusCode": 200,
54
+ "body": json.dumps(parsed_answer),
55
  }