File size: 11,478 Bytes
b30f068 9e42edc b30f068 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | #!/usr/bin/env python3
"""
Deploy AgAdvisor to a Hugging Face Streamlit Space.
Creates (or updates) a private Space, sets the API keys as Space secrets (read
from your local .env β they never leave your machine except into HF's secret
store), uploads the app + the committed offline index, and configures runtime
env vars so it serves from the on-disk index (single process, no Docker).
Run it yourself from the repo root (your terminal, your keys, your call):
export HF_TOKEN=hf_xxx # a write token: https://huggingface.co/settings/tokens
python scripts/deploy_hf.py # -> private Space tirtho149/agadvisor
python scripts/deploy_hf.py --repo you/agadvisor --public # options
After it finishes, open the Space URL β HF builds and launches it automatically.
"""
import argparse
import os
import re
import sys
import tempfile
from pathlib import Path
from dotenv import load_dotenv
from huggingface_hub import HfApi
ROOT = Path(__file__).resolve().parents[1]
# HF Space config written as the Space's README.md.
# NOTE: HF retired the Streamlit SDK (create_repo accepts only gradio|docker|static), AND
# Gradio/Docker on free cpu-basic now requires PRO. The one free path for a real app is
# Gradio on ZeroGPU, so the Space runs app.py (Gradio) rather than the Streamlit UI.
README = """---
title: AgAdvisor
emoji: πΏ
colorFrom: green
colorTo: gray
sdk: gradio
sdk_version: 5.50.0
app_file: app.py
python_version: "3.12"
pinned: false
short_description: CDMS pesticide-label AI assistant
license: other
license_name: iowa-state-university-proprietary
---
# πΏ AgAdvisor
Conversational assistant over CDMS pesticide labels (offline preprocessed index),
with weather / soil / agronomic tools and page-level citations.
Copyright Β© 2026 Iowa State University. All rights reserved.
Pesticide labels are legally binding. This tool is a research prototype and is not a
substitute for reading the product label. Always verify against the label of record.
"""
LICENSE = """Copyright Β© 2026 Iowa State University. All rights reserved.
This software and its accompanying data were developed at Iowa State University.
No license or other right is granted to any third party to use, copy, modify, or
distribute this software, in whole or in part, without prior written permission.
Licensing terms are subject to Iowa State University policy; contact the Iowa State
University Research Foundation (ISURF) regarding use or redistribution.
"""
# spaCy model isn't on PyPI as a normal dep -> install it from the release wheel.
SPACY_MODEL_LINE = (
"en_core_web_sm @ https://github.com/explosion/spacy-models/releases/download/"
"en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl\n"
)
# Never upload these to the Space. The Space repo is the publication boundary β the
# upload is the working tree (not git history), so this list is the ONLY thing standing
# between a local file and the public internet. Add, don't remove.
IGNORE = [
".git*", "**/.git/**",
".env", ".env.*", "*.bak", "*.bak.*",
"config/api_keys.json", # potential local secrets
"**/__pycache__/**", "*.pyc",
"data/*.corrupted_*", "data/*.prebuild.*", "data/cdms_metadata_recovered.db",
"data/qdrant_local/.lock", "data/qdrant_local/**/*.lock",
"README.md", "requirements.txt", # replaced with HF-specific versions
"env/**", "scratchpad/**",
# Accounts DB: usernames + bcrypt hashes. Empty today, but a redeploy after real
# signups would publish the user table.
"data/app/**",
# Unpublished work: docs/ holds the CHI 2027 draft, which is anonymized for review.
"docs/**",
# Real personal email addresses; untracked AND not gitignored.
"email_to_joshua.txt",
# Third-party CDMS pesticide labels (manufacturer copyright, cdms.net terms). Not
# read at serve time: offline mode nulls the PDF loaders (cdms_label_tool.py:44-49)
# and answers come from the Qdrant store + cdms_metadata.db, with citation URLs from
# the Qdrant `pdf_url` payload.
"data/pdfs/**",
# Internal team docs: RUNBOOK carries the live EC2 IP, the private repo name and
# collaborator names; the ISA reports are internal feedback correspondence.
"RUNBOOK.md",
"AgAdvisor_ISA_Fixes_Report.md", "AgAdvisor_ISA_Fixes_Report.pdf",
"AgAdvisor_ISA_Fixes_Visual.md", "CHANGELOG_ISA_FEEDBACK.md", "FIXES.md",
"roundup_chunks_detailed.txt", # debug dump of indexed chunks
# Local runtime noise.
"logs/**", "*.log", "evaluation_reports/**",
".DS_Store", "**/.DS_Store", ".pytest_cache/**", ".ruff_cache/**",
]
def dry_run(quiet: bool = False):
"""List what upload_folder would actually publish, using HF's own matcher.
The Space repo is public-facing and the upload is the working tree, so 'what
exactly ships' is worth being able to answer without pushing. Exits non-zero if
any sensitive path or credential-shaped string is in the upload set.
"""
from huggingface_hub.utils import filter_repo_objects
all_files = [str(p.relative_to(ROOT)) for p in ROOT.rglob("*") if p.is_file()]
kept = sorted(filter_repo_objects(all_files, ignore_patterns=IGNORE))
excluded = sorted(set(all_files) - set(kept))
total = sum((ROOT / f).stat().st_size for f in kept)
print(f"WOULD UPLOAD β {len(kept)} files, {total / 1e6:.1f} MB\n")
if not quiet:
for f in kept:
print(f" + {f}")
print(f"\nEXCLUDED β {len(excluded)} files")
for pat in ("data/app", "docs/", "email_to_joshua", "data/pdfs", ".env"):
hits = [f for f in excluded if f.startswith(pat) or f == pat]
print(f" {'β
' if hits or pat == '.env' else 'β οΈ '} {pat}: {len(hits)} excluded")
leaked = [f for f in kept if f.startswith(("data/app", "docs/", "data/pdfs"))
or f in ("email_to_joshua.txt", ".env", "RUNBOOK.md", ".DS_Store")]
if leaked:
print("\nβ SENSITIVE FILES WOULD BE PUBLISHED:")
for f in leaked:
print(f" {f}")
sys.exit(1)
print("\nβ
No sensitive paths in the upload set.")
hits = scan_for_secrets(kept)
if hits:
print("\nβ CREDENTIAL-SHAPED STRINGS IN THE UPLOAD SET:")
for f, pat, line in hits:
print(f" {f}: {pat} -> {line[:60]}")
sys.exit(1)
print("β
No live credentials in the upload set.")
# Live-credential shapes. The Space is public and the upload is the working tree, so
# a key pasted into any tracked file would be published β check rather than assume.
SECRET_PATTERNS = {
"OpenAI key": re.compile(r"sk-[A-Za-z0-9_\-]{20,}"),
"Tavily key": re.compile(r"tvly-[A-Za-z0-9_\-]{10,}"),
"HF token": re.compile(r"hf_[A-Za-z0-9]{30,}"),
"AWS key": re.compile(r"AKIA[0-9A-Z]{16}"),
}
def scan_for_secrets(files):
hits = []
for f in files:
p = ROOT / f
try:
text = p.read_text(errors="ignore")
except Exception:
continue # binary (PDF/sqlite) β skip
for name, pat in SECRET_PATTERNS.items():
for line in text.splitlines():
if pat.search(line) and "your_" not in line and "example" not in line.lower():
hits.append((f, name, line.strip()))
return hits
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--repo", default="tirtho149/agadvisor", help="HF repo id (user/space)")
ap.add_argument("--public", action="store_true", help="make the Space public (default: private)")
ap.add_argument("--dry-run", action="store_true",
help="print exactly which files WOULD be uploaded, then exit. Touches nothing.")
args = ap.parse_args()
if args.dry_run:
dry_run()
return
# Load .env first so HF_TOKEN can live there rather than being passed on the command
# line (shell history / process list are not places for a write-scoped token).
load_dotenv(ROOT / ".env")
token = os.environ.get("HF_TOKEN")
if not token:
sys.exit("β No HF_TOKEN. Put it in .env (HF_TOKEN=hf_...) or export it.")
api = HfApi(token=token)
# HF now gates ALL dynamic Spaces behind PRO: hosting a Gradio/Docker Space on
# even cpu-basic returns 402 for a free account (only Static Spaces are free).
# Given PRO is required regardless, cpu-basic (2 vCPU / 16 GB, always-on) is the
# right tier for this CPU-only app β ZeroGPU's GPU-quota model is a poor fit here.
print(f"β creating Space {args.repo} (private={not args.public}, hardware=cpu-basic) β¦")
api.create_repo(args.repo, repo_type="space", space_sdk="gradio",
space_hardware="cpu-basic",
private=not args.public, exist_ok=True)
# Secrets from .env (values read locally; not printed).
# SESSION_SECRET + HF_DATA_TOKEN power the accounts/persistent-history feature.
for k in ("OPENAI_API_KEY", "TAVILY_API_KEY", "OPENWEATHER_API_KEY",
"SESSION_SECRET", "HF_DATA_TOKEN"):
v = os.getenv(k)
if v:
api.add_space_secret(args.repo, k, v)
print(f" secret set: {k}")
else:
print(f" β οΈ {k} not in .env β set it in the HF Space UI")
# Ensure the private Dataset that durably stores accounts/history exists.
data_repo = os.getenv("HF_DATA_REPO")
if data_repo:
api.create_repo(data_repo, repo_type="dataset", private=True, exist_ok=True)
api.add_space_variable(args.repo, "HF_DATA_REPO", data_repo)
print(f" dataset ready + var set: HF_DATA_REPO={data_repo}")
else:
print(" β οΈ HF_DATA_REPO not set β user accounts/history will NOT persist "
"across restarts. Set it in .env (e.g. <user>/agadvisor-userdata).")
# Non-secret runtime vars: serve from the committed on-disk index, single process.
for k, v in (("CDMS_OFFLINE_INDEX", "1"), ("QDRANT_FORCE_LOCAL", "1")):
api.add_space_variable(args.repo, k, v)
print(f" var set: {k}={v}")
# HF-specific README + requirements + LICENSE (staged, then uploaded).
with tempfile.TemporaryDirectory() as td:
(Path(td) / "README.md").write_text(README)
(Path(td) / "LICENSE").write_text(LICENSE)
reqs = (ROOT / "requirements.txt").read_text()
(Path(td) / "requirements.txt").write_text(reqs.rstrip() + "\n\n# spaCy model\n" + SPACY_MODEL_LINE)
for name in ("README.md", "LICENSE", "requirements.txt"):
api.upload_file(path_or_fileobj=str(Path(td) / name),
path_in_repo=name, repo_id=args.repo, repo_type="space")
# The upload is the working tree and the Space may be public, so gate the push on
# the same checks --dry-run runs. Exits non-zero (before uploading) if either fails.
print("β pre-flight: checking the upload set for secrets / sensitive paths β¦")
dry_run(quiet=True)
print("β uploading app + committed index (excludes .env/secrets/junk) β¦")
api.upload_folder(repo_id=args.repo, repo_type="space", folder_path=str(ROOT),
ignore_patterns=IGNORE, commit_message="Deploy AgAdvisor")
print(f"\nβ
Done. Space building at: https://huggingface.co/spaces/{args.repo}")
print(" Watch the build logs there; first build is slow (torch/spacy).")
if __name__ == "__main__":
main()
|