Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| # Optional Plotly | |
| try: | |
| import plotly.express as px | |
| import plotly.graph_objects as go | |
| PLOTLY = True | |
| except Exception: | |
| PLOTLY = False | |
| # --------------------------- | |
| # Demo FOIA Data | |
| # --------------------------- | |
| DATA = pd.DataFrame([ | |
| { | |
| "title": "MKULTRA Behavioral Experiments", | |
| "agency": "CIA", | |
| "date": "1977-08-03", | |
| "year": 1977, | |
| "summary": "CIA behavioral research involving human subjects.", | |
| "entities": ["CIA", "MKULTRA"] | |
| }, | |
| { | |
| "title": "Human Performance Research", | |
| "agency": "DoD", | |
| "date": "1975-01-12", | |
| "year": 1975, | |
| "summary": "DoD-funded cognition research.", | |
| "entities": ["DoD", "Cognition"] | |
| } | |
| ]) | |
| AGENCIES = sorted(DATA["agency"].unique().tolist()) | |
| # --------------------------- | |
| # Search | |
| # --------------------------- | |
| def run_search(query, agencies): | |
| df = DATA.copy() | |
| if query: | |
| df = df[df["title"].str.contains(query, case=False)] | |
| if agencies: | |
| df = df[df["agency"].isin(agencies)] | |
| return df[["title", "agency", "date"]] | |
| def preview_row(evt: gr.SelectData): | |
| row = DATA.iloc[evt.index] | |
| return f""" | |
| ### {row['title']} | |
| **Agency:** {row['agency']} | |
| **Date:** {row['date']} | |
| {row['summary']} | |
| """ | |
| # --------------------------- | |
| # Visuals | |
| # --------------------------- | |
| def coverage_heatmap(): | |
| if not PLOTLY: | |
| return None | |
| heat = DATA.groupby(["agency", "year"]).size().reset_index(name="count") | |
| fig = px.density_heatmap( | |
| heat, | |
| x="year", | |
| y="agency", | |
| z="count", | |
| color_continuous_scale="Blues" | |
| ) | |
| fig.update_layout(height=300) | |
| return fig | |
| def entity_graph(): | |
| if not PLOTLY: | |
| return None | |
| nodes = list(set(sum(DATA["entities"].tolist(), []))) | |
| fig = go.Figure( | |
| data=go.Scatter( | |
| x=list(range(len(nodes))), | |
| y=[0]*len(nodes), | |
| mode="markers+text", | |
| text=nodes | |
| ) | |
| ) | |
| fig.update_layout(height=300) | |
| return fig | |
| # --------------------------- | |
| # UI | |
| # --------------------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown(""" | |
| # ποΈ Federal FOIA Intelligence Search | |
| **Public Electronic Reading Rooms Only** | |
| """) | |
| if not PLOTLY: | |
| gr.Markdown("β οΈ Plotly not installed β graphs disabled.") | |
| with gr.Tab("π Search"): | |
| query = gr.Textbox(label="Search query", value="MKULTRA") | |
| agencies = gr.CheckboxGroup(AGENCIES, value=AGENCIES) | |
| search_btn = gr.Button("Search") | |
| table = gr.Dataframe(headers=["Title", "Agency", "Date"]) | |
| preview = gr.Markdown() | |
| search_btn.click(run_search, [query, agencies], table) | |
| table.select(preview_row, preview) | |
| with gr.Tab("π Coverage Heatmap"): | |
| gr.Plot(value=coverage_heatmap) | |
| with gr.Tab("π§ Entity Graph"): | |
| gr.Plot(value=entity_graph) | |
| demo.launch() |