Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,96 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import asyncio
|
|
|
|
|
|
|
|
|
|
| 3 |
from ingest.cia_reading_room import CIAAdapter
|
| 4 |
-
from ingest.
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
with gr.Blocks() as demo:
|
| 21 |
-
gr.Markdown("# FOIA Federated Search
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
cites_md = "\n".join(citation_block(r) for r in res[:5])
|
| 40 |
-
explain_data = explain(res)
|
| 41 |
-
red = {r.get("url"): redaction_confidence(r) for r in res}
|
| 42 |
-
return res, cl, cites_md, explain_data, red
|
| 43 |
-
|
| 44 |
-
btn = gr.Button("Search")
|
| 45 |
-
btn.click(_run, inputs=q, outputs=[results_state, clusters, cites, explain_box, preview])
|
| 46 |
-
|
| 47 |
-
exp = gr.Button("Journalist Export")
|
| 48 |
-
out = gr.File()
|
| 49 |
-
exp.click(lambda r: journalist_export(r, "/tmp/journalist_export.zip"), inputs=results_state, outputs=out)
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from typing import List, Dict
|
| 4 |
+
|
| 5 |
from ingest.cia_reading_room import CIAAdapter
|
| 6 |
+
from ingest.fbi_vault import FBIAdapter
|
| 7 |
+
|
| 8 |
+
# Register adapters here
|
| 9 |
+
ADAPTERS = [
|
| 10 |
+
CIAAdapter(), # stub (safe)
|
| 11 |
+
FBIAdapter(), # live
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
# -------------------------
|
| 15 |
+
# Async search core
|
| 16 |
+
# -------------------------
|
| 17 |
+
async def federated_search_async(query: str) -> List[Dict]:
|
| 18 |
+
tasks = [adapter.search(query) for adapter in ADAPTERS]
|
| 19 |
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 20 |
+
|
| 21 |
+
flat: List[Dict] = []
|
| 22 |
+
for r in results:
|
| 23 |
+
if isinstance(r, Exception):
|
| 24 |
+
continue
|
| 25 |
+
flat.extend(r)
|
| 26 |
+
|
| 27 |
+
return flat
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# -------------------------
|
| 31 |
+
# Sync wrapper (HF-safe)
|
| 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 |
+
return loop.run_until_complete(federated_search_async(query))
|
| 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']}**
|
| 50 |
|
| 51 |
+
{r['snippet']}
|
| 52 |
+
|
| 53 |
+
🔗 {r['url']}
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def run_search(query: str, hide_stubs: bool):
|
| 58 |
+
results = federated_search(query)
|
| 59 |
+
|
| 60 |
+
# If async task returned
|
| 61 |
+
if asyncio.isfuture(results):
|
| 62 |
+
results = asyncio.get_event_loop().run_until_complete(results)
|
| 63 |
+
|
| 64 |
+
if hide_stubs:
|
| 65 |
+
results = [r for r in results if r.get("live")]
|
| 66 |
+
|
| 67 |
+
if not results:
|
| 68 |
+
return "No results found."
|
| 69 |
+
|
| 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 |
+
"Stub sources are clearly labeled."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
query = gr.Textbox(label="Search term", placeholder="e.g. UAP radar")
|
| 84 |
+
hide_stubs = gr.Checkbox(
|
| 85 |
+
label="Hide stub (placeholder) sources",
|
| 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 |
|
| 96 |
demo.launch()
|