File size: 7,318 Bytes
3784513 b9b5fd9 3784513 b9b5fd9 3784513 b9b5fd9 3784513 b9b5fd9 ebbcf47 41b9f9b 3784513 ebbcf47 3b68268 3e1bf17 3784513 b9b5fd9 3784513 d09a296 3784513 d09a296 3784513 3e1bf17 3b68268 3784513 b9b5fd9 3784513 b9b5fd9 3784513 | 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 | """Allowlisted incremental commits; no private runtime, dataset or secrets upload."""
import argparse
import hashlib
import json
import os
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
REPO = 'Wizzas/DefectRAG'
OUT = ROOT/'development-support/space'
def checked_frontend():
checked=subprocess.run(['node',str(ROOT/'deployment/space/frontend_release.mjs'),'check',str(OUT/'ui-dist')],
cwd=ROOT,capture_output=True,text=True,check=False,timeout=60)
if checked.returncode:
raise SystemExit('Frontend release blocked: '+checked.stderr.strip())
return json.loads(checked.stdout)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--phase', type=int, choices=(2,3,4,5))
parser.add_argument('--check-frontend',action='store_true',help='Offline readiness check only; never contacts Hugging Face')
args = parser.parse_args()
frontend=checked_frontend()
if args.check_frontend:
print(json.dumps({'verified':True,'files':len(frontend['files']),'source_fingerprint':frontend['source_fingerprint']}))
return
if args.phase is None:parser.error('--phase is required for an authorized deployment')
from huggingface_hub import HfApi, CommitOperationAdd
if args.phase>=4:
confirmation=OUT/'migration-confirmed.json'
assert confirmation.exists() and json.loads(confirmation.read_text()).get('user_confirmed_manual_execution'), 'Wait for the user to confirm manual SQL execution before enabling staff persistence.'
from deployment.space.supabase_repository import read_public_config
read_public_config(OUT/'ui-dist/config.js') # Refuse a blank example before changing a working Space.
files = {}
for relative in ('docs/THIRD_PARTY_NOTICES.md','docs/FRONTEND_LICENSES.txt','models/BGE-LICENSE.txt'):
path=ROOT/relative
if path.exists():
files[relative]=path
for relative in ('results/sensitive_deployment_evaluation.json','results/macaroni_recall_deployment_evaluation.json'):
files[relative]=ROOT/relative
for directory in ('backend','deployment/space'):
for path in (ROOT/directory).glob('*.py'):
files[path.relative_to(ROOT).as_posix()] = path
for name in frontend['files']:
files['frontend/dist/'+name] = OUT/'ui-dist'/name
frontend_bytes={name:files['frontend/dist/'+name].read_bytes() for name in frontend['files']}
if any(hashlib.sha256(content).hexdigest()!=frontend['files'][name] for name,content in frontend_bytes.items()):
raise SystemExit('Frontend changed while assembling release')
for path in (ROOT/'models/highres').glob('*.npz'):
files[path.relative_to(ROOT).as_posix()] = path
if args.phase>=3:
for path in (ROOT/'models/embedding').rglob('*'):
if path.is_file() and path.suffix not in ('.py','.pyc'):
files[path.relative_to(ROOT).as_posix()] = path
for path in (ROOT/'knowledge').glob('*.md'):
files[path.relative_to(ROOT).as_posix()] = path
requirements = (ROOT/'deployment/zerogpu_phase1/requirements-space.txt').read_text(encoding='utf-8')
# The platform's unused preinstalled datasets 5 requires Hub <2. Gradio needs
# >=1.16; pin the compatible intersection rather than upgrading unrelated libs.
requirements = requirements.replace('huggingface-hub==2.2.0','huggingface-hub==1.31.0')
root_app = ("import os\nfrom pathlib import Path\n"
"os.environ['DEFECTRAG_SPACE_UI']=str(Path(__file__).parent/'frontend/dist')\n"
"os.environ['GRADIO_ANALYTICS_ENABLED']='False'\n"
f"os.environ['DEFECTRAG_LIVE_RAG']='{'1' if args.phase>=3 else '0'}'\n"
f"os.environ['DEFECTRAG_STAFF_BACKEND']='{'1' if args.phase>=4 else '0'}'\n"
"from deployment.space.app import server\n"
"server.launch(server_name='0.0.0.0',server_port=7860,ssr_mode=False,show_error=True,run_history=False)\n")
readme = (ROOT/'deployment/zerogpu_phase1/SPACE_README.md').read_text(encoding='utf-8')
header = readme.split('---',2)[1].replace('DefectRAG - Phase 1 compatibility test','DefectRAG').replace('Existing React UI plus one genuine DINOv2 ZeroGPU inspection','Live React dashboard and DINOv2 inspection on ZeroGPU')
body = f'''\n# DefectRAG — migration phase {args.phase}\n
Existing React UI, real frozen DINOv2 inspection across all 12 VisA categories,
original normal-reference banks and heatmaps, with versioned normal-only decision
calibration. No training rerun. The q95 sensitivity policy trades more false
alarms for higher recall; Macaroni1/2 use the more sensitive q80 decision policy.
Historical inspection decisions are preserved.
Anonymous browser history is isolated and temporary. Sign in to the configured
staff workspace for durable shared records when available. Do not treat anomaly
scores as probabilities or diagnoses. The operator selects the product category.
Gradio client requests use bounded GPU leases. Quotas and cold starts apply.
Local application and static showcase are preserved separately.
VisA: Amazon Science, CC BY 4.0, Zou et al., ECCV 2022.
DINOv2: Meta, Apache-2.0. See docs/THIRD_PARTY_NOTICES.md.
'''
raw = {'app.py':root_app.encode(),'requirements.txt':requirements.encode(),
'README.md':('---'+header+'---\n'+body).encode()}
assert not any(part in ('.env','runtime','node_modules','experiments','.venv') for name in files for part in Path(name).parts)
api = HfApi()
before = api.space_info(REPO)
assert api.get_space_runtime(REPO).requested_hardware=='zero-a10g'
# Deduplicate unchanged allowlisted files between phase commits.
state_file = OUT/'published-hashes.json'
previous = json.loads(state_file.read_text()) if state_file.exists() else {}
hashes = {name:hashlib.sha256(path.read_bytes()).hexdigest() for name,path in files.items()}
hashes.update({name:hashlib.sha256(value).hexdigest() for name,value in raw.items()})
operations = [CommitOperationAdd(path_in_repo=name,path_or_fileobj=frontend_bytes[name.removeprefix('frontend/dist/')] if name.startswith('frontend/dist/')
else path.read_bytes() if path.suffix in ('.py','.js','.json','.md','.html','.css','.svg') else path)
for name,path in files.items() if previous.get(name)!=hashes[name]]
operations += [CommitOperationAdd(path_in_repo=name,path_or_fileobj=value)
for name,value in raw.items() if previous.get(name)!=hashes[name]]
if checked_frontend()!=frontend:raise SystemExit('Frontend inputs changed before publication; rebuild and verify')
commit = api.create_commit(repo_id=REPO,repo_type='space',operations=operations,
parent_commit=before.sha,commit_message=f'DefectRAG phase {args.phase}: incremental real-model application migration')
state_file.write_text(json.dumps(hashes,indent=2))
receipt = {'phase':args.phase,'commit':commit.oid,'parent':before.sha,'changed_files':len(operations),'manifest':hashes}
(OUT/f'publish-phase{args.phase}.json').write_text(json.dumps(receipt,indent=2))
print(json.dumps({key:value for key,value in receipt.items() if key!='manifest'}))
if __name__=='__main__':
main()
|