Spaces:
Sleeping
Sleeping
Gen. Overseer Lupo
commited on
Commit
·
4a73ab0
1
Parent(s):
491cb50
WIP before LFS migrate
Browse files- README.backup +28 -0
- app/ui_streamlit.py.bak +129 -0
README.backup
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Grants RAG Finder
|
| 3 |
+
emoji: 🧭
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
app_file: app/ui_streamlit.py
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Grants Discovery RAG App (v6.2 hotfix)
|
| 12 |
+
|
| 13 |
+
- Desktop paths baked in
|
| 14 |
+
- Grants.gov API + topic routes (General, Elderly, Prison, Evangelism, Vehicles, FTA 5310)
|
| 15 |
+
- Makefile helpers: `make ps`, `make stop`, `make restart`
|
| 16 |
+
- **Hotfix**: corrected Streamlit write line (single line, no syntax error)
|
| 17 |
+
|
| 18 |
+
## One-Time Setup
|
| 19 |
+
```bash
|
| 20 |
+
cd ~/Desktop
|
| 21 |
+
unzip ~/Downloads/grants_rag_app_desktop_v6_2.zip -d ~/Desktop/
|
| 22 |
+
cd /Users/gen.overseerlupo/Desktop/grants_rag_app
|
| 23 |
+
python3 -m venv .venv
|
| 24 |
+
source .venv/bin/activate
|
| 25 |
+
pip install -U pip wheel
|
| 26 |
+
pip install -r requirements.txt
|
| 27 |
+
cp .env.example .env
|
| 28 |
+
open -a TextEdit .env config/sources.yaml
|
app/ui_streamlit.py.bak
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/ui_streamlit.py
|
| 2 |
+
import os, json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
from app.main import get_env, ensure_index_exists
|
| 7 |
+
from app.search import search
|
| 8 |
+
|
| 9 |
+
# Streamlit config should be the first Streamlit call
|
| 10 |
+
st.set_page_config(page_title="Grants Discovery RAG", layout="wide")
|
| 11 |
+
|
| 12 |
+
# Environment + index
|
| 13 |
+
_env = get_env()
|
| 14 |
+
ensure_index_exists(_env)
|
| 15 |
+
|
| 16 |
+
# ---------- helpers ----------
|
| 17 |
+
def _dedup_records(rows):
|
| 18 |
+
seen, out = set(), []
|
| 19 |
+
for r in rows or []:
|
| 20 |
+
k = r.get("id") or r.get("url") or r.get("title")
|
| 21 |
+
if not k or k in seen:
|
| 22 |
+
continue
|
| 23 |
+
seen.add(k)
|
| 24 |
+
out.append(r)
|
| 25 |
+
return out
|
| 26 |
+
# ---------- end helpers ----------
|
| 27 |
+
|
| 28 |
+
# ---------- optional diagnostics ----------
|
| 29 |
+
with st.expander("Diagnostics (optional)", expanded=False):
|
| 30 |
+
idx = Path(_env["INDEX_DIR"])
|
| 31 |
+
st.write("INDEX_DIR:", str(idx))
|
| 32 |
+
st.write("faiss.index exists:", (idx / "faiss.index").exists())
|
| 33 |
+
st.write("meta.json exists:", (idx / "meta.json").exists())
|
| 34 |
+
if (idx / "meta.json").exists():
|
| 35 |
+
try:
|
| 36 |
+
meta = json.loads((idx / "meta.json").read_text())
|
| 37 |
+
st.write("meta.json count:", len(meta))
|
| 38 |
+
st.write("meta head:", [{"id": m.get("id"), "title": m.get("title")} for m in meta[:2]])
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"Failed to read meta.json: {e!r}")
|
| 41 |
+
try:
|
| 42 |
+
demo = search("transportation", _env, top_k=3, filters={})
|
| 43 |
+
st.write("sample search('transportation') results:", len(demo))
|
| 44 |
+
if demo:
|
| 45 |
+
st.write(demo[:3])
|
| 46 |
+
except Exception as e:
|
| 47 |
+
st.error(f"search() raised: {e!r}")
|
| 48 |
+
# ---------- end diagnostics ----------
|
| 49 |
+
|
| 50 |
+
st.title("Grants Discovery RAG (Capacity Building)")
|
| 51 |
+
|
| 52 |
+
preset = st.radio(
|
| 53 |
+
"Quick topic:",
|
| 54 |
+
["General", "Elderly", "Prison Ministry", "Evangelism", "Vehicles/Transport", "FTA 5310"],
|
| 55 |
+
horizontal=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
default_q = {
|
| 59 |
+
"General": "capacity building",
|
| 60 |
+
"Elderly": "capacity building for seniors and aging services",
|
| 61 |
+
"Prison Ministry": "capacity building for reentry and prison ministry",
|
| 62 |
+
"Evangelism": "capacity building for faith and community outreach",
|
| 63 |
+
"Vehicles/Transport": "capacity building transportation vehicles vans buses mobility",
|
| 64 |
+
"FTA 5310": "5310 Enhanced Mobility Seniors Individuals with Disabilities",
|
| 65 |
+
}.get(preset, "capacity building")
|
| 66 |
+
|
| 67 |
+
# --- controls ---
|
| 68 |
+
q = st.text_input("Search query", value=default_q)
|
| 69 |
+
|
| 70 |
+
# No defaults -> no filtering unless the user selects something
|
| 71 |
+
geo = st.multiselect("Geo filter (optional)", options=["US", "MD", "MA"], default=[])
|
| 72 |
+
categories = st.multiselect(
|
| 73 |
+
"Category filter (optional)",
|
| 74 |
+
options=["capacity_building", "elderly", "prison_ministry", "evangelism", "transportation", "vehicle"],
|
| 75 |
+
default=[]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
top_k = st.slider("Results", 5, 50, 15)
|
| 79 |
+
|
| 80 |
+
# Build filters only when selected
|
| 81 |
+
filters = {}
|
| 82 |
+
if geo:
|
| 83 |
+
filters["geo"] = geo
|
| 84 |
+
if categories:
|
| 85 |
+
filters["categories"] = categories # <- use 'categories' key (not 'cats')
|
| 86 |
+
|
| 87 |
+
col1, col2 = st.columns([1, 1])
|
| 88 |
+
|
| 89 |
+
with col1:
|
| 90 |
+
if st.button("Search"):
|
| 91 |
+
try:
|
| 92 |
+
results = search(q, _env, top_k=top_k, filters=filters)
|
| 93 |
+
results = _dedup_records(results)
|
| 94 |
+
st.session_state["results"] = results
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.error(str(e))
|
| 97 |
+
|
| 98 |
+
with col2:
|
| 99 |
+
if st.button("Export Results to CSV"):
|
| 100 |
+
results = st.session_state.get("results", [])
|
| 101 |
+
if not results:
|
| 102 |
+
st.warning("No results to export. Run a search first.")
|
| 103 |
+
else:
|
| 104 |
+
os.makedirs(_env["EXPORT_DIR"], exist_ok=True)
|
| 105 |
+
out_path = os.path.join(_env["EXPORT_DIR"], "results.csv")
|
| 106 |
+
import pandas as pd
|
| 107 |
+
pd.DataFrame(results).to_csv(out_path, index=False)
|
| 108 |
+
st.success(f"Exported to {out_path}")
|
| 109 |
+
|
| 110 |
+
st.markdown("---")
|
| 111 |
+
|
| 112 |
+
results = st.session_state.get("results", [])
|
| 113 |
+
if results:
|
| 114 |
+
st.caption(f"Results: {len(results)}")
|
| 115 |
+
for r in results:
|
| 116 |
+
title = r.get("title", "(no title)")
|
| 117 |
+
url = r.get("url", "")
|
| 118 |
+
cats = r.get("categories") or r.get("cats") or []
|
| 119 |
+
geo_tags = r.get("geo") or []
|
| 120 |
+
|
| 121 |
+
st.markdown(f"### {title}")
|
| 122 |
+
st.write(f"**Source:** {r.get('source','')} | **Geo:** {', '.join(geo_tags) if isinstance(geo_tags, list) else geo_tags} | **Categories:** {', '.join(cats) if isinstance(cats, list) else cats}")
|
| 123 |
+
|
| 124 |
+
if url and not url.startswith("http"):
|
| 125 |
+
st.caption("Note: This item may display an ID or number instead of a full link. Open on Grants.gov if needed.")
|
| 126 |
+
st.write(f"[Open Link]({url}) \nScore: {r.get('score', 0):.3f}")
|
| 127 |
+
st.markdown("---")
|
| 128 |
+
else:
|
| 129 |
+
st.info("Enter a query and click Search.")
|