Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,24 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
print("BasicAgent initialized.")
|
|
@@ -32,13 +50,7 @@ class BasicAgent:
|
|
| 32 |
print(answer)'''
|
| 33 |
|
| 34 |
# Add tools
|
| 35 |
-
tools = [WikipediaSearchTool(
|
| 36 |
-
user_agent="MyResearchBot (kanittha.kra@gmail.com)",
|
| 37 |
-
language="en",
|
| 38 |
-
content_type="summary", # or "text"
|
| 39 |
-
extract_format="WIKI",
|
| 40 |
-
)
|
| 41 |
-
]
|
| 42 |
|
| 43 |
'''query = "Who won the 2024 Olympic gold in swimming?"
|
| 44 |
|
|
@@ -60,7 +72,6 @@ class BasicAgent:
|
|
| 60 |
return fixed_answer
|
| 61 |
except Exception as e:
|
| 62 |
return f"AGENT ERROR: {e}"
|
| 63 |
-
|
| 64 |
|
| 65 |
|
| 66 |
'''def __call__(self, question: str) -> str:
|
|
@@ -251,7 +262,7 @@ if __name__ == "__main__":
|
|
| 251 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 252 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 253 |
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 254 |
-
|
| 255 |
if space_host_startup:
|
| 256 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 257 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
|
@@ -268,4 +279,11 @@ if __name__ == "__main__":
|
|
| 268 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 269 |
|
| 270 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
demo.launch(debug=True, share=False)
|
|
|
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
|
| 19 |
+
class WikipediaSearchTool(Tool):
|
| 20 |
+
name = "wikipedia_search"
|
| 21 |
+
description = "Search Wikipedia and return a summary for a given query"
|
| 22 |
+
|
| 23 |
+
inputs = {"query": {"type": "string", "description": "Search term"}}
|
| 24 |
+
output_type = "string"
|
| 25 |
+
|
| 26 |
+
def forward(self, query: str) -> str:
|
| 27 |
+
try:
|
| 28 |
+
results = wikipedia.search(query)
|
| 29 |
+
if not results:
|
| 30 |
+
return "No results found."
|
| 31 |
+
page = wikipedia.page(results[0])
|
| 32 |
+
return page.summary[:1000] # limit to 1000 chars for safety
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error: {e}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
class BasicAgent:
|
| 38 |
def __init__(self):
|
| 39 |
print("BasicAgent initialized.")
|
|
|
|
| 50 |
print(answer)'''
|
| 51 |
|
| 52 |
# Add tools
|
| 53 |
+
tools = [tools = [WikipediaSearchTool()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
'''query = "Who won the 2024 Olympic gold in swimming?"
|
| 56 |
|
|
|
|
| 72 |
return fixed_answer
|
| 73 |
except Exception as e:
|
| 74 |
return f"AGENT ERROR: {e}"
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
'''def __call__(self, question: str) -> str:
|
|
|
|
| 262 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 263 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 264 |
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 265 |
+
|
| 266 |
if space_host_startup:
|
| 267 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 268 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
|
|
|
| 279 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 280 |
|
| 281 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 282 |
+
|
| 283 |
+
tool = WikipediaSearchTool()
|
| 284 |
+
query = "Artificial intelligence"
|
| 285 |
+
result = tool.forward(query)
|
| 286 |
+
print("🔍 Query:", query)
|
| 287 |
+
print("📄 Result:", result[:500], "...")
|
| 288 |
+
|
| 289 |
demo.launch(debug=True, share=False)
|