Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,12 +10,21 @@ from datetime import datetime
|
|
| 10 |
|
| 11 |
from ingest.registry import get_enabled_adapters
|
| 12 |
from ingest.cluster import build_embeddings, cluster_embeddings
|
|
|
|
| 13 |
from ingest.health import get_health_snapshot
|
| 14 |
-
from ingest.
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
from ingest.timeline import build_timeline_figure
|
| 18 |
from ingest.graph import build_cluster_graph
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
TITLE = "ποΈ Federal FOIA Intelligence Search"
|
|
@@ -24,7 +33,7 @@ SUBTITLE = "Public Electronic Reading Rooms Only"
|
|
| 24 |
|
| 25 |
# ---------------- Async-safe search ----------------
|
| 26 |
|
| 27 |
-
async def _run_search(query
|
| 28 |
adapters = get_enabled_adapters(enable_live=enable_live)
|
| 29 |
tasks = [a.search(query) for a in adapters]
|
| 30 |
responses = await asyncio.gather(*tasks, return_exceptions=True)
|
|
@@ -44,18 +53,22 @@ async def _run_search(query: str, enable_live: bool, hide_stub: bool):
|
|
| 44 |
r["source"] = adapter.source_name
|
| 45 |
r["latency"] = adapter.last_latency
|
| 46 |
r["exemptions"] = classify_foia_exemptions(r.get("snippet", ""))
|
| 47 |
-
r["citation"] =
|
| 48 |
r["date"] = r.get("date", datetime.utcnow().isoformat())
|
| 49 |
results.append(r)
|
| 50 |
|
| 51 |
embeddings = build_embeddings(results)
|
| 52 |
clusters = cluster_embeddings(results, embeddings)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
def search_ui(query, enable_live, hide_stub):
|
|
@@ -65,10 +78,11 @@ def search_ui(query, enable_live, hide_stub):
|
|
| 65 |
(
|
| 66 |
results,
|
| 67 |
clusters,
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
cluster_graph,
|
| 71 |
-
timeline_fig
|
| 72 |
) = loop.run_until_complete(
|
| 73 |
_run_search(query, enable_live, hide_stub)
|
| 74 |
)
|
|
@@ -81,10 +95,10 @@ def search_ui(query, enable_live, hide_stub):
|
|
| 81 |
r["url"],
|
| 82 |
r.get("snippet", ""),
|
| 83 |
",".join(r["exemptions"]),
|
| 84 |
-
|
| 85 |
])
|
| 86 |
|
| 87 |
-
return table,
|
| 88 |
|
| 89 |
|
| 90 |
# ---------------- UI ----------------
|
|
@@ -92,10 +106,8 @@ def search_ui(query, enable_live, hide_stub):
|
|
| 92 |
with gr.Blocks(title="Federal FOIA Intelligence Search") as demo:
|
| 93 |
gr.Markdown(f"# {TITLE}")
|
| 94 |
gr.Markdown(f"### {SUBTITLE}")
|
| 95 |
-
|
| 96 |
gr.Markdown(
|
| 97 |
-
"**Terms of Use:** Indexes only documents already released "
|
| 98 |
-
"via official FOIA Electronic Reading Rooms."
|
| 99 |
)
|
| 100 |
|
| 101 |
with gr.Row():
|
|
@@ -107,26 +119,35 @@ with gr.Blocks(title="Federal FOIA Intelligence Search") as demo:
|
|
| 107 |
hide_stub = gr.Checkbox(label="Hide Stub Sources", value=True)
|
| 108 |
|
| 109 |
results_table = gr.Dataframe(
|
| 110 |
-
headers=["Agency", "Title", "URL", "Snippet", "FOIA Exemptions", "
|
| 111 |
-
wrap=True
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
with gr.Tab("π Coverage Heatmap"):
|
| 116 |
-
coverage_box = gr.JSON()
|
| 117 |
|
| 118 |
-
|
|
|
|
| 119 |
cluster_plot = gr.Plot()
|
| 120 |
|
| 121 |
-
with gr.Tab("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
timeline_plot = gr.Plot()
|
| 123 |
|
| 124 |
-
with gr.Tab("
|
|
|
|
|
|
|
|
|
|
| 125 |
health_box = gr.JSON(value=get_health_snapshot())
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
-
export_zip_btn = gr.Button("π§Ύ Journalist ZIP
|
| 129 |
-
export_pdf_btn = gr.Button("π
|
|
|
|
| 130 |
|
| 131 |
export_status = gr.Textbox(label="Export Status")
|
| 132 |
|
|
@@ -135,23 +156,21 @@ with gr.Blocks(title="Federal FOIA Intelligence Search") as demo:
|
|
| 135 |
inputs=[query, enable_live, hide_stub],
|
| 136 |
outputs=[
|
| 137 |
results_table,
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
cluster_plot,
|
| 141 |
-
|
| 142 |
-
]
|
| 143 |
)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
outputs=[export_status]
|
| 149 |
)
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
outputs=[export_status]
|
| 155 |
-
)
|
| 156 |
|
| 157 |
demo.queue().launch()
|
|
|
|
| 10 |
|
| 11 |
from ingest.registry import get_enabled_adapters
|
| 12 |
from ingest.cluster import build_embeddings, cluster_embeddings
|
| 13 |
+
from ingest.coverage import build_coverage_heatmap, build_agency_map
|
| 14 |
from ingest.health import get_health_snapshot
|
| 15 |
+
from ingest.export import (
|
| 16 |
+
export_journalist_zip,
|
| 17 |
+
export_pdf_report,
|
| 18 |
+
export_congressional_briefing,
|
| 19 |
+
)
|
| 20 |
+
from ingest.utils import (
|
| 21 |
+
highlight_terms,
|
| 22 |
+
classify_foia_exemptions,
|
| 23 |
+
format_bluebook_citation,
|
| 24 |
+
)
|
| 25 |
from ingest.timeline import build_timeline_figure
|
| 26 |
from ingest.graph import build_cluster_graph
|
| 27 |
+
from ingest.trends import build_exemption_trend_chart
|
| 28 |
|
| 29 |
|
| 30 |
TITLE = "ποΈ Federal FOIA Intelligence Search"
|
|
|
|
| 33 |
|
| 34 |
# ---------------- Async-safe search ----------------
|
| 35 |
|
| 36 |
+
async def _run_search(query, enable_live, hide_stub):
|
| 37 |
adapters = get_enabled_adapters(enable_live=enable_live)
|
| 38 |
tasks = [a.search(query) for a in adapters]
|
| 39 |
responses = await asyncio.gather(*tasks, return_exceptions=True)
|
|
|
|
| 53 |
r["source"] = adapter.source_name
|
| 54 |
r["latency"] = adapter.last_latency
|
| 55 |
r["exemptions"] = classify_foia_exemptions(r.get("snippet", ""))
|
| 56 |
+
r["citation"] = format_bluebook_citation(r)
|
| 57 |
r["date"] = r.get("date", datetime.utcnow().isoformat())
|
| 58 |
results.append(r)
|
| 59 |
|
| 60 |
embeddings = build_embeddings(results)
|
| 61 |
clusters = cluster_embeddings(results, embeddings)
|
| 62 |
|
| 63 |
+
return (
|
| 64 |
+
results,
|
| 65 |
+
clusters,
|
| 66 |
+
build_coverage_heatmap(per_agency_counts),
|
| 67 |
+
build_agency_map(per_agency_counts),
|
| 68 |
+
build_timeline_figure(results),
|
| 69 |
+
build_exemption_trend_chart(results),
|
| 70 |
+
build_cluster_graph(results, clusters),
|
| 71 |
+
)
|
| 72 |
|
| 73 |
|
| 74 |
def search_ui(query, enable_live, hide_stub):
|
|
|
|
| 78 |
(
|
| 79 |
results,
|
| 80 |
clusters,
|
| 81 |
+
heatmap,
|
| 82 |
+
agency_map,
|
| 83 |
+
timeline,
|
| 84 |
+
exemption_trends,
|
| 85 |
cluster_graph,
|
|
|
|
| 86 |
) = loop.run_until_complete(
|
| 87 |
_run_search(query, enable_live, hide_stub)
|
| 88 |
)
|
|
|
|
| 95 |
r["url"],
|
| 96 |
r.get("snippet", ""),
|
| 97 |
",".join(r["exemptions"]),
|
| 98 |
+
r["citation"],
|
| 99 |
])
|
| 100 |
|
| 101 |
+
return table, heatmap, agency_map, timeline, exemption_trends, cluster_graph
|
| 102 |
|
| 103 |
|
| 104 |
# ---------------- UI ----------------
|
|
|
|
| 106 |
with gr.Blocks(title="Federal FOIA Intelligence Search") as demo:
|
| 107 |
gr.Markdown(f"# {TITLE}")
|
| 108 |
gr.Markdown(f"### {SUBTITLE}")
|
|
|
|
| 109 |
gr.Markdown(
|
| 110 |
+
"**Terms of Use:** Indexes only documents already released via official FOIA Electronic Reading Rooms."
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
with gr.Row():
|
|
|
|
| 119 |
hide_stub = gr.Checkbox(label="Hide Stub Sources", value=True)
|
| 120 |
|
| 121 |
results_table = gr.Dataframe(
|
| 122 |
+
headers=["Agency", "Title", "URL", "Snippet", "FOIA Exemptions", "Citation"],
|
| 123 |
+
wrap=True,
|
| 124 |
)
|
| 125 |
|
| 126 |
+
preview_box = gr.Markdown(label="π Document Preview")
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
with gr.Tabs():
|
| 129 |
+
with gr.Tab("π§ Cluster Graph"):
|
| 130 |
cluster_plot = gr.Plot()
|
| 131 |
|
| 132 |
+
with gr.Tab("πΊοΈ Agency Coverage Map"):
|
| 133 |
+
agency_map_plot = gr.Plot()
|
| 134 |
+
|
| 135 |
+
with gr.Tab("π Coverage Heatmap"):
|
| 136 |
+
heatmap_box = gr.JSON()
|
| 137 |
+
|
| 138 |
+
with gr.Tab("ποΈ Timeline"):
|
| 139 |
timeline_plot = gr.Plot()
|
| 140 |
|
| 141 |
+
with gr.Tab("βοΈ FOIA Exemption Trends"):
|
| 142 |
+
exemption_plot = gr.Plot()
|
| 143 |
+
|
| 144 |
+
with gr.Tab("π₯ Health"):
|
| 145 |
health_box = gr.JSON(value=get_health_snapshot())
|
| 146 |
|
| 147 |
with gr.Row():
|
| 148 |
+
export_zip_btn = gr.Button("π§Ύ Journalist ZIP")
|
| 149 |
+
export_pdf_btn = gr.Button("π Transparency PDF")
|
| 150 |
+
export_congress_btn = gr.Button("ποΈ Congressional Briefing PDF")
|
| 151 |
|
| 152 |
export_status = gr.Textbox(label="Export Status")
|
| 153 |
|
|
|
|
| 156 |
inputs=[query, enable_live, hide_stub],
|
| 157 |
outputs=[
|
| 158 |
results_table,
|
| 159 |
+
heatmap_box,
|
| 160 |
+
agency_map_plot,
|
| 161 |
+
timeline_plot,
|
| 162 |
+
exemption_plot,
|
| 163 |
cluster_plot,
|
| 164 |
+
],
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
+
results_table.select(
|
| 168 |
+
lambda df, evt: f"### {df[evt.index][1]}\n\n{df[evt.index][3]}\n\nπ {df[evt.index][2]}",
|
| 169 |
+
outputs=preview_box,
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
+
export_zip_btn.click(export_journalist_zip, results_table, export_status)
|
| 173 |
+
export_pdf_btn.click(export_pdf_report, results_table, export_status)
|
| 174 |
+
export_congress_btn.click(export_congressional_briefing, results_table, export_status)
|
|
|
|
|
|
|
| 175 |
|
| 176 |
demo.queue().launch()
|