Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ from smolagents import (
|
|
13 |
CodeAgent,
|
14 |
HfApiModel,
|
15 |
Tool,
|
|
|
16 |
)
|
17 |
from smolagents.agent_types import (
|
18 |
AgentAudio,
|
@@ -35,103 +36,10 @@ from scripts.text_web_browser import (
|
|
35 |
from scripts.visual_qa import visualizer
|
36 |
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
inputs = {
|
42 |
-
"query": {"type": "string", "description": "The search query to perform."},
|
43 |
-
"filter_year": {
|
44 |
-
"type": "integer",
|
45 |
-
"description": "Optionally restrict results to a certain year",
|
46 |
-
"nullable": True,
|
47 |
-
},
|
48 |
-
}
|
49 |
-
output_type = "string"
|
50 |
-
|
51 |
-
def __init__(self):
|
52 |
-
super().__init__(self)
|
53 |
-
import os
|
54 |
-
|
55 |
-
self.serpapi_key = os.getenv("SERPER_API_KEY")
|
56 |
-
|
57 |
-
def forward(self, query: str, filter_year: Optional[int] = None) -> str:
|
58 |
-
import requests
|
59 |
-
|
60 |
-
if self.serpapi_key is None:
|
61 |
-
raise ValueError(
|
62 |
-
"Missing SerpAPI key. Make sure you have 'SERPER_API_KEY' in your env variables."
|
63 |
-
)
|
64 |
-
|
65 |
-
params = {
|
66 |
-
"engine": "google",
|
67 |
-
"q": query,
|
68 |
-
"api_key": self.serpapi_key,
|
69 |
-
"google_domain": "google.com",
|
70 |
-
}
|
71 |
-
|
72 |
-
headers = {"X-API-KEY": self.serpapi_key, "Content-Type": "application/json"}
|
73 |
-
|
74 |
-
if filter_year is not None:
|
75 |
-
params["tbs"] = (
|
76 |
-
f"cdr:1,cd_min:01/01/{filter_year},cd_max:12/31/{filter_year}"
|
77 |
-
)
|
78 |
-
|
79 |
-
response = requests.request(
|
80 |
-
"POST",
|
81 |
-
"https://google.serper.dev/search",
|
82 |
-
headers=headers,
|
83 |
-
data=json.dumps(params),
|
84 |
-
)
|
85 |
|
86 |
-
if response.status_code == 200:
|
87 |
-
results = response.json()
|
88 |
-
else:
|
89 |
-
raise ValueError(response.json())
|
90 |
-
|
91 |
-
if "organic" not in results.keys():
|
92 |
-
print("REZZZ", results.keys())
|
93 |
-
if filter_year is not None:
|
94 |
-
raise Exception(
|
95 |
-
f"No results found for query: '{query}' with filtering on year={filter_year}. Use a less restrictive query or do not filter on year."
|
96 |
-
)
|
97 |
-
else:
|
98 |
-
raise Exception(
|
99 |
-
f"No results found for query: '{query}'. Use a less restrictive query."
|
100 |
-
)
|
101 |
-
if len(results["organic"]) == 0:
|
102 |
-
year_filter_message = (
|
103 |
-
f" with filter year={filter_year}" if filter_year is not None else ""
|
104 |
-
)
|
105 |
-
return f"No results found for '{query}'{year_filter_message}. Try with a more general query, or remove the year filter."
|
106 |
-
|
107 |
-
web_snippets = []
|
108 |
-
if "organic" in results:
|
109 |
-
for idx, page in enumerate(results["organic"]):
|
110 |
-
date_published = ""
|
111 |
-
if "date" in page:
|
112 |
-
date_published = "\nDate published: " + page["date"]
|
113 |
-
|
114 |
-
source = ""
|
115 |
-
if "source" in page:
|
116 |
-
source = "\nSource: " + page["source"]
|
117 |
-
|
118 |
-
snippet = ""
|
119 |
-
if "snippet" in page:
|
120 |
-
snippet = "\n" + page["snippet"]
|
121 |
-
|
122 |
-
redacted_version = f"{idx}. [{page['title']}]({page['link']}){date_published}{source}\n{snippet}"
|
123 |
-
|
124 |
-
redacted_version = redacted_version.replace(
|
125 |
-
"Your browser can't play this video.", ""
|
126 |
-
)
|
127 |
-
web_snippets.append(redacted_version)
|
128 |
-
|
129 |
-
return "## Search Results\n" + "\n\n".join(web_snippets)
|
130 |
-
|
131 |
-
|
132 |
-
# web_search = GoogleSearchTool()
|
133 |
-
|
134 |
-
# print(web_search(query="Donald Trump news"))
|
135 |
# quit()
|
136 |
AUTHORIZED_IMPORTS = [
|
137 |
"requests",
|
@@ -188,7 +96,7 @@ ti_tool = TextInspectorTool(model, text_limit)
|
|
188 |
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
189 |
|
190 |
WEB_TOOLS = [
|
191 |
-
|
192 |
VisitTool(browser),
|
193 |
PageUpTool(browser),
|
194 |
PageDownTool(browser),
|
|
|
13 |
CodeAgent,
|
14 |
HfApiModel,
|
15 |
Tool,
|
16 |
+
GoogleSearchTool
|
17 |
)
|
18 |
from smolagents.agent_types import (
|
19 |
AgentAudio,
|
|
|
36 |
from scripts.visual_qa import visualizer
|
37 |
|
38 |
|
39 |
+
web_search = GoogleSearchTool(provider="serper")
|
40 |
+
|
41 |
+
print(web_search(query="Donald Trump news"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# quit()
|
44 |
AUTHORIZED_IMPORTS = [
|
45 |
"requests",
|
|
|
96 |
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
97 |
|
98 |
WEB_TOOLS = [
|
99 |
+
web_search,
|
100 |
VisitTool(browser),
|
101 |
PageUpTool(browser),
|
102 |
PageDownTool(browser),
|