Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,8 +7,7 @@ from typing import List, Dict, Union
|
|
7 |
import requests
|
8 |
import wikipediaapi
|
9 |
import pandas as pd
|
10 |
-
from
|
11 |
-
from agent import build_graph
|
12 |
|
13 |
load_dotenv()
|
14 |
|
@@ -17,24 +16,84 @@ load_dotenv()
|
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
18 |
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
class BasicAgent:
|
24 |
-
"""A langgraph agent."""
|
25 |
def __init__(self):
|
26 |
print("BasicAgent initialized.")
|
27 |
-
self.graph = build_graph()
|
28 |
-
|
29 |
def __call__(self, question: str) -> str:
|
30 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
answer = messages['messages'][-1].content
|
35 |
-
return answer[14:]
|
36 |
-
|
37 |
-
|
38 |
|
39 |
|
40 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
7 |
import requests
|
8 |
import wikipediaapi
|
9 |
import pandas as pd
|
10 |
+
from duckduckgo_search import DDGS
|
|
|
11 |
|
12 |
load_dotenv()
|
13 |
|
|
|
16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
17 |
|
18 |
|
19 |
+
# Custom search tool class
|
20 |
+
class CustomDuckDuckGoSearchTool:
|
21 |
+
def __call__(self, query: str, max_results: int = 5):
|
22 |
+
try:
|
23 |
+
with DDGS() as ddgs:
|
24 |
+
results = []
|
25 |
+
for r in ddgs.text(query):
|
26 |
+
results.append(r)
|
27 |
+
if len(results) >= max_results:
|
28 |
+
break
|
29 |
+
return results
|
30 |
+
except Exception as e:
|
31 |
+
return f"Search error: {str(e)}"
|
32 |
+
|
33 |
+
# Dummy placeholder for `visit_webpage` tool
|
34 |
+
class VisitWebpageTool:
|
35 |
+
def __call__(self, url: str):
|
36 |
+
return f"Pretending to visit: {url}"
|
37 |
+
|
38 |
+
# Final answer tool to format and return the final response
|
39 |
+
class FinalAnswerTool:
|
40 |
+
def __call__(self, results):
|
41 |
+
formatted_answer = "Final Answer:\n"
|
42 |
+
for result in results:
|
43 |
+
formatted_answer += f"- {str(result)}\n"
|
44 |
+
return formatted_answer
|
45 |
+
|
46 |
+
# Dummy model
|
47 |
+
class DummyModel:
|
48 |
+
def call(self, input_text):
|
49 |
+
return f"Model processing: {input_text}"
|
50 |
+
|
51 |
+
# Modified ToolCallingAgent to use FinalAnswerTool
|
52 |
+
class ToolCallingAgent:
|
53 |
+
def __init__(self, tools, model, final_answer_tool, max_steps=10):
|
54 |
+
self.tools = tools
|
55 |
+
self.model = model
|
56 |
+
self.final_answer_tool = final_answer_tool
|
57 |
+
self.max_steps = max_steps
|
58 |
|
59 |
+
def run(self, query):
|
60 |
+
print(f"Running agent with query: {query}")
|
61 |
+
tool_outputs = []
|
62 |
+
for tool in self.tools:
|
63 |
+
output = tool(query)
|
64 |
+
print("Tool output:", output)
|
65 |
+
tool_outputs.append(output)
|
66 |
+
# Use the final answer tool to format the collected outputs
|
67 |
+
final_result = self.final_answer_tool(tool_outputs)
|
68 |
+
print(final_result)
|
69 |
+
return final_result
|
70 |
|
71 |
+
# Initialize tools and model
|
72 |
+
model = DummyModel()
|
73 |
+
search_tool = CustomDuckDuckGoSearchTool()
|
74 |
+
visit_webpage = VisitWebpageTool()
|
75 |
+
final_answer = FinalAnswerTool()
|
76 |
+
|
77 |
+
# Initialize the agent
|
78 |
+
web_agent = ToolCallingAgent(
|
79 |
+
tools=[search_tool, visit_webpage],
|
80 |
+
model=model,
|
81 |
+
final_answer_tool=final_answer,
|
82 |
+
max_steps=10
|
83 |
+
)
|
84 |
+
|
85 |
+
# Example usage
|
86 |
+
#web_agent.run("Latest AI tools")
|
87 |
+
|
88 |
+
# --- Basic Agent Definition ---
|
89 |
class BasicAgent:
|
|
|
90 |
def __init__(self):
|
91 |
print("BasicAgent initialized.")
|
|
|
|
|
92 |
def __call__(self, question: str) -> str:
|
93 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
94 |
+
fixed_answer = web_agent.run({question})
|
95 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
96 |
+
return fixed_answer
|
|
|
|
|
|
|
|
|
97 |
|
98 |
|
99 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|