Spaces:
Runtime error
Runtime error
TnR
Browse files- app.py +23 -0
- retriever.py +28 -0
- tools.py +65 -0
app.py
CHANGED
|
@@ -3,6 +3,10 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -10,6 +14,25 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import GradioUI, CodeAgent, HfApiModel
|
| 7 |
+
|
| 8 |
+
from tools import WebSearchTool, NasaImageSearchTool, ClinicalTrialsSearchTool, WikipediaSearchTool, DuckDuckGoSearchTool
|
| 9 |
+
from retriever import WebSearchRetriever
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
|
|
| 14 |
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
+
model = HfApiModel(repo_id="google/flan-t5-small")
|
| 18 |
+
|
| 19 |
+
search_tool = DuckDuckGoSearchTool()
|
| 20 |
+
|
| 21 |
+
WebSearchTool = WebSearchTool()
|
| 22 |
+
NasaImageSearchTool = NasaImageSearchTool()
|
| 23 |
+
ClinicalTrialsSearchTool = ClinicalTrialsSearchTool()
|
| 24 |
+
WikipediaSearchTool = WikipediaSearchTool()
|
| 25 |
+
|
| 26 |
+
Age = CodeAgent(
|
| 27 |
+
tools=[WebSearchTool, NasaImageSearchTool, ClinicalTrialsSearchTool, WikipediaSearchTool],
|
| 28 |
+
model=model,
|
| 29 |
+
add_base_tools=True,
|
| 30 |
+
Planning_interval=3
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
GradioUI(agent=Age).launch(debug=True, share=False)
|
| 35 |
+
|
| 36 |
class BasicAgent:
|
| 37 |
def __init__(self):
|
| 38 |
print("BasicAgent initialized.")
|
retriever.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from langchain_community.retrievers import BM25Retriever
|
| 3 |
+
from langchain.docstore.document import Document
|
| 4 |
+
import datasets
|
| 5 |
+
|
| 6 |
+
class WebSearchRetriever(Tool):
|
| 7 |
+
name = "web_search_retriever"
|
| 8 |
+
description = "Useful for retrieving information from the web based on a search query. "
|
| 9 |
+
inputs = {
|
| 10 |
+
"query": {
|
| 11 |
+
"type": "string",
|
| 12 |
+
"description": "The search query string.",
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
output_type = "list[Document]"
|
| 16 |
+
|
| 17 |
+
def __init__(self, docs):
|
| 18 |
+
self.is_initialized = False
|
| 19 |
+
self.retriever = BM25Retriever.from_documents(docs)
|
| 20 |
+
|
| 21 |
+
def forward(self, query: str):
|
| 22 |
+
results = self.retriever.get_relevant_documents(query)
|
| 23 |
+
if results:
|
| 24 |
+
return "\n\n".join([doc.page_content for doc in results[:3]])
|
| 25 |
+
else:
|
| 26 |
+
return "No matching Information found."
|
| 27 |
+
|
| 28 |
+
|
tools.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import DuckDuckGoSearchTool
|
| 2 |
+
from smolagents import Tool
|
| 3 |
+
import random
|
| 4 |
+
from huggingface_hub import list_models
|
| 5 |
+
|
| 6 |
+
search_tool = DuckDuckGoSearchTool()
|
| 7 |
+
|
| 8 |
+
class WebSearchTool(Tool):
|
| 9 |
+
name = "web_search"
|
| 10 |
+
description = "Useful for searching the web for information relevant to the question. "
|
| 11 |
+
inputs = {
|
| 12 |
+
"query": {
|
| 13 |
+
"type": "string",
|
| 14 |
+
"description": "The search query string.",
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
output_type = "string"
|
| 18 |
+
|
| 19 |
+
def forward(self, query: str) -> str:
|
| 20 |
+
return search_tool.forward(query)
|
| 21 |
+
|
| 22 |
+
class NasaImageSearchTool(Tool):
|
| 23 |
+
name = "nasa_image_search"
|
| 24 |
+
description = "Useful for searching NASA's image library for relevant images. "
|
| 25 |
+
inputs = {
|
| 26 |
+
"query": {
|
| 27 |
+
"type": "string",
|
| 28 |
+
"description": "The search query string.",
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
output_type = "string"
|
| 32 |
+
|
| 33 |
+
def forward(self, query: str) -> str:
|
| 34 |
+
# This is a placeholder implementation. You would replace this with actual API calls to NASA's image search.
|
| 35 |
+
return f"Image results for '{query}' from NASA's image library."
|
| 36 |
+
|
| 37 |
+
class ClinicalTrialsSearchTool(Tool):
|
| 38 |
+
name = "clinical_trials_search"
|
| 39 |
+
description = "Useful for searching clinical trial databases for relevant studies. "
|
| 40 |
+
inputs = {
|
| 41 |
+
"query": {
|
| 42 |
+
"type": "string",
|
| 43 |
+
"description": "The search query string.",
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
output_type = "string"
|
| 47 |
+
|
| 48 |
+
def forward(self, query: str) -> str:
|
| 49 |
+
# This is a placeholder implementation. You would replace this with actual API calls to clinical trial databases.
|
| 50 |
+
return f"Clinical trial results for '{query}' from clinical trial databases."
|
| 51 |
+
|
| 52 |
+
class WikipediaSearchTool(Tool):
|
| 53 |
+
name = "wikipedia_search"
|
| 54 |
+
description = "Useful for searching Wikipedia for relevant information. "
|
| 55 |
+
inputs = {
|
| 56 |
+
"query": {
|
| 57 |
+
"type": "string",
|
| 58 |
+
"description": "The search query string.",
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
output_type = "string"
|
| 62 |
+
|
| 63 |
+
def forward(self, query: str) -> str:
|
| 64 |
+
# This is a placeholder implementation. You would replace this with actual API calls to Wikipedia's search.
|
| 65 |
+
return f"Wikipedia results for '{query}' from Wikipedia's search."
|