Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from typing import List, Dict
|
|
|
|
| 3 |
from ingest.registry import get_all_adapters
|
| 4 |
from ingest.export import export_results
|
| 5 |
from ingest.health import get_adapter_health
|
|
@@ -11,21 +12,32 @@ def run_search(
|
|
| 11 |
query: str,
|
| 12 |
include_stubs: bool,
|
| 13 |
enable_extended: bool,
|
| 14 |
-
|
| 15 |
) -> List[Dict]:
|
| 16 |
-
log_event("search", {"
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
for adapter in ALL_ADAPTERS:
|
|
|
|
| 20 |
if not include_stubs and not adapter.is_live:
|
| 21 |
continue
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
if adapter.is_extended
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
results.append({
|
| 31 |
"agency": adapter.name,
|
|
@@ -33,68 +45,109 @@ def run_search(
|
|
| 33 |
"snippet": str(e),
|
| 34 |
"url": "",
|
| 35 |
"status": "⚠️ Error",
|
| 36 |
-
"exportable": False
|
| 37 |
})
|
| 38 |
|
| 39 |
return results
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if not results:
|
| 43 |
return gr.File.update(visible=False)
|
| 44 |
|
| 45 |
exportable = [r for r in results if r.get("exportable")]
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
with gr.Blocks() as app:
|
| 52 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Markdown(
|
| 54 |
"ℹ️ **Stub results are informational and cannot be exported.**\n\n"
|
| 55 |
-
"Extended sources require explicit consent before searching."
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
label="I understand some agencies block automated access",
|
| 63 |
-
|
| 64 |
)
|
| 65 |
|
| 66 |
search_btn = gr.Button("Search")
|
| 67 |
-
export_btn = gr.Button("Export Results (ZIP)", interactive=False)
|
| 68 |
|
| 69 |
results_state = gr.State([])
|
| 70 |
results_table = gr.Dataframe(
|
| 71 |
-
headers=["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
interactive=False
|
| 73 |
)
|
| 74 |
export_file = gr.File(visible=False)
|
| 75 |
|
| 76 |
search_btn.click(
|
| 77 |
fn=run_search,
|
| 78 |
-
inputs=[
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
).then(
|
| 81 |
fn=lambda r: (
|
| 82 |
-
r,
|
| 83 |
gr.Button.update(
|
| 84 |
interactive=any(x.get("exportable") for x in r)
|
| 85 |
-
)
|
| 86 |
),
|
| 87 |
-
inputs=
|
| 88 |
-
outputs=[results_table, export_btn]
|
| 89 |
)
|
| 90 |
|
| 91 |
export_btn.click(
|
| 92 |
-
fn=
|
| 93 |
-
inputs=
|
| 94 |
-
outputs=
|
| 95 |
)
|
| 96 |
|
| 97 |
-
gr.Markdown("##
|
| 98 |
gr.JSON(get_adapter_health())
|
| 99 |
|
| 100 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from typing import List, Dict
|
| 3 |
+
|
| 4 |
from ingest.registry import get_all_adapters
|
| 5 |
from ingest.export import export_results
|
| 6 |
from ingest.health import get_adapter_health
|
|
|
|
| 12 |
query: str,
|
| 13 |
include_stubs: bool,
|
| 14 |
enable_extended: bool,
|
| 15 |
+
acknowledge_extended: bool,
|
| 16 |
) -> List[Dict]:
|
| 17 |
+
log_event("search", {"query_length": len(query or "")})
|
| 18 |
+
|
| 19 |
+
if not query:
|
| 20 |
+
return []
|
| 21 |
+
|
| 22 |
+
results: List[Dict] = []
|
| 23 |
|
| 24 |
for adapter in ALL_ADAPTERS:
|
| 25 |
+
# Skip stub adapters if not enabled
|
| 26 |
if not include_stubs and not adapter.is_live:
|
| 27 |
continue
|
| 28 |
+
|
| 29 |
+
# Extended adapters require explicit enable + acknowledgement
|
| 30 |
+
if adapter.is_extended:
|
| 31 |
+
if not enable_extended or not acknowledge_extended:
|
| 32 |
+
continue
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
adapter_results = adapter.search(query)
|
| 36 |
+
for r in adapter_results:
|
| 37 |
+
r.setdefault("agency", adapter.name)
|
| 38 |
+
r.setdefault("exportable", adapter.is_live)
|
| 39 |
+
r.setdefault("status", "🟢 Live" if adapter.is_live else "🔒 Stub")
|
| 40 |
+
results.append(r)
|
| 41 |
except Exception as e:
|
| 42 |
results.append({
|
| 43 |
"agency": adapter.name,
|
|
|
|
| 45 |
"snippet": str(e),
|
| 46 |
"url": "",
|
| 47 |
"status": "⚠️ Error",
|
| 48 |
+
"exportable": False,
|
| 49 |
})
|
| 50 |
|
| 51 |
return results
|
| 52 |
|
| 53 |
+
|
| 54 |
+
def table_from_results(results: List[Dict]):
|
| 55 |
+
rows = []
|
| 56 |
+
for r in results:
|
| 57 |
+
rows.append([
|
| 58 |
+
r.get("agency", ""),
|
| 59 |
+
r.get("status", ""),
|
| 60 |
+
r.get("title", ""),
|
| 61 |
+
r.get("snippet", ""),
|
| 62 |
+
r.get("url", ""),
|
| 63 |
+
])
|
| 64 |
+
return rows
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def export_handler(results: List[Dict]):
|
| 68 |
if not results:
|
| 69 |
return gr.File.update(visible=False)
|
| 70 |
|
| 71 |
exportable = [r for r in results if r.get("exportable")]
|
| 72 |
+
|
| 73 |
+
if not exportable:
|
| 74 |
+
return gr.File.update(visible=False)
|
| 75 |
+
|
| 76 |
+
path = export_results(exportable)
|
| 77 |
+
return gr.File.update(value=path, visible=True)
|
| 78 |
+
|
| 79 |
|
| 80 |
with gr.Blocks() as app:
|
| 81 |
+
gr.Markdown(
|
| 82 |
+
"# 🏛️ Federal FOIA Intelligence Search\n"
|
| 83 |
+
"### Public Electronic Reading Rooms Only"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
gr.Markdown(
|
| 87 |
"ℹ️ **Stub results are informational and cannot be exported.**\n\n"
|
| 88 |
+
"Extended sources require explicit user consent before searching."
|
| 89 |
)
|
| 90 |
|
| 91 |
+
with gr.Row():
|
| 92 |
+
query = gr.Textbox(label="Search query", placeholder="e.g. MKULTRA")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
include_stubs = gr.Checkbox(
|
| 96 |
+
label="Include Stub Results",
|
| 97 |
+
value=True
|
| 98 |
+
)
|
| 99 |
+
enable_extended = gr.Checkbox(
|
| 100 |
+
label="Enable Extended Coverage (opt-in)",
|
| 101 |
+
value=False
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
acknowledge_extended = gr.Checkbox(
|
| 105 |
label="I understand some agencies block automated access",
|
| 106 |
+
value=False
|
| 107 |
)
|
| 108 |
|
| 109 |
search_btn = gr.Button("Search")
|
|
|
|
| 110 |
|
| 111 |
results_state = gr.State([])
|
| 112 |
results_table = gr.Dataframe(
|
| 113 |
+
headers=["Agency", "Status", "Title", "Snippet", "URL"],
|
| 114 |
+
interactive=False,
|
| 115 |
+
wrap=True
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
export_btn = gr.Button(
|
| 119 |
+
"Export Results (ZIP)",
|
| 120 |
interactive=False
|
| 121 |
)
|
| 122 |
export_file = gr.File(visible=False)
|
| 123 |
|
| 124 |
search_btn.click(
|
| 125 |
fn=run_search,
|
| 126 |
+
inputs=[
|
| 127 |
+
query,
|
| 128 |
+
include_stubs,
|
| 129 |
+
enable_extended,
|
| 130 |
+
acknowledge_extended,
|
| 131 |
+
],
|
| 132 |
+
outputs=results_state,
|
| 133 |
).then(
|
| 134 |
fn=lambda r: (
|
| 135 |
+
table_from_results(r),
|
| 136 |
gr.Button.update(
|
| 137 |
interactive=any(x.get("exportable") for x in r)
|
| 138 |
+
),
|
| 139 |
),
|
| 140 |
+
inputs=results_state,
|
| 141 |
+
outputs=[results_table, export_btn],
|
| 142 |
)
|
| 143 |
|
| 144 |
export_btn.click(
|
| 145 |
+
fn=export_handler,
|
| 146 |
+
inputs=results_state,
|
| 147 |
+
outputs=export_file,
|
| 148 |
)
|
| 149 |
|
| 150 |
+
gr.Markdown("## 🔍 Adapter Health")
|
| 151 |
gr.JSON(get_adapter_health())
|
| 152 |
|
| 153 |
app.launch()
|