Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,45 +5,53 @@ from typing import List, Dict
|
|
| 5 |
from ingest.cia_reading_room import CIAAdapter
|
| 6 |
from ingest.fbi_vault import FBIAdapter
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
]
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 20 |
|
| 21 |
-
flat
|
| 22 |
for r in results:
|
| 23 |
-
if isinstance(r,
|
| 24 |
-
|
| 25 |
-
flat.extend(r)
|
| 26 |
-
|
| 27 |
return flat
|
| 28 |
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def federated_search(query: str) -> List[Dict]:
|
| 34 |
-
loop = asyncio.get_event_loop()
|
| 35 |
-
if loop.is_running():
|
| 36 |
-
# HF / Gradio case
|
| 37 |
-
return asyncio.create_task(federated_search_async(query))
|
| 38 |
else:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
# -------------------------
|
| 43 |
-
# UI helpers
|
| 44 |
-
# -------------------------
|
| 45 |
-
def format_result(r: Dict) -> str:
|
| 46 |
-
badge = "🟢 LIVE" if r.get("live") else "⚪ STUB"
|
| 47 |
return f"""
|
| 48 |
### {r['title']}
|
| 49 |
**{badge} · {r['source']}**
|
|
@@ -54,12 +62,15 @@ def format_result(r: Dict) -> str:
|
|
| 54 |
"""
|
| 55 |
|
| 56 |
|
| 57 |
-
def run_search(query: str, hide_stubs: bool):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
if hide_stubs:
|
| 65 |
results = [r for r in results if r.get("live")]
|
|
@@ -70,26 +81,26 @@ def run_search(query: str, hide_stubs: bool):
|
|
| 70 |
return "\n\n---\n\n".join(format_result(r) for r in results)
|
| 71 |
|
| 72 |
|
| 73 |
-
# -------------------------
|
| 74 |
-
# Gradio UI
|
| 75 |
-
# -------------------------
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
gr.Markdown("# 📜 FOIA Federated Search")
|
| 78 |
gr.Markdown(
|
| 79 |
-
"Search across public government FOIA reading rooms
|
| 80 |
-
"
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
-
query = gr.Textbox(label="Search term"
|
| 84 |
-
hide_stubs = gr.Checkbox(
|
| 85 |
-
|
|
|
|
| 86 |
value=False
|
| 87 |
)
|
| 88 |
output = gr.Markdown()
|
| 89 |
|
| 90 |
gr.Button("Search").click(
|
| 91 |
run_search,
|
| 92 |
-
inputs=[query, hide_stubs],
|
| 93 |
outputs=output
|
| 94 |
)
|
| 95 |
|
|
|
|
| 5 |
from ingest.cia_reading_room import CIAAdapter
|
| 6 |
from ingest.fbi_vault import FBIAdapter
|
| 7 |
|
| 8 |
+
from ingest.cia_extended import CIAExtendedAdapter
|
| 9 |
+
from ingest.nsa_extended import NSAExtendedAdapter
|
| 10 |
+
from ingest.nro_extended import NROExtendedAdapter
|
| 11 |
+
from ingest.dod_special_extended import (
|
| 12 |
+
AATIPExtendedAdapter,
|
| 13 |
+
SAPExtendedAdapter,
|
| 14 |
+
TENCAPExtendedAdapter,
|
| 15 |
+
SpecialActivitiesExtendedAdapter
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
BASE_ADAPTERS = [
|
| 19 |
+
CIAAdapter(), # stub
|
| 20 |
+
FBIAdapter(), # live
|
| 21 |
]
|
| 22 |
|
| 23 |
+
EXTENDED_ADAPTERS = [
|
| 24 |
+
CIAExtendedAdapter(),
|
| 25 |
+
NSAExtendedAdapter(),
|
| 26 |
+
NROExtendedAdapter(),
|
| 27 |
+
AATIPExtendedAdapter(),
|
| 28 |
+
SAPExtendedAdapter(),
|
| 29 |
+
TENCAPExtendedAdapter(),
|
| 30 |
+
SpecialActivitiesExtendedAdapter()
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
async def federated_search_async(query: str, extended_mode: bool) -> List[Dict]:
|
| 35 |
+
adapters = list(BASE_ADAPTERS)
|
| 36 |
+
if extended_mode:
|
| 37 |
+
adapters.extend(EXTENDED_ADAPTERS)
|
| 38 |
+
|
| 39 |
+
tasks = [a.search(query) for a in adapters]
|
| 40 |
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 41 |
|
| 42 |
+
flat = []
|
| 43 |
for r in results:
|
| 44 |
+
if isinstance(r, list):
|
| 45 |
+
flat.extend(r)
|
|
|
|
|
|
|
| 46 |
return flat
|
| 47 |
|
| 48 |
|
| 49 |
+
def format_result(r: Dict) -> str:
|
| 50 |
+
if r.get("extended"):
|
| 51 |
+
badge = "🟣 LIVE (Extended)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
+
badge = "🟢 LIVE" if r.get("live") else "⚪ STUB"
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return f"""
|
| 56 |
### {r['title']}
|
| 57 |
**{badge} · {r['source']}**
|
|
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
|
| 65 |
+
def run_search(query: str, hide_stubs: bool, extended_mode: bool):
|
| 66 |
+
loop = asyncio.get_event_loop()
|
| 67 |
+
if loop.is_running():
|
| 68 |
+
task = asyncio.create_task(federated_search_async(query, extended_mode))
|
| 69 |
+
results = loop.run_until_complete(task)
|
| 70 |
+
else:
|
| 71 |
+
results = loop.run_until_complete(
|
| 72 |
+
federated_search_async(query, extended_mode)
|
| 73 |
+
)
|
| 74 |
|
| 75 |
if hide_stubs:
|
| 76 |
results = [r for r in results if r.get("live")]
|
|
|
|
| 81 |
return "\n\n---\n\n".join(format_result(r) for r in results)
|
| 82 |
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
gr.Markdown("# 📜 FOIA Federated Search")
|
| 86 |
gr.Markdown(
|
| 87 |
+
"Search across public government FOIA reading rooms.\n\n"
|
| 88 |
+
"- ⚪ Stub = placeholder\n"
|
| 89 |
+
"- 🟢 Live = public indexed source\n"
|
| 90 |
+
"- 🟣 Extended = advanced public sources (opt-in)"
|
| 91 |
)
|
| 92 |
|
| 93 |
+
query = gr.Textbox(label="Search term")
|
| 94 |
+
hide_stubs = gr.Checkbox(label="Hide stub sources", value=False)
|
| 95 |
+
extended_mode = gr.Checkbox(
|
| 96 |
+
label="Enable Extended Features (advanced public agencies)",
|
| 97 |
value=False
|
| 98 |
)
|
| 99 |
output = gr.Markdown()
|
| 100 |
|
| 101 |
gr.Button("Search").click(
|
| 102 |
run_search,
|
| 103 |
+
inputs=[query, hide_stubs, extended_mode],
|
| 104 |
outputs=output
|
| 105 |
)
|
| 106 |
|