tigerstride commited on
Commit
4c25f2f
·
verified ·
1 Parent(s): 1fb6d89

Add tool to read Philly crash data

Browse files

Add RAG to pull in Philly public crash data with Pandas

Files changed (3) hide show
  1. app.py +5 -3
  2. retriever.py +25 -1
  3. tools.py +22 -2
app.py CHANGED
@@ -5,8 +5,8 @@ import getpass
5
  from smolagents import GradioUI, CodeAgent, HfApiModel
6
 
7
  # Import our custom tools from their modules
8
- from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
9
- from retriever import load_guest_dataset
10
 
11
  #Set up in HuggingFace Space Secrets
12
  #os.environ["HF_TOKEN"] = getpass.getpass("Enter HF token:")
@@ -31,9 +31,11 @@ hub_stats_tool = HubStatsTool()
31
  # Load the guest dataset and initialize the guest info tool
32
  guest_info_tool = load_guest_dataset()
33
 
 
 
34
  # Create Alfred with all the tools
35
  alfred = CodeAgent(
36
- tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
37
  model=model,
38
  add_base_tools=True, # Add any additional base tools
39
  planning_interval=3 # Enable planning every 3 steps
 
5
  from smolagents import GradioUI, CodeAgent, HfApiModel
6
 
7
  # Import our custom tools from their modules
8
+ from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool, CrashInfoRetrieverTool
9
+ from retriever import load_guest_dataset, load_crash_data
10
 
11
  #Set up in HuggingFace Space Secrets
12
  #os.environ["HF_TOKEN"] = getpass.getpass("Enter HF token:")
 
31
  # Load the guest dataset and initialize the guest info tool
32
  guest_info_tool = load_guest_dataset()
33
 
34
+ crash_info_tool = load_crash_data()
35
+
36
  # Create Alfred with all the tools
37
  alfred = CodeAgent(
38
+ tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool, crash_info_tool],
39
  model=model,
40
  add_base_tools=True, # Add any additional base tools
41
  planning_interval=3 # Enable planning every 3 steps
retriever.py CHANGED
@@ -2,7 +2,8 @@ from smolagents import Tool
2
  from langchain_community.retrievers import BM25Retriever
3
  from langchain.docstore.document import Document
4
  import datasets
5
-
 
6
 
7
  class GuestInfoRetrieverTool(Tool):
8
  name = "guest_info_retriever"
@@ -50,4 +51,27 @@ def load_guest_dataset():
50
  return GuestInfoRetrieverTool(docs)
51
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
 
2
  from langchain_community.retrievers import BM25Retriever
3
  from langchain.docstore.document import Document
4
  import datasets
5
+ import pandas as pd
6
+ import requests
7
 
8
  class GuestInfoRetrieverTool(Tool):
9
  name = "guest_info_retriever"
 
51
  return GuestInfoRetrieverTool(docs)
52
 
53
 
54
+ def load_crash_data():
55
+ # Public Philly crash data CSV (small demo file)
56
+ url = "https://phl.carto.com/api/v2/sql?q=SELECT+*+FROM+vision_zero_crash+LIMIT+5000&format=csv"
57
+ response = requests.get(url)
58
+ with open("philly_crashes.csv", "wb") as f:
59
+ f.write(response.content)
60
+
61
+ df = pd.read_csv("philly_crashes.csv")
62
+
63
+ # Convert to LangChain documents
64
+ docs = []
65
+ for _, row in df.iterrows():
66
+ content = "\n".join([
67
+ f"Date: {row.get('crash_date', 'N/A')}",
68
+ f"Location: {row.get('location', 'N/A')}",
69
+ f"Injury Count: {row.get('injury_count', 'N/A')}",
70
+ f"Fatalities: {row.get('fatal_count', 'N/A')}",
71
+ f"Crash Type: {row.get('crash_type', 'N/A')}",
72
+ f"Weather: {row.get('weather', 'N/A')}"
73
+ ])
74
+ docs.append(Document(page_content=content, metadata={"id": row.get("id", "unknown")}))
75
+
76
+ return docs
77
 
tools.py CHANGED
@@ -10,7 +10,6 @@ from huggingface_hub import list_models
10
 
11
  class GuestInfoRetrieverTool(Tool):
12
  name = "guest_info_retriever"
13
- priority = 10 # Higher priority, prefer personal this dataset knowledge and augment with internet search
14
  description = "Retrieves detailed information about gala guests based on their name or relation."
15
  inputs = {
16
  "query": {
@@ -32,7 +31,7 @@ class GuestInfoRetrieverTool(Tool):
32
  return "No matching guest information found."
33
 
34
  # Initialize the tool
35
- # guest_info_tool = GuestInfoRetrieverTool(docs) # no, not here, in app.py
36
 
37
 
38
  class WeatherInfoTool(Tool):
@@ -81,3 +80,24 @@ class HubStatsTool(Tool):
81
  except Exception as e:
82
  return f"Error fetching models for {author}: {str(e)}"
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  class GuestInfoRetrieverTool(Tool):
12
  name = "guest_info_retriever"
 
13
  description = "Retrieves detailed information about gala guests based on their name or relation."
14
  inputs = {
15
  "query": {
 
31
  return "No matching guest information found."
32
 
33
  # Initialize the tool
34
+ # guest_info_tool = GuestInfoRetrieverTool(docs)
35
 
36
 
37
  class WeatherInfoTool(Tool):
 
80
  except Exception as e:
81
  return f"Error fetching models for {author}: {str(e)}"
82
 
83
+
84
+ class CrashInfoRetrieverTool(Tool):
85
+ name = "crash_info_tool"
86
+ description = "Retrieves traffic crash details in Philadelphia by keyword (e.g. location, date, weather)."
87
+ inputs = {
88
+ "query": {
89
+ "type": "string",
90
+ "description": "Any keyword like a date, street name, or crash type."
91
+ }
92
+ }
93
+ output_type = "string"
94
+
95
+ def __init__(self, docs):
96
+ self.retriever = BM25Retriever.from_documents(docs)
97
+
98
+ def forward(self, query: str):
99
+ results = self.retriever.get_relevant_documents(query)
100
+ if results:
101
+ return "\n\n".join([doc.page_content for doc in results[:3]])
102
+ else:
103
+ return "No crash data found matching your query."