Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -8,7 +8,7 @@ import regex as re
|
|
| 8 |
import time
|
| 9 |
import os
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
-
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
|
|
@@ -38,20 +38,17 @@ def _download_file_for_task(task_id: str, ext: str) -> str:
|
|
| 38 |
# If we get here, either 404 or download error
|
| 39 |
return ""
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
"""
|
| 43 |
-
Expects:
|
| 44 |
-
• a local image path (e.g. "./hf_files/abc.png"), OR
|
| 45 |
-
• a Task ID (e.g. "abc123"), in which case we try downloading
|
| 46 |
-
GET {DEFAULT_API_URL}/files/{task_id} with .png/.jpg/.jpeg extensions.
|
| 47 |
-
|
| 48 |
Returns: "OCR text + brief caption or an error message"
|
| 49 |
|
| 50 |
"""
|
| 51 |
-
print("reached
|
| 52 |
# path_or_id = state.get("ocr_path", "")
|
| 53 |
for ext in ("png", "jpg", "jpeg"):
|
| 54 |
-
candidate = _download_file_for_task(
|
| 55 |
if candidate:
|
| 56 |
local_img = candidate
|
| 57 |
break
|
|
@@ -122,19 +119,18 @@ def ocr_image_tool(args: dict) -> str:
|
|
| 122 |
print("combined: ")
|
| 123 |
return combined
|
| 124 |
|
| 125 |
-
|
| 126 |
-
def
|
| 127 |
"""
|
| 128 |
Downloads <task_id>.xlsx (if any) and returns a stringified list of
|
| 129 |
records from the specified sheet. No fallback to user-supplied tables.
|
| 130 |
-
Expected keys in `
|
| 131 |
• task_id – required (used to download the file)
|
| 132 |
-
|
| 133 |
returns: stringified list of records from the specified sheet
|
| 134 |
"""
|
| 135 |
-
print("reached
|
| 136 |
-
|
| 137 |
-
sheet = args.get("excel_sheet_name", "")
|
| 138 |
|
| 139 |
local_xlsx = _download_file_for_task(task_id, "xlsx")
|
| 140 |
if not local_xlsx or not os.path.exists(local_xlsx):
|
|
@@ -153,27 +149,22 @@ def parse_excel_tool(args: dict) -> str:
|
|
| 153 |
|
| 154 |
|
| 155 |
import openai
|
| 156 |
-
|
| 157 |
-
def audio_transcriber_tool(
|
| 158 |
"""
|
| 159 |
LangGraph tool for transcribing audio via OpenAI's Whisper API.
|
| 160 |
-
Expects:
|
| 161 |
-
• A local file path (e.g. "./hf_files/abc.mp3"), OR
|
| 162 |
-
• A Task ID (e.g. "abc123"), in which case we try downloading
|
| 163 |
-
GET {DEFAULT_API_URL}/files/{task_id} with .mp3, .wav, .m4a extensions.
|
| 164 |
Returns:
|
| 165 |
"<text or error message>"
|
| 166 |
Always attempts to download the file for the given path or task ID.
|
| 167 |
"""
|
| 168 |
print("reached audio_transcriber_tool")
|
| 169 |
-
|
| 170 |
-
# if not path_or_id:
|
| 171 |
-
# return {}
|
| 172 |
|
| 173 |
# Always attempt to download the file, regardless of local existence
|
| 174 |
local_audio = ""
|
| 175 |
for ext in ("mp3", "wav", "m4a"):
|
| 176 |
-
candidate = _download_file_for_task(
|
| 177 |
if candidate:
|
| 178 |
local_audio = candidate
|
| 179 |
break
|
|
@@ -205,16 +196,17 @@ def audio_transcriber_tool(args: dict) -> str:
|
|
| 205 |
import re
|
| 206 |
import requests
|
| 207 |
|
| 208 |
-
|
|
|
|
| 209 |
"""
|
| 210 |
LangGraph wrapper for searching Wikipedia.
|
| 211 |
-
Expects:
|
| 212 |
Returns: text summary of first matching page or an error message>"
|
| 213 |
|
| 214 |
If no valid wiki_query is provided, returns {}.
|
| 215 |
"""
|
| 216 |
print("reached wikipedia search tool")
|
| 217 |
-
query =
|
| 218 |
if not query:
|
| 219 |
return {}
|
| 220 |
|
|
@@ -268,9 +260,10 @@ from langchain_openai import ChatOpenAI
|
|
| 268 |
from langchain.schema import SystemMessage, HumanMessage
|
| 269 |
LLM = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.2)
|
| 270 |
|
| 271 |
-
|
|
|
|
| 272 |
"""
|
| 273 |
-
Either
|
| 274 |
Reads the code (max 400 lines / 10 kB) and asks the LLM for:
|
| 275 |
• plain-language summary
|
| 276 |
• list of key functions/classes
|
|
@@ -279,15 +272,15 @@ def analyze_code_tool(args: dict) -> str:
|
|
| 279 |
"""
|
| 280 |
print("reached analyze_code_tool")
|
| 281 |
code_txt = ""
|
| 282 |
-
if
|
| 283 |
-
code_txt =
|
| 284 |
-
|
| 285 |
-
path = _download_file_for_task(
|
| 286 |
if not path:
|
| 287 |
return "Error: .py file not found for this task."
|
| 288 |
code_txt = Path(path).read_text(encoding="utf-8", errors="ignore")
|
| 289 |
-
else:
|
| 290 |
-
|
| 291 |
|
| 292 |
# Truncate for safety
|
| 293 |
lines = code_txt.splitlines()[:400]
|
|
@@ -300,6 +293,7 @@ def analyze_code_tool(args: dict) -> str:
|
|
| 300 |
"Summarise what it does, list key functions/classes, "
|
| 301 |
"and point out any obvious bugs, performance issues or style problems.\n\n"
|
| 302 |
f"```python\n{code_sample}\n```"
|
|
|
|
| 303 |
))
|
| 304 |
]
|
| 305 |
return LLM.invoke(prompt).content.strip()
|
|
|
|
| 8 |
import time
|
| 9 |
import os
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
+
from langchain_core.tools import tool
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
|
|
|
|
| 38 |
# If we get here, either 404 or download error
|
| 39 |
return ""
|
| 40 |
|
| 41 |
+
@tool
|
| 42 |
+
def image_tool(task_id: str) -> str:
|
| 43 |
"""
|
| 44 |
+
Expects: task_id is a string
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
Returns: "OCR text + brief caption or an error message"
|
| 46 |
|
| 47 |
"""
|
| 48 |
+
print("reached image_tool")
|
| 49 |
# path_or_id = state.get("ocr_path", "")
|
| 50 |
for ext in ("png", "jpg", "jpeg"):
|
| 51 |
+
candidate = _download_file_for_task(task_id, ext)
|
| 52 |
if candidate:
|
| 53 |
local_img = candidate
|
| 54 |
break
|
|
|
|
| 119 |
print("combined: ")
|
| 120 |
return combined
|
| 121 |
|
| 122 |
+
@tool
|
| 123 |
+
def excel_tool(task_id: str) -> str:
|
| 124 |
"""
|
| 125 |
Downloads <task_id>.xlsx (if any) and returns a stringified list of
|
| 126 |
records from the specified sheet. No fallback to user-supplied tables.
|
| 127 |
+
Expected keys in `task_id`:
|
| 128 |
• task_id – required (used to download the file)
|
| 129 |
+
|
| 130 |
returns: stringified list of records from the specified sheet
|
| 131 |
"""
|
| 132 |
+
print("reached excel_tool")
|
| 133 |
+
sheet = "Sheet1"
|
|
|
|
| 134 |
|
| 135 |
local_xlsx = _download_file_for_task(task_id, "xlsx")
|
| 136 |
if not local_xlsx or not os.path.exists(local_xlsx):
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
import openai
|
| 152 |
+
@tool
|
| 153 |
+
def audio_transcriber_tool(task_id: str) -> str:
|
| 154 |
"""
|
| 155 |
LangGraph tool for transcribing audio via OpenAI's Whisper API.
|
| 156 |
+
Expects: task_id is a string
|
|
|
|
|
|
|
|
|
|
| 157 |
Returns:
|
| 158 |
"<text or error message>"
|
| 159 |
Always attempts to download the file for the given path or task ID.
|
| 160 |
"""
|
| 161 |
print("reached audio_transcriber_tool")
|
| 162 |
+
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Always attempt to download the file, regardless of local existence
|
| 165 |
local_audio = ""
|
| 166 |
for ext in ("mp3", "wav", "m4a"):
|
| 167 |
+
candidate = _download_file_for_task(task_id, ext)
|
| 168 |
if candidate:
|
| 169 |
local_audio = candidate
|
| 170 |
break
|
|
|
|
| 196 |
import re
|
| 197 |
import requests
|
| 198 |
|
| 199 |
+
@tool
|
| 200 |
+
def wikipedia_search_tool(wiki_query: str) -> str:
|
| 201 |
"""
|
| 202 |
LangGraph wrapper for searching Wikipedia.
|
| 203 |
+
Expects: wiki_query is a non‐empty string.
|
| 204 |
Returns: text summary of first matching page or an error message>"
|
| 205 |
|
| 206 |
If no valid wiki_query is provided, returns {}.
|
| 207 |
"""
|
| 208 |
print("reached wikipedia search tool")
|
| 209 |
+
query = wiki_query
|
| 210 |
if not query:
|
| 211 |
return {}
|
| 212 |
|
|
|
|
| 260 |
from langchain.schema import SystemMessage, HumanMessage
|
| 261 |
LLM = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.2)
|
| 262 |
|
| 263 |
+
@tool
|
| 264 |
+
def analyze_code_tool(task_id: str) -> str:
|
| 265 |
"""
|
| 266 |
+
Either task_id OR (file + task_id)
|
| 267 |
Reads the code (max 400 lines / 10 kB) and asks the LLM for:
|
| 268 |
• plain-language summary
|
| 269 |
• list of key functions/classes
|
|
|
|
| 272 |
"""
|
| 273 |
print("reached analyze_code_tool")
|
| 274 |
code_txt = ""
|
| 275 |
+
if not task_id:
|
| 276 |
+
code_txt = "No code provided."
|
| 277 |
+
else:
|
| 278 |
+
path = _download_file_for_task(task_id, "py")
|
| 279 |
if not path:
|
| 280 |
return "Error: .py file not found for this task."
|
| 281 |
code_txt = Path(path).read_text(encoding="utf-8", errors="ignore")
|
| 282 |
+
# else:
|
| 283 |
+
# return "Error: neither snippet nor file provided."
|
| 284 |
|
| 285 |
# Truncate for safety
|
| 286 |
lines = code_txt.splitlines()[:400]
|
|
|
|
| 293 |
"Summarise what it does, list key functions/classes, "
|
| 294 |
"and point out any obvious bugs, performance issues or style problems.\n\n"
|
| 295 |
f"```python\n{code_sample}\n```"
|
| 296 |
+
"If you can then find the output of the code and return it in the output."
|
| 297 |
))
|
| 298 |
]
|
| 299 |
return LLM.invoke(prompt).content.strip()
|