updating function calls
Browse files- README.md +1 -1
- app.py +28 -28
- mcp_functions/census_api_calls.py +7 -3
- mcp_functions/census_api_docs.py +3 -3
- mcp_functions/census_utils.py +1 -1
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title: Mcp Census
|
3 |
emoji: π’
|
4 |
colorFrom: red
|
5 |
colorTo: gray
|
|
|
1 |
---
|
2 |
+
title: ποΈ Mcp Census
|
3 |
emoji: π’
|
4 |
colorFrom: red
|
5 |
colorTo: gray
|
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
from mcp_functions.census_api_calls import (
|
3 |
-
|
4 |
-
|
5 |
)
|
6 |
from mcp_functions.census_api_docs import (
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
)
|
11 |
from mcp_functions.census_utils import (
|
12 |
-
|
13 |
required_geograpy_hierarchy_parents,
|
14 |
)
|
15 |
|
16 |
-
|
17 |
-
fn=
|
18 |
inputs=["textbox"],
|
19 |
outputs=gr.JSON(),
|
20 |
title="U.S. Census Bureau 2020 demographic and housing characteristics documentation",
|
21 |
description="Fetches demographic and housing characteristics documentation for the 2020 U.S. Census Bureau decennial census.",
|
22 |
)
|
23 |
|
24 |
-
|
25 |
-
fn=
|
26 |
inputs=["textbox", "textbox", "textbox"],
|
27 |
outputs=gr.JSON(),
|
28 |
title="U.S. Census Bureau 2020 Demographic Profile data",
|
29 |
description="Fetches demographic profile data from the 2020 U.S. Census Bureau decennial API.",
|
30 |
)
|
31 |
|
32 |
-
|
33 |
-
fn=
|
34 |
inputs=["textbox", "textbox", "textbox"],
|
35 |
outputs=gr.JSON(),
|
36 |
title="FIPS code lookup for the U.S. Census Bureau 2020 decennial census demographic profile dataset",
|
37 |
description="Lookup FIPS codes for geography hierarchies provided by the U.S. Census Bureau 2020 decennial census demographic profile dataset",
|
38 |
)
|
39 |
|
40 |
-
|
41 |
fn=required_geograpy_hierarchy_parents,
|
42 |
inputs=["textbox"],
|
43 |
outputs=gr.JSON(),
|
@@ -45,16 +45,16 @@ decennial_2020_demographic_profile_geographies_required_parent_geographies_inter
|
|
45 |
description="Utility function that provides required parent geographies when requesting geography hierarchies during U.S. Census Bureau 2020 decennial census demographic profile API calls",
|
46 |
)
|
47 |
|
48 |
-
|
49 |
-
fn=
|
50 |
inputs=[],
|
51 |
outputs=gr.TextArea(),
|
52 |
title="U.S. Census Bureau 2020 decennial census demographic profile dataset geographies",
|
53 |
description="Information on available geographies for the the U.S. Census Bureau 2020 decennial census demographic profile API.",
|
54 |
)
|
55 |
|
56 |
-
|
57 |
-
fn=
|
58 |
inputs=[],
|
59 |
outputs=gr.TextArea(),
|
60 |
title="U.S. Census Bureau 2020 decennial census demographic profile dataset variables",
|
@@ -62,8 +62,8 @@ decennial_2020_demographic_profile_variables_interface = gr.Interface(
|
|
62 |
)
|
63 |
|
64 |
|
65 |
-
|
66 |
-
fn=
|
67 |
inputs=[],
|
68 |
outputs=gr.TextArea(),
|
69 |
title="U.S. Census Bureau 2020 decennial census datasets",
|
@@ -71,15 +71,15 @@ decennial_2020_datasets_homepage_interface = gr.Interface(
|
|
71 |
)
|
72 |
|
73 |
|
74 |
-
|
75 |
[
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
],
|
84 |
[
|
85 |
"2020 U.S. Census Bureau decennial census API Homepage",
|
@@ -94,4 +94,4 @@ demo = gr.TabbedInterface(
|
|
94 |
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from mcp_functions.census_api_calls import (
|
3 |
+
dec2020_dp,
|
4 |
+
dec2020_dp_fips_lookup,
|
5 |
)
|
6 |
from mcp_functions.census_api_docs import (
|
7 |
+
import_dec2020_datasets_homepage,
|
8 |
+
import_dec2020_dp_geographies,
|
9 |
+
import_dec2020_dp_variables,
|
10 |
)
|
11 |
from mcp_functions.census_utils import (
|
12 |
+
dec2020_dhc_semantic_search,
|
13 |
required_geograpy_hierarchy_parents,
|
14 |
)
|
15 |
|
16 |
+
dec2020_dhc_semantic_search_interface = gr.Interface(
|
17 |
+
fn=dec2020_dhc_semantic_search,
|
18 |
inputs=["textbox"],
|
19 |
outputs=gr.JSON(),
|
20 |
title="U.S. Census Bureau 2020 demographic and housing characteristics documentation",
|
21 |
description="Fetches demographic and housing characteristics documentation for the 2020 U.S. Census Bureau decennial census.",
|
22 |
)
|
23 |
|
24 |
+
dec2020_dp_interface = gr.Interface(
|
25 |
+
fn=dec2020_dp,
|
26 |
inputs=["textbox", "textbox", "textbox"],
|
27 |
outputs=gr.JSON(),
|
28 |
title="U.S. Census Bureau 2020 Demographic Profile data",
|
29 |
description="Fetches demographic profile data from the 2020 U.S. Census Bureau decennial API.",
|
30 |
)
|
31 |
|
32 |
+
dec2020_dp_fips_lookup_interface = gr.Interface(
|
33 |
+
fn=dec2020_dp_fips_lookup,
|
34 |
inputs=["textbox", "textbox", "textbox"],
|
35 |
outputs=gr.JSON(),
|
36 |
title="FIPS code lookup for the U.S. Census Bureau 2020 decennial census demographic profile dataset",
|
37 |
description="Lookup FIPS codes for geography hierarchies provided by the U.S. Census Bureau 2020 decennial census demographic profile dataset",
|
38 |
)
|
39 |
|
40 |
+
dec2020_dp_geographies_required_interface = gr.Interface(
|
41 |
fn=required_geograpy_hierarchy_parents,
|
42 |
inputs=["textbox"],
|
43 |
outputs=gr.JSON(),
|
|
|
45 |
description="Utility function that provides required parent geographies when requesting geography hierarchies during U.S. Census Bureau 2020 decennial census demographic profile API calls",
|
46 |
)
|
47 |
|
48 |
+
dec2020_dp_geographies_interface = gr.Interface(
|
49 |
+
fn=import_dec2020_dp_geographies,
|
50 |
inputs=[],
|
51 |
outputs=gr.TextArea(),
|
52 |
title="U.S. Census Bureau 2020 decennial census demographic profile dataset geographies",
|
53 |
description="Information on available geographies for the the U.S. Census Bureau 2020 decennial census demographic profile API.",
|
54 |
)
|
55 |
|
56 |
+
dec2020_dp_variables_interface = gr.Interface(
|
57 |
+
fn=import_dec2020_dp_variables,
|
58 |
inputs=[],
|
59 |
outputs=gr.TextArea(),
|
60 |
title="U.S. Census Bureau 2020 decennial census demographic profile dataset variables",
|
|
|
62 |
)
|
63 |
|
64 |
|
65 |
+
dec2020_datasets_homepage_interface = gr.Interface(
|
66 |
+
fn=import_dec2020_datasets_homepage,
|
67 |
inputs=[],
|
68 |
outputs=gr.TextArea(),
|
69 |
title="U.S. Census Bureau 2020 decennial census datasets",
|
|
|
71 |
)
|
72 |
|
73 |
|
74 |
+
mcp_server_demo = gr.TabbedInterface(
|
75 |
[
|
76 |
+
dec2020_datasets_homepage_interface,
|
77 |
+
dec2020_dp_geographies_interface,
|
78 |
+
dec2020_dp_variables_interface,
|
79 |
+
dec2020_dp_geographies_required_interface,
|
80 |
+
dec2020_dp_fips_lookup_interface,
|
81 |
+
dec2020_dp_interface,
|
82 |
+
dec2020_dhc_semantic_search_interface,
|
83 |
],
|
84 |
[
|
85 |
"2020 U.S. Census Bureau decennial census API Homepage",
|
|
|
94 |
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
+
mcp_server_demo.launch(mcp_server=True)
|
mcp_functions/census_api_calls.py
CHANGED
@@ -41,7 +41,7 @@ if not API_KEY:
|
|
41 |
raise ValueError("Set a CENSUS_API_KEY environment variable")
|
42 |
|
43 |
|
44 |
-
def
|
45 |
geography_hierarchy: str,
|
46 |
name: str,
|
47 |
required_parent_geographies: str,
|
@@ -86,10 +86,14 @@ def decennial_2020_demographic_profile_fips_lookup(
|
|
86 |
data = response.json()
|
87 |
except requests.RequestException as e:
|
88 |
if error_text == "error: unknown/unsupported geography hierarchy":
|
|
|
|
|
89 |
raise ValueError(
|
90 |
"Invalid geography hierarchy provided.",
|
91 |
"Acceptable required_parent_geographies must be provided.",
|
92 |
-
f"{geography_hierarchy} requires the following parent geographies: {find_required_parent_geographies(
|
|
|
|
|
93 |
)
|
94 |
raise RuntimeError(f"Failed to fetch data from the Census API: {e} ") from e
|
95 |
|
@@ -108,7 +112,7 @@ def decennial_2020_demographic_profile_fips_lookup(
|
|
108 |
)
|
109 |
|
110 |
|
111 |
-
def
|
112 |
get_variables: str, for_clauses: str, in_clauses: str
|
113 |
):
|
114 |
"""
|
|
|
41 |
raise ValueError("Set a CENSUS_API_KEY environment variable")
|
42 |
|
43 |
|
44 |
+
def dec2020_dp_fips_lookup(
|
45 |
geography_hierarchy: str,
|
46 |
name: str,
|
47 |
required_parent_geographies: str,
|
|
|
86 |
data = response.json()
|
87 |
except requests.RequestException as e:
|
88 |
if error_text == "error: unknown/unsupported geography hierarchy":
|
89 |
+
# TODO: This error gets raise both if a parent geography is missing AND if the requested
|
90 |
+
# geography is invalid. We need to impove this raised error to help parse the difference.
|
91 |
raise ValueError(
|
92 |
"Invalid geography hierarchy provided.",
|
93 |
"Acceptable required_parent_geographies must be provided.",
|
94 |
+
f"{geography_hierarchy} requires the following parent geographies: {find_required_parent_geographies(
|
95 |
+
geography_hierarchy
|
96 |
+
)}",
|
97 |
)
|
98 |
raise RuntimeError(f"Failed to fetch data from the Census API: {e} ") from e
|
99 |
|
|
|
112 |
)
|
113 |
|
114 |
|
115 |
+
def dec2020_dp(
|
116 |
get_variables: str, for_clauses: str, in_clauses: str
|
117 |
):
|
118 |
"""
|
mcp_functions/census_api_docs.py
CHANGED
@@ -2,7 +2,7 @@ import requests
|
|
2 |
from markdownify import markdownify
|
3 |
|
4 |
|
5 |
-
def
|
6 |
"""
|
7 |
Fetches the homepage for the the U.S. Census Bureau 2020 decennial census API housed at https://api.census.gov/data/2020/dec/dp.html.
|
8 |
Contains descriptions of available datasets.
|
@@ -19,7 +19,7 @@ def import_decennial_2020_datasets_homepage() -> str:
|
|
19 |
return markdownify(response.text.strip())
|
20 |
|
21 |
|
22 |
-
def
|
23 |
"""
|
24 |
Fetches information on available geographies for the the U.S. Census Bureau 2020 decennial census demographic profile API housed at https://api.census.gov/data/2020/dec/dp/geography.html.
|
25 |
Includes:
|
@@ -37,7 +37,7 @@ def import_decennial_2020_demographic_profile_geographies() -> str:
|
|
37 |
return markdownify(response.text.strip())
|
38 |
|
39 |
|
40 |
-
def
|
41 |
"""
|
42 |
Fetches information on available variables for the the U.S. Census Bureau 2020 decennial census demographic profile API housed at https://api.census.gov/data/2020/dec/dp/variables.html.
|
43 |
* "Name" -- used to access variable during API calls
|
|
|
2 |
from markdownify import markdownify
|
3 |
|
4 |
|
5 |
+
def import_dec2020_datasets_homepage() -> str:
|
6 |
"""
|
7 |
Fetches the homepage for the the U.S. Census Bureau 2020 decennial census API housed at https://api.census.gov/data/2020/dec/dp.html.
|
8 |
Contains descriptions of available datasets.
|
|
|
19 |
return markdownify(response.text.strip())
|
20 |
|
21 |
|
22 |
+
def import_dec2020_dp_geographies() -> str:
|
23 |
"""
|
24 |
Fetches information on available geographies for the the U.S. Census Bureau 2020 decennial census demographic profile API housed at https://api.census.gov/data/2020/dec/dp/geography.html.
|
25 |
Includes:
|
|
|
37 |
return markdownify(response.text.strip())
|
38 |
|
39 |
|
40 |
+
def import_dec2020_dp_variables() -> str:
|
41 |
"""
|
42 |
Fetches information on available variables for the the U.S. Census Bureau 2020 decennial census demographic profile API housed at https://api.census.gov/data/2020/dec/dp/variables.html.
|
43 |
* "Name" -- used to access variable during API calls
|
mcp_functions/census_utils.py
CHANGED
@@ -20,7 +20,7 @@ def required_geograpy_hierarchy_parents(geography_hierarchy: str) -> List[str |
|
|
20 |
return find_required_parent_geographies(geography_hierarchy)
|
21 |
|
22 |
|
23 |
-
def
|
24 |
query: str,
|
25 |
) -> List[Document]:
|
26 |
"""
|
|
|
20 |
return find_required_parent_geographies(geography_hierarchy)
|
21 |
|
22 |
|
23 |
+
def dec2020_dhc_semantic_search(
|
24 |
query: str,
|
25 |
) -> List[Document]:
|
26 |
"""
|