Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -88,67 +88,56 @@ def image_tool(task_id: str) -> str:
|
|
88 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
89 |
print("DEBUG: HF token found, proceeding with API calls")
|
90 |
|
91 |
-
#
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
if not caption:
|
142 |
-
caption = "(no caption generated)"
|
143 |
-
print(f"DEBUG: Generated caption: {caption}")
|
144 |
-
except Exception as e:
|
145 |
-
caption = f"Error during HF captioning: {e}"
|
146 |
-
print(f"DEBUG: Captioning failed: {e}")
|
147 |
-
|
148 |
-
# 6) Combine OCR + caption
|
149 |
-
combined = f"OCR text:\n{ocr_text}\n\nImage caption:\n{caption}"
|
150 |
-
print(f"DEBUG: Final result: {combined}")
|
151 |
-
return combined
|
152 |
|
153 |
@tool
|
154 |
def excel_tool(task_id: str) -> str:
|
|
|
88 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
89 |
print("DEBUG: HF token found, proceeding with API calls")
|
90 |
|
91 |
+
# Try different HF models for image analysis
|
92 |
+
models_to_try = [
|
93 |
+
"nlpconnect/vit-gpt2-image-captioning",
|
94 |
+
"Salesforce/blip-image-captioning-large",
|
95 |
+
"microsoft/git-base-coco",
|
96 |
+
"microsoft/git-large-coco"
|
97 |
+
]
|
98 |
+
|
99 |
+
result_text = ""
|
100 |
+
success = False
|
101 |
+
|
102 |
+
for model_name in models_to_try:
|
103 |
+
try:
|
104 |
+
print(f"DEBUG: Trying model: {model_name}")
|
105 |
+
resp = requests.post(
|
106 |
+
f"https://api-inference.huggingface.co/models/{model_name}",
|
107 |
+
headers=headers,
|
108 |
+
files={"file": image_bytes},
|
109 |
+
timeout=30
|
110 |
+
)
|
111 |
+
print(f"DEBUG: {model_name} response status: {resp.status_code}")
|
112 |
+
|
113 |
+
if resp.status_code == 200:
|
114 |
+
resp_json = resp.json()
|
115 |
+
print(f"DEBUG: {model_name} response: {resp_json}")
|
116 |
+
|
117 |
+
# Handle different response formats
|
118 |
+
if isinstance(resp_json, list) and len(resp_json) > 0:
|
119 |
+
result_text = resp_json[0].get("generated_text", "").strip()
|
120 |
+
elif isinstance(resp_json, dict):
|
121 |
+
result_text = resp_json.get("generated_text", "").strip()
|
122 |
+
|
123 |
+
if result_text:
|
124 |
+
print(f"DEBUG: Successfully got result from {model_name}: {result_text}")
|
125 |
+
success = True
|
126 |
+
break
|
127 |
+
else:
|
128 |
+
print(f"DEBUG: {model_name} failed with status {resp.status_code}")
|
129 |
+
|
130 |
+
except Exception as e:
|
131 |
+
print(f"DEBUG: {model_name} failed with error: {e}")
|
132 |
+
continue
|
133 |
+
|
134 |
+
if not success or not result_text:
|
135 |
+
result_text = "Unable to analyze image - all HuggingFace models failed or returned empty results"
|
136 |
+
|
137 |
+
# Format the result
|
138 |
+
final_result = f"Image Analysis Result:\n{result_text}"
|
139 |
+
print(f"DEBUG: Final result: {final_result}")
|
140 |
+
return final_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
@tool
|
143 |
def excel_tool(task_id: str) -> str:
|