Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- api/startup.py +53 -0
- app.py +1 -1
api/startup.py
CHANGED
|
@@ -23,6 +23,55 @@ from api.logger import init_logger
|
|
| 23 |
STARTUP_TIME = None
|
| 24 |
MODEL_VERSION = "v1.0"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def load_all():
|
| 28 |
"""
|
|
@@ -36,6 +85,10 @@ def load_all():
|
|
| 36 |
print("AnomalyOS startup sequence")
|
| 37 |
print("=" * 50)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# ββ CPU thread tuning βββββββββββββββββββββββββββββββββββββ
|
| 40 |
# HF Spaces CPU Basic = 2 vCPU
|
| 41 |
# Limit PyTorch threads to match β prevents over-subscription
|
|
|
|
| 23 |
STARTUP_TIME = None
|
| 24 |
MODEL_VERSION = "v1.0"
|
| 25 |
|
| 26 |
+
def download_artifacts():
|
| 27 |
+
"""Download all required artifacts from HF Dataset at startup."""
|
| 28 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 29 |
+
import shutil
|
| 30 |
+
|
| 31 |
+
HF_REPO = "CaffeinatedCoding/anomalyos-logs"
|
| 32 |
+
token = os.environ.get("HF_TOKEN")
|
| 33 |
+
|
| 34 |
+
os.makedirs("data", exist_ok=True)
|
| 35 |
+
|
| 36 |
+
files_to_download = [
|
| 37 |
+
("models/pca_256.pkl", "data/pca_256.pkl"),
|
| 38 |
+
("configs/thresholds.json", "data/thresholds.json"),
|
| 39 |
+
("graph/knowledge_graph.json", "data/knowledge_graph.json"),
|
| 40 |
+
("indexes/index1_category.faiss", "data/index1_category.faiss"),
|
| 41 |
+
("indexes/index1_metadata.json", "data/index1_metadata.json"),
|
| 42 |
+
("indexes/index2_defect.faiss", "data/index2_defect.faiss"),
|
| 43 |
+
("indexes/index2_metadata.json", "data/index2_metadata.json"),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Index 3 β one per category
|
| 47 |
+
categories = [
|
| 48 |
+
'bottle','cable','capsule','carpet','grid','hazelnut',
|
| 49 |
+
'leather','metal_nut','pill','screw','tile','toothbrush',
|
| 50 |
+
'transistor','wood','zipper'
|
| 51 |
+
]
|
| 52 |
+
for cat in categories:
|
| 53 |
+
files_to_download.append((
|
| 54 |
+
f"indexes/index3_{cat}.faiss",
|
| 55 |
+
f"data/index3_{cat}.faiss"
|
| 56 |
+
))
|
| 57 |
+
|
| 58 |
+
for repo_path, local_path in files_to_download:
|
| 59 |
+
if os.path.exists(local_path):
|
| 60 |
+
print(f"Already exists: {local_path}")
|
| 61 |
+
continue
|
| 62 |
+
try:
|
| 63 |
+
print(f"Downloading {repo_path}...")
|
| 64 |
+
downloaded = hf_hub_download(
|
| 65 |
+
repo_id=HF_REPO,
|
| 66 |
+
filename=repo_path,
|
| 67 |
+
repo_type="dataset",
|
| 68 |
+
token=token,
|
| 69 |
+
local_dir="/tmp/artifacts"
|
| 70 |
+
)
|
| 71 |
+
shutil.copy(downloaded, local_path)
|
| 72 |
+
print(f" β {local_path}")
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f" WARNING: Could not download {repo_path}: {e}")
|
| 75 |
|
| 76 |
def load_all():
|
| 77 |
"""
|
|
|
|
| 85 |
print("AnomalyOS startup sequence")
|
| 86 |
print("=" * 50)
|
| 87 |
|
| 88 |
+
# Download artifacts first
|
| 89 |
+
download_artifacts()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
# ββ CPU thread tuning βββββββββββββββββββββββββββββββββββββ
|
| 93 |
# HF Spaces CPU Basic = 2 vCPU
|
| 94 |
# Limit PyTorch threads to match β prevents over-subscription
|
app.py
CHANGED
|
@@ -348,6 +348,6 @@ with gr.Blocks(title="AnomalyOS", theme=gr.themes.Soft()) as demo:
|
|
| 348 |
btn_refresh = gr.Button("π Refresh")
|
| 349 |
analytics_out = gr.Textbox(label="System Stats", lines=15)
|
| 350 |
|
| 351 |
-
btn_refresh.click(
|
| 352 |
fn=load_analytics,
|
| 353 |
inputs=[],
|
|
|
|
| 348 |
btn_refresh = gr.Button("π Refresh")
|
| 349 |
analytics_out = gr.Textbox(label="System Stats", lines=15)
|
| 350 |
|
| 351 |
+
btn_refresh.click()
|
| 352 |
fn=load_analytics,
|
| 353 |
inputs=[],
|