Spaces:
Sleeping
Sleeping
deug
Browse files
app.py
CHANGED
|
@@ -157,6 +157,11 @@ def tool_node(state: AgentState) -> AgentState:
|
|
| 157 |
# We expect exactly one of these keys to be non‐empty:
|
| 158 |
# "web_search_query", "ocr_path", "excel_path"/"excel_sheet_name", "audio_path"
|
| 159 |
# Whichever is present, call the corresponding tool and return its result.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
if state.get("web_search_query"):
|
| 161 |
# print(f">>> tools_node dispatching web_search_tool with query: {state['web_search_query']!r}")
|
| 162 |
out = web_search_tool(state)
|
|
@@ -178,9 +183,7 @@ def tool_node(state: AgentState) -> AgentState:
|
|
| 178 |
out = audio_transcriber_tool(state)
|
| 179 |
return out
|
| 180 |
|
| 181 |
-
|
| 182 |
-
out = wikipedia_search_tool(state)
|
| 183 |
-
return out
|
| 184 |
|
| 185 |
# If we somehow reach here, no recognized tool key was set:
|
| 186 |
# print(">>> tools_node: no valid tool key found in state!")
|
|
|
|
| 157 |
# We expect exactly one of these keys to be non‐empty:
|
| 158 |
# "web_search_query", "ocr_path", "excel_path"/"excel_sheet_name", "audio_path"
|
| 159 |
# Whichever is present, call the corresponding tool and return its result.
|
| 160 |
+
|
| 161 |
+
if state.get("wiki_query"):
|
| 162 |
+
out = wikipedia_search_tool(state)
|
| 163 |
+
return out
|
| 164 |
+
|
| 165 |
if state.get("web_search_query"):
|
| 166 |
# print(f">>> tools_node dispatching web_search_tool with query: {state['web_search_query']!r}")
|
| 167 |
out = web_search_tool(state)
|
|
|
|
| 183 |
out = audio_transcriber_tool(state)
|
| 184 |
return out
|
| 185 |
|
| 186 |
+
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# If we somehow reach here, no recognized tool key was set:
|
| 189 |
# print(">>> tools_node: no valid tool key found in state!")
|
tools.py
CHANGED
|
@@ -19,6 +19,7 @@ def _download_file_for_task(task_id: str, ext: str) -> str:
|
|
| 19 |
Saves under ./hf_files/{task_id}.{ext}. Returns the local path if successful,
|
| 20 |
or an empty string if no file / download failed.
|
| 21 |
"""
|
|
|
|
| 22 |
os.makedirs("hf_files", exist_ok=True)
|
| 23 |
local_path = os.path.join("hf_files", f"{task_id}.{ext}")
|
| 24 |
url = f"{DEFAULT_API_URL}/files/{task_id}"
|
|
@@ -43,6 +44,7 @@ def web_search_tool(state: AgentState) -> AgentState:
|
|
| 43 |
Returns: {"web_search_query": None, "web_search_result": <string>}.
|
| 44 |
Retries up to 5 times on either a DuckDuckGo “202 Ratelimit” response or any exception (e.g. timeout).
|
| 45 |
"""
|
|
|
|
| 46 |
query = state.get("web_search_query", "")
|
| 47 |
if not query:
|
| 48 |
return {} # nothing to do
|
|
@@ -95,6 +97,7 @@ def ocr_image_tool(state: AgentState) -> AgentState:
|
|
| 95 |
{ "ocr_path": None, "ocr_result": "<OCRed text or error string>" }
|
| 96 |
Always attempts to download the file for the given path or task ID.
|
| 97 |
"""
|
|
|
|
| 98 |
path_or_id = state.get("ocr_path", "")
|
| 99 |
if not path_or_id:
|
| 100 |
return {}
|
|
@@ -139,6 +142,7 @@ def parse_excel_tool(state: AgentState) -> AgentState:
|
|
| 139 |
}
|
| 140 |
Always attempts to download the file for the given path or task ID.
|
| 141 |
"""
|
|
|
|
| 142 |
path_or_id = state.get("excel_path", "")
|
| 143 |
sheet = state.get("excel_sheet_name", "")
|
| 144 |
if not path_or_id:
|
|
@@ -200,15 +204,7 @@ def parse_excel_tool(state: AgentState) -> AgentState:
|
|
| 200 |
"excel_result": table_block
|
| 201 |
}
|
| 202 |
|
| 203 |
-
|
| 204 |
-
"""
|
| 205 |
-
Merges whatever partial state the tool wrapper returned (tool_out)
|
| 206 |
-
into the main state. That is, combine previous keys with new keys:
|
| 207 |
-
new_state = { **state, **tool_out }.
|
| 208 |
-
This node should be wired as its own graph node, not as a transition function.
|
| 209 |
-
"""
|
| 210 |
-
new_state = {**state, **tool_out}
|
| 211 |
-
return new_state
|
| 212 |
|
| 213 |
|
| 214 |
import os
|
|
@@ -235,6 +231,7 @@ def audio_transcriber_tool(state: AgentState) -> AgentState:
|
|
| 235 |
}
|
| 236 |
Always attempts to download the file for the given path or task ID.
|
| 237 |
"""
|
|
|
|
| 238 |
path_or_id = state.get("audio_path", "")
|
| 239 |
if not path_or_id:
|
| 240 |
return {}
|
|
@@ -335,3 +332,17 @@ def wikipedia_search_tool(state: AgentState) -> AgentState:
|
|
| 335 |
return {"wiki_query": None, "wiki_result": f"Wikipedia search error: {e}"}
|
| 336 |
except Exception as e:
|
| 337 |
return {"wiki_query": None, "wiki_result": f"Unexpected error in wikipedia_search_tool: {e}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Saves under ./hf_files/{task_id}.{ext}. Returns the local path if successful,
|
| 20 |
or an empty string if no file / download failed.
|
| 21 |
"""
|
| 22 |
+
print("reached _download_file_for_task")
|
| 23 |
os.makedirs("hf_files", exist_ok=True)
|
| 24 |
local_path = os.path.join("hf_files", f"{task_id}.{ext}")
|
| 25 |
url = f"{DEFAULT_API_URL}/files/{task_id}"
|
|
|
|
| 44 |
Returns: {"web_search_query": None, "web_search_result": <string>}.
|
| 45 |
Retries up to 5 times on either a DuckDuckGo “202 Ratelimit” response or any exception (e.g. timeout).
|
| 46 |
"""
|
| 47 |
+
print("reached web_search_tool")
|
| 48 |
query = state.get("web_search_query", "")
|
| 49 |
if not query:
|
| 50 |
return {} # nothing to do
|
|
|
|
| 97 |
{ "ocr_path": None, "ocr_result": "<OCRed text or error string>" }
|
| 98 |
Always attempts to download the file for the given path or task ID.
|
| 99 |
"""
|
| 100 |
+
print("reached ocr_image_tool")
|
| 101 |
path_or_id = state.get("ocr_path", "")
|
| 102 |
if not path_or_id:
|
| 103 |
return {}
|
|
|
|
| 142 |
}
|
| 143 |
Always attempts to download the file for the given path or task ID.
|
| 144 |
"""
|
| 145 |
+
print("reached parse_excel_tool")
|
| 146 |
path_or_id = state.get("excel_path", "")
|
| 147 |
sheet = state.get("excel_sheet_name", "")
|
| 148 |
if not path_or_id:
|
|
|
|
| 204 |
"excel_result": table_block
|
| 205 |
}
|
| 206 |
|
| 207 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
|
| 210 |
import os
|
|
|
|
| 231 |
}
|
| 232 |
Always attempts to download the file for the given path or task ID.
|
| 233 |
"""
|
| 234 |
+
print("reached audio_transcriber_tool")
|
| 235 |
path_or_id = state.get("audio_path", "")
|
| 236 |
if not path_or_id:
|
| 237 |
return {}
|
|
|
|
| 332 |
return {"wiki_query": None, "wiki_result": f"Wikipedia search error: {e}"}
|
| 333 |
except Exception as e:
|
| 334 |
return {"wiki_query": None, "wiki_result": f"Unexpected error in wikipedia_search_tool: {e}"}
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def run_tools(state: AgentState, tool_out: AgentState) -> AgentState:
|
| 341 |
+
"""
|
| 342 |
+
Merges whatever partial state the tool wrapper returned (tool_out)
|
| 343 |
+
into the main state. That is, combine previous keys with new keys:
|
| 344 |
+
new_state = { **state, **tool_out }.
|
| 345 |
+
This node should be wired as its own graph node, not as a transition function.
|
| 346 |
+
"""
|
| 347 |
+
new_state = {**state, **tool_out}
|
| 348 |
+
return new_state
|