Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,13 @@ import requests
|
|
5 |
import inspect
|
6 |
import subprocess
|
7 |
import dateparser
|
|
|
|
|
8 |
import pandas as pd
|
9 |
import torch
|
|
|
10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
11 |
-
from smolagents import CodeAgent, WebSearchTool, WikipediaSearchTool
|
12 |
from smolagents.models import ChatMessage
|
13 |
# from huggingface_hub import InferenceClient, hf_hub_download
|
14 |
|
@@ -60,6 +63,17 @@ def check_token_access():
|
|
60 |
except Exception as e:
|
61 |
print("❌ Token check failed:", e)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# --- Basic Agent Definition ---
|
64 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
65 |
class BasicAgent:
|
@@ -103,13 +117,15 @@ class BasicAgent:
|
|
103 |
# temperature=1.0,
|
104 |
)
|
105 |
# Introduce tools
|
106 |
-
wiki_tool =
|
107 |
-
search_tool =
|
|
|
|
|
108 |
# Initialize the agent
|
109 |
self.agent = CodeAgent(model=self,
|
110 |
-
tools=[wiki_tool, search_tool],
|
111 |
add_base_tools=True,
|
112 |
-
additional_authorized_imports=["dateparser"])
|
113 |
|
114 |
def _serialize_messages(self, messages):
|
115 |
prompt = []
|
@@ -307,7 +323,8 @@ with gr.Blocks() as demo:
|
|
307 |
|
308 |
run_button.click(
|
309 |
fn=run_and_submit_all,
|
310 |
-
outputs=[status_output, results_table]
|
|
|
311 |
)
|
312 |
|
313 |
if __name__ == "__main__":
|
|
|
5 |
import inspect
|
6 |
import subprocess
|
7 |
import dateparser
|
8 |
+
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
|
14 |
+
from smolagents import CodeAgent, WebSearchTool, WikipediaSearchTool, PythonTool, HTMLParseTool
|
15 |
from smolagents.models import ChatMessage
|
16 |
# from huggingface_hub import InferenceClient, hf_hub_download
|
17 |
|
|
|
63 |
except Exception as e:
|
64 |
print("❌ Token check failed:", e)
|
65 |
|
66 |
+
class CachedWebSearchTool(WebSearchTool):
|
67 |
+
@lru_cache(maxsize=128)
|
68 |
+
def run(self, query: str):
|
69 |
+
# identical queries return instantly
|
70 |
+
return super().run(query)
|
71 |
+
|
72 |
+
class CachedWikiTool(WikipediaTool):
|
73 |
+
@lru_cache(maxsize=128)
|
74 |
+
def run(self, page: str):
|
75 |
+
return super().run(page)
|
76 |
+
|
77 |
# --- Basic Agent Definition ---
|
78 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
79 |
class BasicAgent:
|
|
|
117 |
# temperature=1.0,
|
118 |
)
|
119 |
# Introduce tools
|
120 |
+
wiki_tool = CachedWikiTool()
|
121 |
+
search_tool = CachedWebSearchTool()
|
122 |
+
python_tool = PythonTool()
|
123 |
+
html_parse_tool = HTMLParseTool()
|
124 |
# Initialize the agent
|
125 |
self.agent = CodeAgent(model=self,
|
126 |
+
tools=[wiki_tool, search_tool, python_tool, html_parse_tool],
|
127 |
add_base_tools=True,
|
128 |
+
additional_authorized_imports=["dateparser", "bs4", "regex"])
|
129 |
|
130 |
def _serialize_messages(self, messages):
|
131 |
prompt = []
|
|
|
323 |
|
324 |
run_button.click(
|
325 |
fn=run_and_submit_all,
|
326 |
+
outputs=[status_output, results_table],
|
327 |
+
stream=True
|
328 |
)
|
329 |
|
330 |
if __name__ == "__main__":
|