Spaces:
Runtime error
Runtime error
fixing
Browse files- app.py +11 -22
- requirements.txt +1 -1
app.py
CHANGED
@@ -5,13 +5,11 @@ import requests
|
|
5 |
import inspect
|
6 |
import pandas as pd
|
7 |
from smolagents import CodeAgent
|
|
|
8 |
import json
|
9 |
import tempfile
|
10 |
import urllib.parse
|
11 |
from pathlib import Path
|
12 |
-
from duckduckgo_search import DDGS
|
13 |
-
from smolagents.schema import Tool
|
14 |
-
|
15 |
|
16 |
# --- Constants ---
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -31,20 +29,9 @@ class HfApiModel:
|
|
31 |
outputs = self.pipe(prompt, max_new_tokens=512, do_sample=True)
|
32 |
return outputs[0]["generated_text"]
|
33 |
|
34 |
-
|
35 |
-
name = "python_interpreter"
|
36 |
-
description = "Evaluate Python code for logic, data, and math."
|
37 |
-
|
38 |
-
def __call__(self, code: str) -> str:
|
39 |
-
try:
|
40 |
-
local_vars = {}
|
41 |
-
exec(code, {"__builtins__": {}}, local_vars)
|
42 |
-
return repr(local_vars)
|
43 |
-
except Exception as e:
|
44 |
-
return f"Python execution error: {e}"
|
45 |
-
|
46 |
|
47 |
-
class DuckDuckGoSearchTool
|
48 |
name = "duckduckgo_search"
|
49 |
description = "Use DuckDuckGo to search the web."
|
50 |
|
@@ -58,9 +45,8 @@ class DuckDuckGoSearchTool(Tool):
|
|
58 |
except Exception as e:
|
59 |
return f"Error using DuckDuckGoSearchTool: {e}"
|
60 |
|
61 |
-
|
62 |
# --- Custom Tools ---
|
63 |
-
class SerperSearchTool
|
64 |
"""Enhanced search tool using Serper API for more reliable results"""
|
65 |
|
66 |
name = "serper_search"
|
@@ -74,6 +60,7 @@ class SerperSearchTool(Tool):
|
|
74 |
def __call__(self, query: str) -> str:
|
75 |
"""Search the web and return formatted results"""
|
76 |
if not self.api_key:
|
|
|
77 |
return f"Search query: {query} - API key not available"
|
78 |
|
79 |
try:
|
@@ -93,25 +80,27 @@ class SerperSearchTool(Tool):
|
|
93 |
data = response.json()
|
94 |
results = []
|
95 |
|
|
|
96 |
if 'organic' in data:
|
97 |
-
for item in data['organic'][:3]:
|
98 |
results.append(f"Title: {item.get('title', 'N/A')}")
|
99 |
results.append(f"Content: {item.get('snippet', 'N/A')}")
|
100 |
results.append(f"URL: {item.get('link', 'N/A')}")
|
101 |
results.append("---")
|
102 |
|
|
|
103 |
if 'answerBox' in data:
|
104 |
answer = data['answerBox']
|
105 |
results.insert(0, f"Answer: {answer.get('answer', answer.get('snippet', 'N/A'))}")
|
106 |
results.insert(1, "---")
|
107 |
|
108 |
return "\n".join(results) if results else f"No results found for: {query}"
|
109 |
-
|
110 |
except Exception as e:
|
111 |
print(f"Serper search error: {e}")
|
112 |
return f"Search error for '{query}': {str(e)}"
|
113 |
|
114 |
-
class MathCalculatorTool
|
115 |
"""Tool for mathematical calculations and computations"""
|
116 |
|
117 |
name = "math_calculator"
|
@@ -142,7 +131,7 @@ class MathCalculatorTool(Tool):
|
|
142 |
except Exception as e:
|
143 |
return f"Math calculation error: {str(e)}"
|
144 |
|
145 |
-
class FileProcessorTool
|
146 |
"""Tool for processing various file formats"""
|
147 |
|
148 |
name = "file_processor"
|
|
|
5 |
import inspect
|
6 |
import pandas as pd
|
7 |
from smolagents import CodeAgent
|
8 |
+
from smolagents.tools import DuckDuckGoSearchTool, PythonInterpreterTool
|
9 |
import json
|
10 |
import tempfile
|
11 |
import urllib.parse
|
12 |
from pathlib import Path
|
|
|
|
|
|
|
13 |
|
14 |
# --- Constants ---
|
15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
29 |
outputs = self.pipe(prompt, max_new_tokens=512, do_sample=True)
|
30 |
return outputs[0]["generated_text"]
|
31 |
|
32 |
+
from duckduckgo_search import DDGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
class DuckDuckGoSearchTool:
|
35 |
name = "duckduckgo_search"
|
36 |
description = "Use DuckDuckGo to search the web."
|
37 |
|
|
|
45 |
except Exception as e:
|
46 |
return f"Error using DuckDuckGoSearchTool: {e}"
|
47 |
|
|
|
48 |
# --- Custom Tools ---
|
49 |
+
class SerperSearchTool:
|
50 |
"""Enhanced search tool using Serper API for more reliable results"""
|
51 |
|
52 |
name = "serper_search"
|
|
|
60 |
def __call__(self, query: str) -> str:
|
61 |
"""Search the web and return formatted results"""
|
62 |
if not self.api_key:
|
63 |
+
# Fallback to basic search if no Serper API key
|
64 |
return f"Search query: {query} - API key not available"
|
65 |
|
66 |
try:
|
|
|
80 |
data = response.json()
|
81 |
results = []
|
82 |
|
83 |
+
# Process organic results
|
84 |
if 'organic' in data:
|
85 |
+
for item in data['organic'][:3]: # Top 3 results
|
86 |
results.append(f"Title: {item.get('title', 'N/A')}")
|
87 |
results.append(f"Content: {item.get('snippet', 'N/A')}")
|
88 |
results.append(f"URL: {item.get('link', 'N/A')}")
|
89 |
results.append("---")
|
90 |
|
91 |
+
# Add answer box if available
|
92 |
if 'answerBox' in data:
|
93 |
answer = data['answerBox']
|
94 |
results.insert(0, f"Answer: {answer.get('answer', answer.get('snippet', 'N/A'))}")
|
95 |
results.insert(1, "---")
|
96 |
|
97 |
return "\n".join(results) if results else f"No results found for: {query}"
|
98 |
+
|
99 |
except Exception as e:
|
100 |
print(f"Serper search error: {e}")
|
101 |
return f"Search error for '{query}': {str(e)}"
|
102 |
|
103 |
+
class MathCalculatorTool:
|
104 |
"""Tool for mathematical calculations and computations"""
|
105 |
|
106 |
name = "math_calculator"
|
|
|
131 |
except Exception as e:
|
132 |
return f"Math calculation error: {str(e)}"
|
133 |
|
134 |
+
class FileProcessorTool:
|
135 |
"""Tool for processing various file formats"""
|
136 |
|
137 |
name = "file_processor"
|
requirements.txt
CHANGED
@@ -4,7 +4,7 @@ requests>=2.32.3
|
|
4 |
pandas==2.1.4
|
5 |
|
6 |
# SmolagentS and AI dependencies
|
7 |
-
smolagents==1.
|
8 |
|
9 |
transformers==4.45.2
|
10 |
torch==2.1.2
|
|
|
4 |
pandas==2.1.4
|
5 |
|
6 |
# SmolagentS and AI dependencies
|
7 |
+
smolagents==1.18.0
|
8 |
|
9 |
transformers==4.45.2
|
10 |
torch==2.1.2
|