dlaima commited on
Commit
88701df
·
verified ·
1 Parent(s): 5331296

Update image_analyzer.py

Browse files
Files changed (1) hide show
  1. image_analyzer.py +1 -44
image_analyzer.py CHANGED
@@ -29,50 +29,7 @@ class ImageAnalysisTool(Tool):
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 JSON payload - the exact structure depends on the model capabilities
41
- # Here we send just the image for captioning
42
- payload = {
43
- "inputs": img_b64
44
- }
45
-
46
- response = requests.post(
47
- self.api_url,
48
- headers=self.headers,
49
- json=payload,
50
- timeout=60
51
- )
52
-
53
- if response.status_code == 200:
54
- result = response.json()
55
-
56
- caption = None
57
- # Try common keys for caption output
58
- if isinstance(result, dict):
59
- caption = result.get("generated_text") or result.get("caption") or result.get("text")
60
- elif isinstance(result, list) and len(result) > 0 and isinstance(result[0], dict):
61
- caption = result[0].get("generated_text") or result[0].get("caption") or result[0].get("text")
62
-
63
- if not caption:
64
- return "Error: No caption found in model response."
65
-
66
- # Combine caption with the question to form a simple answer
67
- answer = f"Caption: {caption}\nAnswer to question '{question}': {caption}"
68
- return answer.strip()
69
-
70
- else:
71
- return f"Error analyzing image: {response.status_code} {response.text}"
72
-
73
- except Exception as e:
74
- return f"Error analyzing image: {e}"
75
-
76
 
77
 
78
 
 
29
  "Content-Type": "application/json"
30
  }
31
 
32
+ def forward(self, image_path: str, question: str) -> str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35