Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,9 @@ from typing import List, Dict
|
|
| 4 |
from ingest.registry import get_all_adapters
|
| 5 |
from ingest.export import export_results
|
| 6 |
from ingest.health import get_adapter_health
|
|
|
|
|
|
|
|
|
|
| 7 |
from analytics.events import log_event
|
| 8 |
|
| 9 |
ALL_ADAPTERS = get_all_adapters()
|
|
@@ -13,7 +16,9 @@ def run_search(
|
|
| 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:
|
|
@@ -22,11 +27,9 @@ def run_search(
|
|
| 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
|
|
@@ -48,33 +51,33 @@ def run_search(
|
|
| 48 |
"exportable": False,
|
| 49 |
})
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
return results
|
| 52 |
|
| 53 |
|
| 54 |
def table_from_results(results: List[Dict]):
|
| 55 |
-
|
| 56 |
-
|
| 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 |
-
|
|
|
|
| 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 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
with gr.Blocks() as app:
|
|
@@ -85,20 +88,18 @@ with gr.Blocks() as app:
|
|
| 85 |
|
| 86 |
gr.Markdown(
|
| 87 |
"ℹ️ **Stub results are informational and cannot be exported.**\n\n"
|
| 88 |
-
"
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
query = gr.Textbox(label="Search query", placeholder="e.g. MKULTRA")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
-
include_stubs = gr.Checkbox(
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
value=False
|
| 102 |
)
|
| 103 |
|
| 104 |
acknowledge_extended = gr.Checkbox(
|
|
@@ -109,17 +110,25 @@ with gr.Blocks() as app:
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
interactive=False
|
| 121 |
)
|
| 122 |
-
export_file = gr.File(visible=False)
|
| 123 |
|
| 124 |
search_btn.click(
|
| 125 |
fn=run_search,
|
|
@@ -128,25 +137,28 @@ with gr.Blocks() as app:
|
|
| 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 |
-
|
| 137 |
-
|
| 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 |
|
|
|
|
| 4 |
from ingest.registry import get_all_adapters
|
| 5 |
from ingest.export import export_results
|
| 6 |
from ingest.health import get_adapter_health
|
| 7 |
+
from ingest.coverage import coverage_summary
|
| 8 |
+
from ingest.discovery import agency_discovery
|
| 9 |
+
from ingest.semantic import semantic_refine, semantic_available
|
| 10 |
from analytics.events import log_event
|
| 11 |
|
| 12 |
ALL_ADAPTERS = get_all_adapters()
|
|
|
|
| 16 |
include_stubs: bool,
|
| 17 |
enable_extended: bool,
|
| 18 |
acknowledge_extended: bool,
|
| 19 |
+
enable_semantic: bool,
|
| 20 |
) -> List[Dict]:
|
| 21 |
+
|
| 22 |
log_event("search", {"query_length": len(query or "")})
|
| 23 |
|
| 24 |
if not query:
|
|
|
|
| 27 |
results: List[Dict] = []
|
| 28 |
|
| 29 |
for adapter in ALL_ADAPTERS:
|
|
|
|
| 30 |
if not include_stubs and not adapter.is_live:
|
| 31 |
continue
|
| 32 |
|
|
|
|
| 33 |
if adapter.is_extended:
|
| 34 |
if not enable_extended or not acknowledge_extended:
|
| 35 |
continue
|
|
|
|
| 51 |
"exportable": False,
|
| 52 |
})
|
| 53 |
|
| 54 |
+
if enable_semantic and semantic_available():
|
| 55 |
+
results = semantic_refine(query, results)
|
| 56 |
+
|
| 57 |
return results
|
| 58 |
|
| 59 |
|
| 60 |
def table_from_results(results: List[Dict]):
|
| 61 |
+
return [
|
| 62 |
+
[
|
|
|
|
| 63 |
r.get("agency", ""),
|
| 64 |
r.get("status", ""),
|
| 65 |
r.get("title", ""),
|
| 66 |
r.get("snippet", ""),
|
| 67 |
r.get("url", ""),
|
| 68 |
+
]
|
| 69 |
+
for r in results
|
| 70 |
+
]
|
| 71 |
|
| 72 |
|
| 73 |
def export_handler(results: List[Dict]):
|
|
|
|
|
|
|
|
|
|
| 74 |
exportable = [r for r in results if r.get("exportable")]
|
|
|
|
| 75 |
if not exportable:
|
| 76 |
return gr.File.update(visible=False)
|
| 77 |
+
return gr.File.update(
|
| 78 |
+
value=export_results(exportable),
|
| 79 |
+
visible=True
|
| 80 |
+
)
|
| 81 |
|
| 82 |
|
| 83 |
with gr.Blocks() as app:
|
|
|
|
| 88 |
|
| 89 |
gr.Markdown(
|
| 90 |
"ℹ️ **Stub results are informational and cannot be exported.**\n\n"
|
| 91 |
+
"Semantic search is optional and operates only on returned results."
|
| 92 |
)
|
| 93 |
|
| 94 |
+
query = gr.Textbox(label="Search query")
|
|
|
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
+
include_stubs = gr.Checkbox(label="Include Stub Results", value=True)
|
| 98 |
+
enable_extended = gr.Checkbox(label="Enable Extended Coverage", value=False)
|
| 99 |
+
enable_semantic = gr.Checkbox(
|
| 100 |
+
label="Enable Semantic Refinement (Experimental)",
|
| 101 |
+
value=False,
|
| 102 |
+
interactive=semantic_available()
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
acknowledge_extended = gr.Checkbox(
|
|
|
|
| 110 |
search_btn = gr.Button("Search")
|
| 111 |
|
| 112 |
results_state = gr.State([])
|
| 113 |
+
|
| 114 |
results_table = gr.Dataframe(
|
| 115 |
headers=["Agency", "Status", "Title", "Snippet", "URL"],
|
| 116 |
interactive=False,
|
| 117 |
wrap=True
|
| 118 |
)
|
| 119 |
|
| 120 |
+
export_btn = gr.Button("Export Results (ZIP)", interactive=False)
|
| 121 |
+
export_file = gr.File(visible=False)
|
| 122 |
+
|
| 123 |
+
coverage_table = gr.Dataframe(
|
| 124 |
+
headers=["Agency", "Result Count"],
|
| 125 |
+
interactive=False
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
discovery_table = gr.Dataframe(
|
| 129 |
+
headers=["Agency", "Status", "Reason"],
|
| 130 |
interactive=False
|
| 131 |
)
|
|
|
|
| 132 |
|
| 133 |
search_btn.click(
|
| 134 |
fn=run_search,
|
|
|
|
| 137 |
include_stubs,
|
| 138 |
enable_extended,
|
| 139 |
acknowledge_extended,
|
| 140 |
+
enable_semantic,
|
| 141 |
],
|
| 142 |
+
outputs=results_state
|
| 143 |
).then(
|
| 144 |
fn=lambda r: (
|
| 145 |
table_from_results(r),
|
| 146 |
+
coverage_summary(r),
|
| 147 |
+
gr.Button.update(interactive=any(x.get("exportable") for x in r))
|
|
|
|
| 148 |
),
|
| 149 |
inputs=results_state,
|
| 150 |
+
outputs=[results_table, coverage_table, export_btn]
|
| 151 |
)
|
| 152 |
|
| 153 |
export_btn.click(
|
| 154 |
fn=export_handler,
|
| 155 |
inputs=results_state,
|
| 156 |
+
outputs=export_file
|
| 157 |
)
|
| 158 |
|
| 159 |
+
gr.Markdown("## 🌐 Agency Coverage")
|
| 160 |
+
discovery_table.value = agency_discovery()
|
| 161 |
+
|
| 162 |
gr.Markdown("## 🔍 Adapter Health")
|
| 163 |
gr.JSON(get_adapter_health())
|
| 164 |
|