GodsDevProject commited on
Commit
6d124ed
·
verified ·
1 Parent(s): 7b326b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -34
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import asyncio
2
  import time
3
  from typing import List, Dict
4
 
@@ -14,16 +13,16 @@ import coverage
14
  import saved_searches
15
 
16
 
17
- # -----------------------------
18
  # Adapter Registry
19
- # -----------------------------
20
 
21
  ALL_ADAPTERS = get_all_adapters()
22
 
23
 
24
- # -----------------------------
25
- # Async Search Engine
26
- # -----------------------------
27
 
28
  async def _search_adapter(adapter, query: str) -> Dict:
29
  start = time.time()
@@ -57,7 +56,7 @@ async def federated_search_async(
57
  ]
58
 
59
  tasks = [_search_adapter(a, query) for a in adapters]
60
- responses = await asyncio.gather(*tasks)
61
 
62
  flat_results = []
63
  health = {}
@@ -73,9 +72,17 @@ async def federated_search_async(
73
  return flat_results, health
74
 
75
 
76
- # -----------------------------
 
 
 
 
 
 
 
 
77
  # Rendering Helpers
78
- # -----------------------------
79
 
80
  def badge(result: Dict) -> str:
81
  badges = []
@@ -113,11 +120,11 @@ def render_health(health: Dict) -> str:
113
  return "\n".join(lines)
114
 
115
 
116
- # -----------------------------
117
- # Sync Wrapper (HF Safe)
118
- # -----------------------------
119
 
120
- def run_search(
121
  query: str,
122
  hide_stubs: bool,
123
  enable_extended: bool,
@@ -133,10 +140,7 @@ def run_search(
133
  [a for a in enabled_extended_agencies if a in EXTENDED_AGENCIES]
134
  )
135
 
136
- loop = asyncio.get_event_loop()
137
- results, health = loop.run_until_complete(
138
- federated_search_async(query, agencies)
139
- )
140
 
141
  saved_searches.save(query, enable_extended)
142
 
@@ -150,28 +154,28 @@ def run_search(
150
  return rendered, render_health(health), coverage.render_coverage()
151
 
152
 
153
- # -----------------------------
154
  # Gradio UI
155
- # -----------------------------
156
 
157
- with gr.Blocks(title="Federated FOIA Search") as demo:
158
  gr.Markdown(
159
  """
160
  # 🏛️ Federated FOIA Document Search
161
- Search **public government FOIA reading rooms** across multiple agencies.
162
 
163
- - Default: **Safe, public-only sources**
164
- - Extended mode: **Additional public releases (opt-in)**
165
- - No authentication
166
- - No classified or restricted systems
 
 
167
  """
168
  )
169
 
170
- with gr.Row():
171
- query = gr.Textbox(
172
- label="Search term",
173
- placeholder="e.g. UAP radar incident",
174
- )
175
 
176
  with gr.Row():
177
  hide_stubs = gr.Checkbox(
@@ -219,7 +223,7 @@ Search **public government FOIA reading rooms** across multiple agencies.
219
  **Legal Notice**
220
 
221
  This tool:
222
- - Accesses **only publicly available FOIA Electronic Reading Rooms**
223
  - Respects robots.txt and rate limits
224
  - Does not bypass access controls
225
  - Is intended for journalism, research, and public-interest use
@@ -227,9 +231,9 @@ This tool:
227
  )
228
 
229
 
230
- # -----------------------------
231
- # HF Entry Point
232
- # -----------------------------
233
 
234
  if __name__ == "__main__":
235
  demo.launch()
 
 
1
  import time
2
  from typing import List, Dict
3
 
 
13
  import saved_searches
14
 
15
 
16
+ # ======================================================
17
  # Adapter Registry
18
+ # ======================================================
19
 
20
  ALL_ADAPTERS = get_all_adapters()
21
 
22
 
23
+ # ======================================================
24
+ # Async Federated Search Engine
25
+ # ======================================================
26
 
27
  async def _search_adapter(adapter, query: str) -> Dict:
28
  start = time.time()
 
56
  ]
57
 
58
  tasks = [_search_adapter(a, query) for a in adapters]
59
+ responses = await gradio_async_gather(tasks)
60
 
61
  flat_results = []
62
  health = {}
 
72
  return flat_results, health
73
 
74
 
75
+ async def gradio_async_gather(tasks):
76
+ """
77
+ HF-safe async gather wrapper.
78
+ """
79
+ import asyncio
80
+ return await asyncio.gather(*tasks, return_exceptions=False)
81
+
82
+
83
+ # ======================================================
84
  # Rendering Helpers
85
+ # ======================================================
86
 
87
  def badge(result: Dict) -> str:
88
  badges = []
 
120
  return "\n".join(lines)
121
 
122
 
123
+ # ======================================================
124
+ # HF-SAFE ASYNC SEARCH HANDLER
125
+ # ======================================================
126
 
127
+ async def run_search(
128
  query: str,
129
  hide_stubs: bool,
130
  enable_extended: bool,
 
140
  [a for a in enabled_extended_agencies if a in EXTENDED_AGENCIES]
141
  )
142
 
143
+ results, health = await federated_search_async(query, agencies)
 
 
 
144
 
145
  saved_searches.save(query, enable_extended)
146
 
 
154
  return rendered, render_health(health), coverage.render_coverage()
155
 
156
 
157
+ # ======================================================
158
  # Gradio UI
159
+ # ======================================================
160
 
161
+ with gr.Blocks(title="Federated FOIA Document Search") as demo:
162
  gr.Markdown(
163
  """
164
  # 🏛️ Federated FOIA Document Search
 
165
 
166
+ Search **public government FOIA electronic reading rooms** across multiple agencies.
167
+
168
+ - **Default:** Safe, public-only sources
169
+ - **Extended mode:** Additional public releases (opt-in)
170
+ - No authentication
171
+ - No classified or restricted systems
172
  """
173
  )
174
 
175
+ query = gr.Textbox(
176
+ label="Search term",
177
+ placeholder="e.g. UAP, radar incident, AATIP",
178
+ )
 
179
 
180
  with gr.Row():
181
  hide_stubs = gr.Checkbox(
 
223
  **Legal Notice**
224
 
225
  This tool:
226
+ - Accesses **only publicly available FOIA electronic reading rooms**
227
  - Respects robots.txt and rate limits
228
  - Does not bypass access controls
229
  - Is intended for journalism, research, and public-interest use
 
231
  )
232
 
233
 
234
+ # ======================================================
235
+ # HF ENTRY POINT
236
+ # ======================================================
237
 
238
  if __name__ == "__main__":
239
  demo.launch()