Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
|
@@ -22,6 +23,43 @@ class BasicAgent:
|
|
| 22 |
return fixed_answer
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
|
|
@@ -49,8 +87,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 49 |
try:
|
| 50 |
# agent = BasicAgent()
|
| 51 |
model = InferenceClientModel()
|
| 52 |
-
|
| 53 |
-
my_small_agent = CodeAgent(tools=[search_tool], model=model)
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error instantiating agent: {e}")
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, tool
|
| 7 |
+
|
| 8 |
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
|
|
|
| 23 |
return fixed_answer
|
| 24 |
|
| 25 |
|
| 26 |
+
@tool
|
| 27 |
+
# this tool allow web search on a local SearXNG instance
|
| 28 |
+
def search_web(query: str, num_results: int = 5) -> list:
|
| 29 |
+
"""
|
| 30 |
+
Perform a web search using local SearXNG instance.
|
| 31 |
+
Args:
|
| 32 |
+
query (str): The search query.
|
| 33 |
+
num_results (int): The number of results to return.
|
| 34 |
+
Returns:
|
| 35 |
+
list: A list of search results sorted by score with {url, title, content, score} for each result.
|
| 36 |
+
"""
|
| 37 |
+
# local metaserach engine searxng, run on localhost:8888
|
| 38 |
+
searxng_url = "http://localhost:8888/search"
|
| 39 |
+
params = {"q": query, "format": 'json'}
|
| 40 |
+
response = requests.get(searxng_url, params=params)
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
ret = response.json()
|
| 43 |
+
# keep only the response'results' array
|
| 44 |
+
results = ret.get("results", [])
|
| 45 |
+
# keep only the first num_results
|
| 46 |
+
results = results[:num_results]
|
| 47 |
+
# for each result keep only the url, title and content ans score
|
| 48 |
+
results = [
|
| 49 |
+
{
|
| 50 |
+
"url": result.get("url"),
|
| 51 |
+
"title": result.get("title"),
|
| 52 |
+
"content": result.get("content"),
|
| 53 |
+
"score": result.get("score"),
|
| 54 |
+
}
|
| 55 |
+
for result in results
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
return results
|
| 59 |
+
|
| 60 |
+
else:
|
| 61 |
+
print(f"Error: {response.status_code}")
|
| 62 |
+
return []
|
| 63 |
|
| 64 |
|
| 65 |
|
|
|
|
| 87 |
try:
|
| 88 |
# agent = BasicAgent()
|
| 89 |
model = InferenceClientModel()
|
| 90 |
+
my_small_agent = CodeAgent(tools=[search_web], model=model)
|
|
|
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
print(f"Error instantiating agent: {e}")
|