GodsDevProject commited on
Commit
b75db41
·
verified ·
1 Parent(s): 8085ea8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -45
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.fbi_vault_live import FBIAdapter
5
- from ingest.dod_reading_room_live import DoDAdapter
6
- from core.async_search import fanout_search
7
- from core.cache import dedupe
8
- from core.cluster import cluster_results
9
- from core.citations import citation_block
10
- from core.redaction import redaction_confidence
11
- from core.journalist import journalist_export
12
- from core.explain import explain
13
-
14
- cia, fbi, dod = CIAAdapter(), FBIAdapter(), DoDAdapter()
15
-
16
- async def run(q):
17
- res = await fanout_search([cia,fbi,dod], q)
18
- return dedupe(res)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  with gr.Blocks() as demo:
21
- gr.Markdown("# FOIA Federated Search — Supreme")
22
-
23
- q = gr.Textbox(label="Query")
24
- results_state = gr.State([])
25
-
26
- with gr.Tabs():
27
- with gr.Tab("Clustered Results"):
28
- clusters = gr.JSON()
29
- with gr.Tab("Citations"):
30
- cites = gr.Markdown()
31
- with gr.Tab("Explainability"):
32
- explain_box = gr.JSON()
33
-
34
- preview = gr.JSON(label="Redaction Confidence")
35
-
36
- def _run(q):
37
- res = asyncio.run(run(q))
38
- cl = cluster_results(res)
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()