dlaima commited on
Commit
8504f2c
·
verified ·
1 Parent(s): 1381703

Update image_analyzer.py

Browse files
Files changed (1) hide show
  1. image_analyzer.py +42 -1
image_analyzer.py CHANGED
@@ -29,7 +29,48 @@ class ImageAnalysisTool(Tool):
29
  "Content-Type": "application/json"
30
  }
31
 
32
- def forward(self, image_path: str, question: str) -> str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
 
 
29
  "Content-Type": "application/json"
30
  }
31
 
32
+ def forward(self, image_path: str, question: str) -> str:
33
+ try:
34
+ with open(image_path, "rb") as img_file:
35
+ image_bytes = img_file.read()
36
+
37
+ # Encode image to base64 string
38
+ img_b64 = base64.b64encode(image_bytes).decode("utf-8")
39
+
40
+ # Prepare payload for the API
41
+ payload = {
42
+ "inputs": img_b64
43
+ }
44
+
45
+ response = requests.post(
46
+ self.api_url,
47
+ headers=self.headers,
48
+ json=payload,
49
+ timeout=60
50
+ )
51
+
52
+ if response.status_code == 200:
53
+ result = response.json()
54
+
55
+ caption = None
56
+ # Try common keys for caption output
57
+ if isinstance(result, dict):
58
+ caption = result.get("generated_text") or result.get("caption") or result.get("text")
59
+ elif isinstance(result, list) and len(result) > 0 and isinstance(result[0], dict):
60
+ caption = result[0].get("generated_text") or result[0].get("caption") or result[0].get("text")
61
+
62
+ if not caption:
63
+ return "Error: No caption found in model response."
64
+
65
+ # Combine caption with the question to form a simple answer
66
+ answer = f"Caption: {caption}\nAnswer to question '{question}': {caption}"
67
+ return answer.strip()
68
+
69
+ else:
70
+ return f"Error analyzing image: {response.status_code} {response.text}"
71
+
72
+ except Exception as e:
73
+ return f"Error analyzing image: {e}"
74
 
75
 
76