Update app.py
Browse files
app.py
CHANGED
@@ -9,17 +9,13 @@ from bs4 import BeautifulSoup
|
|
9 |
import regex
|
10 |
import pandas as pd
|
11 |
import torch
|
12 |
-
from functools import lru_cache
|
13 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
14 |
-
from smolagents import
|
15 |
-
import smolagents.tools as _tools
|
16 |
from smolagents.models import ChatMessage
|
17 |
-
|
18 |
|
19 |
subprocess.run(["playwright", "install"], check=True)
|
20 |
|
21 |
-
print(dir(_tools))
|
22 |
-
|
23 |
# (Keep Constants as is)
|
24 |
# --- Constants ---
|
25 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -46,30 +42,6 @@ def check_token_access():
|
|
46 |
except Exception as e:
|
47 |
print("❌ Token check failed:", e)
|
48 |
|
49 |
-
class CachedWebSearchTool(WebSearchTool):
|
50 |
-
@lru_cache(maxsize=128)
|
51 |
-
def run(self, query: str):
|
52 |
-
# identical queries return instantly
|
53 |
-
return super().run(query)
|
54 |
-
|
55 |
-
class CachedWikiTool(WikipediaSearchTool):
|
56 |
-
@lru_cache(maxsize=128)
|
57 |
-
def run(self, page: str):
|
58 |
-
return super().run(page)
|
59 |
-
|
60 |
-
class PreloadedPythonTool(PythonInterpreterTool):
|
61 |
-
"""
|
62 |
-
A PythonInterpreterTool that automatically prepends the necessary imports
|
63 |
-
(bs4, BeautifulSoup, regex) so you never hit NameError inside your code blocks.
|
64 |
-
"""
|
65 |
-
def run(self, code: str) -> str:
|
66 |
-
preamble = (
|
67 |
-
"import bs4\n"
|
68 |
-
"from bs4 import BeautifulSoup\n"
|
69 |
-
"import regex\n"
|
70 |
-
)
|
71 |
-
return super().run(preamble + code)
|
72 |
-
|
73 |
# --- Basic Model Definition ---
|
74 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
75 |
class BasicModel:
|
@@ -194,13 +166,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
194 |
# (1) Create the LLM wrapper
|
195 |
llm_model = BasicModel(model_id=MODEL_ID, hf_token=hf_token)
|
196 |
# (2) Create the tools
|
|
|
197 |
wiki_tool = CachedWikiTool()
|
198 |
search_tool = CachedWebSearchTool()
|
199 |
python_tool = PythonInterpreterTool()
|
200 |
html_parse_tool = VisitWebpageTool()
|
201 |
#(3) Create the CodeAgent, passing the LLM wrapper and tools
|
202 |
agent = CodeAgent(model=llm_model,
|
203 |
-
tools=[wiki_tool, search_tool, python_tool, html_parse_tool],
|
204 |
max_steps=5,
|
205 |
add_base_tools=True,
|
206 |
additional_authorized_imports=["dateparser", "bs4", "regex"])
|
|
|
9 |
import regex
|
10 |
import pandas as pd
|
11 |
import torch
|
|
|
12 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
13 |
+
from smolagents import CodeAgent, VisitWebpageTool
|
|
|
14 |
from smolagents.models import ChatMessage
|
15 |
+
from custom_tools import WebpageStructureAnalyzerTool, CachedWebSearchTool, CachedWikiTool, PreloadedPythonTool
|
16 |
|
17 |
subprocess.run(["playwright", "install"], check=True)
|
18 |
|
|
|
|
|
19 |
# (Keep Constants as is)
|
20 |
# --- Constants ---
|
21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
42 |
except Exception as e:
|
43 |
print("❌ Token check failed:", e)
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# --- Basic Model Definition ---
|
46 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
47 |
class BasicModel:
|
|
|
166 |
# (1) Create the LLM wrapper
|
167 |
llm_model = BasicModel(model_id=MODEL_ID, hf_token=hf_token)
|
168 |
# (2) Create the tools
|
169 |
+
web_structure_analyzer_tool = WebpageStructureAnalyzerTool()
|
170 |
wiki_tool = CachedWikiTool()
|
171 |
search_tool = CachedWebSearchTool()
|
172 |
python_tool = PythonInterpreterTool()
|
173 |
html_parse_tool = VisitWebpageTool()
|
174 |
#(3) Create the CodeAgent, passing the LLM wrapper and tools
|
175 |
agent = CodeAgent(model=llm_model,
|
176 |
+
tools=[web_structure_analyzer_tool, wiki_tool, search_tool, python_tool, html_parse_tool],
|
177 |
max_steps=5,
|
178 |
add_base_tools=True,
|
179 |
additional_authorized_imports=["dateparser", "bs4", "regex"])
|