naman1102 commited on
Commit
9017277
·
1 Parent(s): 13d93fc
Files changed (2) hide show
  1. app.py +3 -0
  2. tools.py +80 -95
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 is a string
47
- Returns: "OCR text + brief caption or an error message"
48
-
49
  """
50
- print(f"DEBUG: image_tool called with task_id: {task_id}")
51
-
52
- local_img = None # Initialize the variable
53
-
54
- # Try to download image file with different extensions
55
  for ext in ("png", "jpg", "jpeg"):
56
- print(f"DEBUG: Trying to download {task_id}.{ext}")
57
- candidate = _download_file_for_task(task_id, ext)
58
- if candidate:
59
- local_img = candidate
60
- print(f"DEBUG: Successfully downloaded image: {local_img}")
61
  break
62
- else:
63
- print(f"DEBUG: Failed to download {task_id}.{ext}")
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
- # 2) Read raw bytes
71
  try:
72
- print(f"DEBUG: Reading image file: {local_img}")
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
- error_msg = f"Error reading image file: {e}"
78
- print(f"DEBUG: {error_msg}")
79
- return error_msg
80
 
81
- # 3) Prepare HF Inference headers
82
  hf_token = os.getenv("HF_TOKEN")
83
  if not hf_token:
84
- error_msg = "Error: HF_TOKEN not set in environment."
85
- print(f"DEBUG: {error_msg}")
86
- return error_msg
87
 
 
 
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:
@@ -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=2).load()
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
- # Truncate content if too long
259
- content = doc.page_content[:2000] if len(doc.page_content) > 2000 else doc.page_content
260
- result += f"\n\nDocument{counter}: {title}\n{content}"
 
 
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=2).load()
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
- # Truncate content if too long
314
- content = doc.page_content[:2000] if len(doc.page_content) > 2000 else doc.page_content
315
- result += f"\n\nDocument{counter}: {title}\n{content}"
 
 
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