rroset commited on
Commit
7020761
1 Parent(s): 5249c43

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +23 -8
handler.py CHANGED
@@ -34,6 +34,13 @@ class EndpointHandler():
34
  if not image_url or not prompt:
35
  return {"error": "Both 'url' and 'prompt' must be provided in 'inputs'."}
36
 
 
 
 
 
 
 
 
37
  try:
38
  response = requests.get(image_url, stream=True)
39
  image = Image.open(response.raw)
@@ -46,15 +53,23 @@ class EndpointHandler():
46
  image = Image.open(buffer)
47
 
48
  except Exception as e:
49
- return {"error": str(e)}
 
 
50
 
51
- inputs = self.processor(prompt, image, return_tensors="pt").to("cuda")
52
- output = self.model.generate(**inputs, max_new_tokens=100)
53
- result = self.processor.decode(output[0], skip_special_tokens=True)
54
-
55
- scores = self.extract_scores(result)
56
- sorted_scores = sorted(scores.items(), key=lambda item: item[1], reverse=True)
57
- return sorted_scores
 
 
 
 
 
 
58
 
59
  def extract_scores(self, response):
60
  scores = {}
 
34
  if not image_url or not prompt:
35
  return {"error": "Both 'url' and 'prompt' must be provided in 'inputs'."}
36
 
37
+ # Depuració
38
+ debug_info = {
39
+ "stage": "processing input",
40
+ "image_url": image_url,
41
+ "prompt": prompt
42
+ }
43
+
44
  try:
45
  response = requests.get(image_url, stream=True)
46
  image = Image.open(response.raw)
 
53
  image = Image.open(buffer)
54
 
55
  except Exception as e:
56
+ debug_info["stage"] = "loading image"
57
+ debug_info["error"] = str(e)
58
+ return debug_info
59
 
60
+ try:
61
+ inputs = self.processor(prompt, image, return_tensors="pt").to("cuda")
62
+ output = self.model.generate(**inputs, max_new_tokens=100)
63
+ result = self.processor.decode(output[0], skip_special_tokens=True)
64
+
65
+ scores = self.extract_scores(result)
66
+ sorted_scores = sorted(scores.items(), key=lambda item: item[1], reverse=True)
67
+ return sorted_scores
68
+
69
+ except Exception as e:
70
+ debug_info["stage"] = "processing model"
71
+ debug_info["error"] = str(e)
72
+ return debug_info
73
 
74
  def extract_scores(self, response):
75
  scores = {}