Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from smolagents import ToolCallingAgent, DuckDuckGoSearchTool, load_tool, tool, FinalAnswerTool, OpenAIServerModel, VisitWebpageTool
|
| 9 |
|
|
@@ -32,6 +34,40 @@ model = OpenAIServerModel(
|
|
| 32 |
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
class BasicAgent:
|
| 36 |
def __init__(self):
|
| 37 |
print("BasicAgent initialized.")
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
| 7 |
+
from urllib.parse import quote
|
| 8 |
+
|
| 9 |
|
| 10 |
from smolagents import ToolCallingAgent, DuckDuckGoSearchTool, load_tool, tool, FinalAnswerTool, OpenAIServerModel, VisitWebpageTool
|
| 11 |
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
+
|
| 38 |
+
@tool
|
| 39 |
+
def simple_web_search(query: str, num_results: int = 3) -> str:
|
| 40 |
+
"""The web search using direct requests"""
|
| 41 |
+
try:
|
| 42 |
+
url = f"https://html.duckduckgo.com/html/?q={quote(query)}"
|
| 43 |
+
headers = {
|
| 44 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0'
|
| 45 |
+
}
|
| 46 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
|
| 49 |
+
# This is a simplified parser - you may need to adjust based on actual HTML
|
| 50 |
+
from bs4 import BeautifulSoup
|
| 51 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 52 |
+
results = []
|
| 53 |
+
|
| 54 |
+
for result in soup.select('.result')[:num_results]:
|
| 55 |
+
title = result.select_one('.result__title a')
|
| 56 |
+
snippet = result.select_one('.result__snippet')
|
| 57 |
+
if title and snippet:
|
| 58 |
+
results.append({
|
| 59 |
+
'title': title.get_text(strip=True),
|
| 60 |
+
'link': title['href'],
|
| 61 |
+
'snippet': snippet.get_text(strip=True)
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
return json.dumps(results if results else {"error": "No results found"})
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return json.dumps({"error": str(e)})
|
| 68 |
+
|
| 69 |
+
simple_web_search = simple_web_search()
|
| 70 |
+
|
| 71 |
class BasicAgent:
|
| 72 |
def __init__(self):
|
| 73 |
print("BasicAgent initialized.")
|