File size: 2,819 Bytes
1aa7fae ec7fe2c 1aa7fae ec7fe2c 1aa7fae | 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 | """
Helper script to push the Streamlit app and deployment files
to a Hugging Face Space.
This script mirrors the behaviour of the notebook's `hosting.py`:
- Ensures the Space exists (creating it if necessary),
- Uploads only the files required for deployment (code, Dockerfile,
and requirements), excluding data, MLflow artifacts, etc.
"""
from __future__ import annotations
import os
from huggingface_hub import HfApi
import config
def main() -> None:
token = config.HF_TOKEN or os.getenv("HF_TOKEN")
if not token:
raise ValueError(
"HF_TOKEN is not set. Please export HF_TOKEN or configure it in config.py."
)
space_repo = config.HF_SPACE_REPO or os.getenv("HF_SPACE_REPO")
if not space_repo:
raise ValueError(
"HF_SPACE_REPO is not set. Set it as an environment variable or in config.py."
)
api = HfApi(token=token)
# Create the Space if it does not exist
# Use "docker" SDK since we're deploying with Dockerfile
api.create_repo(
repo_id=space_repo,
repo_type="space",
space_sdk="docker",
exist_ok=True,
)
# Create Space README with proper configuration
space_readme = """---
title: Engine Predictive Maintenance
emoji: 🔧
colorFrom: blue
colorTo: red
sdk: docker
app_file: src/app.py
pinned: false
---
# Engine Predictive Maintenance System
Predictive maintenance application for engine failure classification using sensor data.
## Features
- Real-time engine condition prediction
- Interactive sensor visualizations
- Model inference from trained Random Forest classifier
- Modern Streamlit interface
## Usage
Enter sensor values (RPM, pressures, temperatures) to get real-time predictions about engine health.
## Model
Trained Random Forest model with hyperparameter tuning, deployed from Hugging Face Model Hub.
"""
# Upload Space README first
api.upload_file(
path_or_fileobj=space_readme.encode(),
path_in_repo="README.md",
repo_id=space_repo,
repo_type="space",
)
# Upload project files needed for deployment.
# We ignore large local artifacts like raw data, mlruns, and models,
# similar to how the reference notebook uploads only the deployment folder.
api.upload_folder(
folder_path=str(config.PROJECT_ROOT),
path_in_repo=".",
repo_id=space_repo,
repo_type="space",
ignore_patterns=[
"data/*",
"mlruns/*",
"models/*",
".git/*",
"__pycache__/*",
".github/*",
"README.md", # Don't upload project README, use Space-specific one
],
)
print(f"Deployment files pushed to Hugging Face Space: {space_repo}")
if __name__ == "__main__":
main()
|