Spaces:
Sleeping
Sleeping
Create app.py
Browse files- governance_docs/app.py +88 -0
governance_docs/app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import HTMLResponse
|
| 4 |
+
import hashlib, io, time, requests, base64
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak
|
| 7 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 8 |
+
|
| 9 |
+
from governance_site import load_governance_doc
|
| 10 |
+
|
| 11 |
+
# ======================================================
|
| 12 |
+
# HARD GOVERNANCE FLAGS
|
| 13 |
+
# ======================================================
|
| 14 |
+
|
| 15 |
+
ENABLE_FAISS_PHASE_4 = False # CRYPTOGRAPHICALLY LOCKED
|
| 16 |
+
ENABLE_AI = True
|
| 17 |
+
|
| 18 |
+
# ======================================================
|
| 19 |
+
# FASTAPI APP (PROVENANCE HEADERS)
|
| 20 |
+
# ======================================================
|
| 21 |
+
|
| 22 |
+
api = FastAPI()
|
| 23 |
+
|
| 24 |
+
@api.middleware("http")
|
| 25 |
+
async def provenance_headers(request: Request, call_next):
|
| 26 |
+
response = await call_next(request)
|
| 27 |
+
response.headers["X-FOIA-Tool"] = "Federal FOIA Intelligence Search"
|
| 28 |
+
response.headers["X-Provenance-UTC"] = datetime.utcnow().isoformat()
|
| 29 |
+
response.headers["X-AI-Usage"] = "User-Initiated Only"
|
| 30 |
+
response.headers["X-FAISS-Status"] = "DISABLED"
|
| 31 |
+
return response
|
| 32 |
+
|
| 33 |
+
# ======================================================
|
| 34 |
+
# GRADIO UI
|
| 35 |
+
# ======================================================
|
| 36 |
+
|
| 37 |
+
def sha256(text):
|
| 38 |
+
return hashlib.sha256(text.encode()).hexdigest()
|
| 39 |
+
|
| 40 |
+
LAST_RESULTS = []
|
| 41 |
+
SELECTED = None
|
| 42 |
+
|
| 43 |
+
def search(query):
|
| 44 |
+
global LAST_RESULTS
|
| 45 |
+
LAST_RESULTS = [{
|
| 46 |
+
"agency": "CIA",
|
| 47 |
+
"title": "CIA FOIA Reading Room",
|
| 48 |
+
"url": f"https://www.cia.gov/readingroom/search/site/{query}",
|
| 49 |
+
"hash": sha256(query)
|
| 50 |
+
}]
|
| 51 |
+
return LAST_RESULTS
|
| 52 |
+
|
| 53 |
+
def preview():
|
| 54 |
+
if not LAST_RESULTS:
|
| 55 |
+
return "No document selected."
|
| 56 |
+
return f"<iframe src='{LAST_RESULTS[0]['url']}' style='width:100%;height:700px'></iframe>"
|
| 57 |
+
|
| 58 |
+
def toa():
|
| 59 |
+
return "\n".join(f"Exhibit {i+1}: {r['agency']}" for i, r in enumerate(LAST_RESULTS))
|
| 60 |
+
|
| 61 |
+
with gr.Blocks(title="Federal FOIA Intelligence Search") as ui:
|
| 62 |
+
gr.Markdown("## Federal FOIA Intelligence Search")
|
| 63 |
+
|
| 64 |
+
with gr.Tab("Search"):
|
| 65 |
+
q = gr.Textbox()
|
| 66 |
+
out = gr.JSON()
|
| 67 |
+
gr.Button("Search").click(search, q, out)
|
| 68 |
+
|
| 69 |
+
with gr.Tab("Preview"):
|
| 70 |
+
gr.HTML().render(preview)
|
| 71 |
+
|
| 72 |
+
with gr.Tab("Exhibits"):
|
| 73 |
+
gr.Textbox(lines=10).render(toa)
|
| 74 |
+
|
| 75 |
+
with gr.Tab("Governance"):
|
| 76 |
+
gr.HTML(load_governance_doc("index.html"))
|
| 77 |
+
|
| 78 |
+
with gr.Tab("Trust & Safety"):
|
| 79 |
+
gr.HTML(load_governance_doc("trust_safety.html"))
|
| 80 |
+
|
| 81 |
+
with gr.Tab("Judicial / DOJ"):
|
| 82 |
+
gr.HTML(load_governance_doc("judicial_doj_memo.html"))
|
| 83 |
+
|
| 84 |
+
# ======================================================
|
| 85 |
+
# MOUNT GRADIO INTO FASTAPI
|
| 86 |
+
# ======================================================
|
| 87 |
+
|
| 88 |
+
app = gr.mount_gradio_app(api, ui, path="/")
|