Spaces:
Sleeping
Sleeping
prompt
Browse files
app.py
CHANGED
@@ -23,6 +23,9 @@ SEARCH STRATEGY:
|
|
23 |
2. If Wikipedia still doesn't help, try arxiv_search_tool for academic/research topics
|
24 |
3. You can use multiple search attempts with different keywords to find better information
|
25 |
- Always evaluate if the search results are relevant and sufficient before proceeding to your final answer
|
|
|
|
|
|
|
26 |
|
27 |
RECURSION LIMIT HANDLING:
|
28 |
- You have a maximum of 8 steps to complete your task
|
|
|
23 |
2. If Wikipedia still doesn't help, try arxiv_search_tool for academic/research topics
|
24 |
3. You can use multiple search attempts with different keywords to find better information
|
25 |
- Always evaluate if the search results are relevant and sufficient before proceeding to your final answer
|
26 |
+
- IMPORTANT: When you see [END_OF_SEARCH] in tool results, this means the search is complete and you have all available information
|
27 |
+
- Do NOT perform additional searches after seeing [END_OF_SEARCH] - immediately proceed to analyze the provided information and give your final answer
|
28 |
+
- The [END_OF_SEARCH] marker indicates you should stop searching and work with what you have
|
29 |
|
30 |
RECURSION LIMIT HANDLING:
|
31 |
- You have a maximum of 8 steps to complete your task
|
tools.py
CHANGED
@@ -43,101 +43,68 @@ def _download_file_for_task(task_id: str, ext: str) -> str:
|
|
43 |
@tool
|
44 |
def image_tool(task_id: str) -> str:
|
45 |
"""
|
46 |
-
Expects: task_id
|
47 |
-
Returns:
|
48 |
-
|
49 |
"""
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
# Try to download image file with different extensions
|
55 |
for ext in ("png", "jpg", "jpeg"):
|
56 |
-
|
57 |
-
|
58 |
-
if candidate:
|
59 |
-
local_img = candidate
|
60 |
-
print(f"DEBUG: Successfully downloaded image: {local_img}")
|
61 |
break
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
if not local_img or not os.path.exists(local_img):
|
66 |
-
error_msg = f"Error: No image file found for task_id {task_id} (tried png, jpg, jpeg extensions)"
|
67 |
-
print(f"DEBUG: {error_msg}")
|
68 |
-
return error_msg
|
69 |
|
70 |
-
#
|
71 |
try:
|
72 |
-
|
73 |
-
with open(local_img, "rb") as f:
|
74 |
image_bytes = f.read()
|
75 |
-
print(f"DEBUG: Successfully read {len(image_bytes)} bytes from image")
|
76 |
except Exception as e:
|
77 |
-
|
78 |
-
print(f"DEBUG: {error_msg}")
|
79 |
-
return error_msg
|
80 |
|
81 |
-
#
|
82 |
hf_token = os.getenv("HF_TOKEN")
|
83 |
if not hf_token:
|
84 |
-
|
85 |
-
print(f"DEBUG: {error_msg}")
|
86 |
-
return error_msg
|
87 |
|
|
|
|
|
88 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
89 |
-
|
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 |
-
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:
|
@@ -227,7 +194,7 @@ def wikipedia_search_tool(wiki_query: str) -> str:
|
|
227 |
"""
|
228 |
print(f"DEBUG: reached wikipedia_search_tool with query: {wiki_query}")
|
229 |
try:
|
230 |
-
docs = WikipediaLoader(query=wiki_query, load_max_docs=
|
231 |
print(f"DEBUG: WikipediaLoader returned {len(docs)} documents")
|
232 |
|
233 |
result = ""
|
@@ -255,19 +222,28 @@ def wikipedia_search_tool(wiki_query: str) -> str:
|
|
255 |
|
256 |
print(f"DEBUG: Using Wikipedia title: {title}")
|
257 |
|
258 |
-
#
|
259 |
-
content = doc.page_content[:
|
260 |
-
|
|
|
|
|
261 |
counter += 1
|
|
|
|
|
|
|
|
|
262 |
|
263 |
if not result.strip():
|
264 |
-
return "No Wikipedia results found for the given query"
|
|
|
|
|
|
|
265 |
|
266 |
print(f"DEBUG: Final Wikipedia result length: {len(result)}")
|
267 |
return result
|
268 |
|
269 |
except Exception as e:
|
270 |
-
error_msg = f"Error during Wikipedia search: {str(e)}"
|
271 |
print(f"DEBUG: {error_msg}")
|
272 |
return error_msg
|
273 |
|
@@ -280,7 +256,7 @@ def arxiv_search_tool(arxiv_query: str) -> str:
|
|
280 |
"""
|
281 |
print(f"DEBUG: reached arxiv_search_tool with query: {arxiv_query}")
|
282 |
try:
|
283 |
-
docs = ArxivLoader(query=arxiv_query, load_max_docs=
|
284 |
print(f"DEBUG: ArxivLoader returned {len(docs)} documents")
|
285 |
|
286 |
result = ""
|
@@ -310,19 +286,28 @@ def arxiv_search_tool(arxiv_query: str) -> str:
|
|
310 |
|
311 |
print(f"DEBUG: Using title: {title}")
|
312 |
|
313 |
-
#
|
314 |
-
content = doc.page_content[:
|
315 |
-
|
|
|
|
|
316 |
counter += 1
|
|
|
|
|
|
|
|
|
317 |
|
318 |
if not result.strip():
|
319 |
-
return "No ArXiv results found for the given query"
|
|
|
|
|
|
|
320 |
|
321 |
print(f"DEBUG: Final ArXiv result length: {len(result)}")
|
322 |
return result
|
323 |
|
324 |
except Exception as e:
|
325 |
-
error_msg = f"Error during Arxiv search: {str(e)}"
|
326 |
print(f"DEBUG: {error_msg}")
|
327 |
return error_msg
|
328 |
|
|
|
43 |
@tool
|
44 |
def image_tool(task_id: str) -> str:
|
45 |
"""
|
46 |
+
Expects: task_id (str) — a valid image task ID.
|
47 |
+
Returns: image caption from Hugging Face API or error message.
|
|
|
48 |
"""
|
49 |
+
|
50 |
+
import requests, os
|
51 |
+
|
52 |
+
# Try downloading image with one of the allowed extensions
|
|
|
53 |
for ext in ("png", "jpg", "jpeg"):
|
54 |
+
file_path = _download_file_for_task(task_id, ext)
|
55 |
+
if file_path and os.path.exists(file_path):
|
|
|
|
|
|
|
56 |
break
|
57 |
+
else:
|
58 |
+
return f"Error: Image file for task_id '{task_id}' not found."
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Read the image bytes
|
61 |
try:
|
62 |
+
with open(file_path, "rb") as f:
|
|
|
63 |
image_bytes = f.read()
|
|
|
64 |
except Exception as e:
|
65 |
+
return f"Error reading image: {str(e)}"
|
|
|
|
|
66 |
|
67 |
+
# Load HF token
|
68 |
hf_token = os.getenv("HF_TOKEN")
|
69 |
if not hf_token:
|
70 |
+
return "Error: HF_TOKEN not set in environment."
|
|
|
|
|
71 |
|
72 |
+
# Use a single reliable model
|
73 |
+
model = "Salesforce/blip-image-captioning-base"
|
74 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
75 |
+
|
76 |
+
try:
|
77 |
+
response = requests.post(
|
78 |
+
f"https://api-inference.huggingface.co/models/{model}",
|
79 |
+
headers=headers,
|
80 |
+
files={"file": image_bytes},
|
81 |
+
timeout=30
|
82 |
+
)
|
83 |
+
except Exception as e:
|
84 |
+
return f"Error calling HuggingFace API: {e}"
|
85 |
+
|
86 |
+
# Parse response
|
87 |
+
if response.status_code != 200:
|
88 |
+
return f"Error from model ({model}): {response.status_code} - {response.text}"
|
89 |
+
|
90 |
+
try:
|
91 |
+
result = response.json()
|
92 |
+
if isinstance(result, list) and result:
|
93 |
+
caption = result[0].get("generated_text", "").strip()
|
94 |
+
elif isinstance(result, dict):
|
95 |
+
caption = result.get("generated_text", "").strip()
|
96 |
+
else:
|
97 |
+
caption = ""
|
98 |
+
except Exception as e:
|
99 |
+
return f"Error parsing response: {e}"
|
100 |
+
|
101 |
+
if not caption:
|
102 |
+
return "No caption generated by model."
|
103 |
+
|
104 |
+
return f"Image Caption:\n{caption}"
|
105 |
+
|
106 |
+
|
107 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
@tool
|
110 |
def excel_tool(task_id: str) -> str:
|
|
|
194 |
"""
|
195 |
print(f"DEBUG: reached wikipedia_search_tool with query: {wiki_query}")
|
196 |
try:
|
197 |
+
docs = WikipediaLoader(query=wiki_query, load_max_docs=3).load() # Reduced from 5 to 3
|
198 |
print(f"DEBUG: WikipediaLoader returned {len(docs)} documents")
|
199 |
|
200 |
result = ""
|
|
|
222 |
|
223 |
print(f"DEBUG: Using Wikipedia title: {title}")
|
224 |
|
225 |
+
# Trim content to key information only (reduced from 2000 to 800 characters)
|
226 |
+
content = doc.page_content[:800] if len(doc.page_content) > 800 else doc.page_content
|
227 |
+
|
228 |
+
# Add document but keep it concise
|
229 |
+
result += f"\n\nWikipedia Result {counter}: {title}\nSummary: {content}..."
|
230 |
counter += 1
|
231 |
+
|
232 |
+
# Stop after 2 documents to keep response manageable
|
233 |
+
if counter > 2:
|
234 |
+
break
|
235 |
|
236 |
if not result.strip():
|
237 |
+
return "No Wikipedia results found for the given query. [END_OF_SEARCH]"
|
238 |
+
|
239 |
+
# Add clear end marker
|
240 |
+
result += "\n\n[END_OF_SEARCH] - Wikipedia search complete. Use this information to answer the question."
|
241 |
|
242 |
print(f"DEBUG: Final Wikipedia result length: {len(result)}")
|
243 |
return result
|
244 |
|
245 |
except Exception as e:
|
246 |
+
error_msg = f"Error during Wikipedia search: {str(e)} [END_OF_SEARCH]"
|
247 |
print(f"DEBUG: {error_msg}")
|
248 |
return error_msg
|
249 |
|
|
|
256 |
"""
|
257 |
print(f"DEBUG: reached arxiv_search_tool with query: {arxiv_query}")
|
258 |
try:
|
259 |
+
docs = ArxivLoader(query=arxiv_query, load_max_docs=3).load() # Reduced from 5 to 3
|
260 |
print(f"DEBUG: ArxivLoader returned {len(docs)} documents")
|
261 |
|
262 |
result = ""
|
|
|
286 |
|
287 |
print(f"DEBUG: Using title: {title}")
|
288 |
|
289 |
+
# Trim content to key information only (reduced from 2000 to 800 characters)
|
290 |
+
content = doc.page_content[:800] if len(doc.page_content) > 800 else doc.page_content
|
291 |
+
|
292 |
+
# Add document but keep it concise
|
293 |
+
result += f"\n\nArXiv Result {counter}: {title}\nAbstract/Summary: {content}..."
|
294 |
counter += 1
|
295 |
+
|
296 |
+
# Stop after 2 documents to keep response manageable
|
297 |
+
if counter > 2:
|
298 |
+
break
|
299 |
|
300 |
if not result.strip():
|
301 |
+
return "No ArXiv results found for the given query. [END_OF_SEARCH]"
|
302 |
+
|
303 |
+
# Add clear end marker
|
304 |
+
result += "\n\n[END_OF_SEARCH] - ArXiv search complete. Use this information to answer the question."
|
305 |
|
306 |
print(f"DEBUG: Final ArXiv result length: {len(result)}")
|
307 |
return result
|
308 |
|
309 |
except Exception as e:
|
310 |
+
error_msg = f"Error during Arxiv search: {str(e)} [END_OF_SEARCH]"
|
311 |
print(f"DEBUG: {error_msg}")
|
312 |
return error_msg
|
313 |
|