Spaces:
Runtime error
Runtime error
Update app.py
#3
by
wt002
- opened
app.py
CHANGED
@@ -9,10 +9,67 @@ from Gradio_UI import GradioUI
|
|
9 |
|
10 |
import requests
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
class FinalAnswerTool:
|
13 |
def __call__(self, result: str):
|
14 |
return f"\n Final Answer:\n{result}"
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
class ToolCallingAgent:
|
17 |
def __init__(self, tools, final_tool, model, max_steps=10):
|
18 |
self.tools = tools
|
@@ -21,25 +78,25 @@ class ToolCallingAgent:
|
|
21 |
self.max_steps = max_steps
|
22 |
|
23 |
def run(self, input_text):
|
24 |
-
print(f"\
|
25 |
for tool in self.tools:
|
26 |
try:
|
27 |
result = tool(input_text)
|
28 |
if result:
|
29 |
final_output = self.final_tool(str(result))
|
30 |
print(final_output)
|
31 |
-
return final_output
|
32 |
except Exception as e:
|
33 |
print(f"\n[{tool.__class__.__name__} Error]: {str(e)}")
|
34 |
|
35 |
-
# Initialize
|
36 |
-
weather_tool = WeatherTool(api_key="
|
37 |
visit_webpage = VisitWebpageTool()
|
38 |
duckduckgo_tool = CustomDuckDuckGoSearchTool()
|
39 |
final_answer = FinalAnswerTool()
|
40 |
model = DummyModel()
|
41 |
|
42 |
-
# Initialize
|
43 |
web_agent = ToolCallingAgent(
|
44 |
tools=[duckduckgo_tool, visit_webpage, weather_tool],
|
45 |
final_tool=final_answer,
|
@@ -47,12 +104,10 @@ web_agent = ToolCallingAgent(
|
|
47 |
max_steps=10
|
48 |
)
|
49 |
|
50 |
-
#
|
51 |
-
web_agent.run("Tokyo") #
|
52 |
|
53 |
|
54 |
-
final_answer = FinalAnswerTool()
|
55 |
-
|
56 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
57 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
58 |
|
|
|
9 |
|
10 |
import requests
|
11 |
|
12 |
+
import requests
|
13 |
+
from duckduckgo_search import DDGS
|
14 |
+
|
15 |
+
# ----- WeatherTool -----
|
16 |
+
class WeatherTool:
|
17 |
+
def __init__(self, api_key: str):
|
18 |
+
self.api_key = api_key
|
19 |
+
|
20 |
+
def __call__(self, city: str):
|
21 |
+
try:
|
22 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={self.api_key}&units=metric"
|
23 |
+
response = requests.get(url)
|
24 |
+
data = response.json()
|
25 |
+
|
26 |
+
if response.status_code != 200:
|
27 |
+
return f"Error: {data.get('message', 'Unable to fetch weather data')}"
|
28 |
+
|
29 |
+
weather = data["weather"][0]["description"]
|
30 |
+
temp = data["main"]["temp"]
|
31 |
+
feels_like = data["main"]["feels_like"]
|
32 |
+
humidity = data["main"]["humidity"]
|
33 |
+
wind_speed = data["wind"]["speed"]
|
34 |
+
|
35 |
+
return (f"Weather in {city.title()}:\n"
|
36 |
+
f"- Condition: {weather}\n"
|
37 |
+
f"- Temperature: {temp}°C (Feels like {feels_like}°C)\n"
|
38 |
+
f"- Humidity: {humidity}%\n"
|
39 |
+
f"- Wind Speed: {wind_speed} m/s")
|
40 |
+
except Exception as e:
|
41 |
+
return f"Exception during weather fetch: {str(e)}"
|
42 |
+
|
43 |
+
# ----- VisitWebpageTool -----
|
44 |
+
class VisitWebpageTool:
|
45 |
+
def __call__(self, url: str):
|
46 |
+
return f"Pretending to visit webpage: {url}"
|
47 |
+
|
48 |
+
# ----- CustomDuckDuckGoSearchTool -----
|
49 |
+
class CustomDuckDuckGoSearchTool:
|
50 |
+
def __call__(self, query: str, max_results: int = 3):
|
51 |
+
try:
|
52 |
+
with DDGS() as ddgs:
|
53 |
+
results = []
|
54 |
+
for r in ddgs.text(query):
|
55 |
+
results.append(r)
|
56 |
+
if len(results) >= max_results:
|
57 |
+
break
|
58 |
+
return results
|
59 |
+
except Exception as e:
|
60 |
+
return f"Search error: {str(e)}"
|
61 |
+
|
62 |
+
# ----- FinalAnswerTool -----
|
63 |
class FinalAnswerTool:
|
64 |
def __call__(self, result: str):
|
65 |
return f"\n Final Answer:\n{result}"
|
66 |
|
67 |
+
# ----- DummyModel (if needed) -----
|
68 |
+
class DummyModel:
|
69 |
+
def call(self, input_text):
|
70 |
+
return f"Model processed: {input_text}"
|
71 |
+
|
72 |
+
# ----- ToolCallingAgent -----
|
73 |
class ToolCallingAgent:
|
74 |
def __init__(self, tools, final_tool, model, max_steps=10):
|
75 |
self.tools = tools
|
|
|
78 |
self.max_steps = max_steps
|
79 |
|
80 |
def run(self, input_text):
|
81 |
+
print(f"\nAgent received: {input_text}")
|
82 |
for tool in self.tools:
|
83 |
try:
|
84 |
result = tool(input_text)
|
85 |
if result:
|
86 |
final_output = self.final_tool(str(result))
|
87 |
print(final_output)
|
88 |
+
return final_output
|
89 |
except Exception as e:
|
90 |
print(f"\n[{tool.__class__.__name__} Error]: {str(e)}")
|
91 |
|
92 |
+
# Initialize tools
|
93 |
+
weather_tool = WeatherTool(api_key="HF_Token")
|
94 |
visit_webpage = VisitWebpageTool()
|
95 |
duckduckgo_tool = CustomDuckDuckGoSearchTool()
|
96 |
final_answer = FinalAnswerTool()
|
97 |
model = DummyModel()
|
98 |
|
99 |
+
# Initialize and run the agent
|
100 |
web_agent = ToolCallingAgent(
|
101 |
tools=[duckduckgo_tool, visit_webpage, weather_tool],
|
102 |
final_tool=final_answer,
|
|
|
104 |
max_steps=10
|
105 |
)
|
106 |
|
107 |
+
# Example query
|
108 |
+
web_agent.run("Tokyo") # Can try a city or any other search term
|
109 |
|
110 |
|
|
|
|
|
111 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
112 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
113 |
|