Upload folder using huggingface_hub
Browse files- README.md +33 -33
- deploy_spaces.py +121 -14
README.md
CHANGED
|
@@ -1,33 +1,33 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: AnomalyMachine-50K Demo
|
| 3 |
-
colorFrom: red
|
| 4 |
-
colorTo: gray
|
| 5 |
-
sdk: gradio
|
| 6 |
-
sdk_version: 4.44.0
|
| 7 |
-
app_file: app.py
|
| 8 |
-
pinned: true
|
| 9 |
-
license: cc-by-4.0
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
# AnomalyMachine-50K Demo
|
| 13 |
-
|
| 14 |
-
Interactive demo for the AnomalyMachine-50K synthetic industrial machine sound anomaly detection dataset.
|
| 15 |
-
|
| 16 |
-
## Features
|
| 17 |
-
|
| 18 |
-
- **Anomaly Detection**: Upload audio or record live to detect anomalies in machine sounds
|
| 19 |
-
- **Dataset Exploration**: Browse example spectrograms for each machine type
|
| 20 |
-
- **Visual Analysis**: Compare input audio against reference normal samples
|
| 21 |
-
|
| 22 |
-
## Dataset
|
| 23 |
-
|
| 24 |
-
- **50,000** synthetic audio clips
|
| 25 |
-
- **6** machine types (fan, pump, compressor, conveyor_belt, electric_motor, valve)
|
| 26 |
-
- **5** anomaly types (bearing_fault, imbalance, cavitation, overheating, obstruction)
|
| 27 |
-
- **10 seconds** per clip at **22,050 Hz**
|
| 28 |
-
|
| 29 |
-
[View Dataset →](https://huggingface.co/datasets/AnomalyMachine-50K)
|
| 30 |
-
|
| 31 |
-
## License
|
| 32 |
-
|
| 33 |
-
CC-BY-4.0
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: AnomalyMachine-50K Demo
|
| 3 |
+
colorFrom: red
|
| 4 |
+
colorTo: gray
|
| 5 |
+
sdk: gradio
|
| 6 |
+
sdk_version: 4.44.0
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: true
|
| 9 |
+
license: cc-by-4.0
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# AnomalyMachine-50K Demo
|
| 13 |
+
|
| 14 |
+
Interactive demo for the AnomalyMachine-50K synthetic industrial machine sound anomaly detection dataset.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- **Anomaly Detection**: Upload audio or record live to detect anomalies in machine sounds
|
| 19 |
+
- **Dataset Exploration**: Browse example spectrograms for each machine type
|
| 20 |
+
- **Visual Analysis**: Compare input audio against reference normal samples
|
| 21 |
+
|
| 22 |
+
## Dataset
|
| 23 |
+
|
| 24 |
+
- **50,000** synthetic audio clips
|
| 25 |
+
- **6** machine types (fan, pump, compressor, conveyor_belt, electric_motor, valve)
|
| 26 |
+
- **5** anomaly types (bearing_fault, imbalance, cavitation, overheating, obstruction)
|
| 27 |
+
- **10 seconds** per clip at **22,050 Hz**
|
| 28 |
+
|
| 29 |
+
[View Dataset →](https://huggingface.co/datasets/AnomalyMachine-50K)
|
| 30 |
+
|
| 31 |
+
## License
|
| 32 |
+
|
| 33 |
+
CC-BY-4.0
|
deploy_spaces.py
CHANGED
|
@@ -8,15 +8,74 @@ from pathlib import Path
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from huggingface_hub import HfApi, upload_folder
|
| 10 |
|
| 11 |
-
# Load environment variables
|
| 12 |
-
load_dotenv()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def ensure_hf_token() -> str:
|
| 16 |
"""Load the Hugging Face token from environment variables."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
token = os.getenv("HF_TOKEN")
|
|
|
|
| 18 |
if not token:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
raise EnvironmentError("HF_TOKEN is not set in environment or .env file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return token
|
| 21 |
|
| 22 |
|
|
@@ -58,7 +117,20 @@ def deploy_space(
|
|
| 58 |
private: bool = False,
|
| 59 |
) -> None:
|
| 60 |
"""Deploy the demo to Hugging Face Spaces."""
|
| 61 |
-
api = HfApi()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Check if repo exists
|
| 64 |
try:
|
|
@@ -66,13 +138,25 @@ def deploy_space(
|
|
| 66 |
print(f"Space '{repo_id}' already exists. Updating...")
|
| 67 |
except Exception:
|
| 68 |
print(f"Creating new Space '{repo_id}'...")
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Copy example audio files from generated dataset
|
| 78 |
examples_dir = spaces_demo_dir / "examples"
|
|
@@ -82,6 +166,28 @@ def deploy_space(
|
|
| 82 |
else:
|
| 83 |
print(f"Warning: Output directory not found at {output_dir}. Skipping example files.")
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# Upload all files in spaces_demo directory
|
| 86 |
print(f"Uploading files from {spaces_demo_dir}...")
|
| 87 |
upload_folder(
|
|
@@ -129,11 +235,12 @@ def main() -> None:
|
|
| 129 |
# Determine paths
|
| 130 |
script_dir = Path(__file__).resolve().parent
|
| 131 |
spaces_demo_dir = script_dir
|
|
|
|
| 132 |
output_dir = Path(args.output_dir).resolve()
|
| 133 |
if not output_dir.is_absolute():
|
| 134 |
-
output_dir =
|
| 135 |
|
| 136 |
-
token = ensure_hf_token()
|
| 137 |
|
| 138 |
deploy_space(
|
| 139 |
spaces_demo_dir=spaces_demo_dir,
|
|
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from huggingface_hub import HfApi, upload_folder
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def ensure_hf_token(project_root: Path) -> str:
|
|
|
|
| 13 |
"""Load the Hugging Face token from environment variables."""
|
| 14 |
+
# Try loading from project root .env file first
|
| 15 |
+
env_file = project_root / ".env"
|
| 16 |
+
env_loaded = False
|
| 17 |
+
|
| 18 |
+
if env_file.exists():
|
| 19 |
+
print(f"Loading .env from: {env_file}")
|
| 20 |
+
load_dotenv(env_file, override=True)
|
| 21 |
+
env_loaded = True
|
| 22 |
+
else:
|
| 23 |
+
# Fallback: try current directory
|
| 24 |
+
print("Project root .env not found, trying current directory...")
|
| 25 |
+
load_dotenv()
|
| 26 |
+
|
| 27 |
+
# Also try spaces_demo/.env if it exists
|
| 28 |
+
spaces_env = project_root / "spaces_demo" / ".env"
|
| 29 |
+
if spaces_env.exists():
|
| 30 |
+
print(f"Also loading .env from: {spaces_env}")
|
| 31 |
+
load_dotenv(spaces_env, override=True)
|
| 32 |
+
|
| 33 |
+
# Check environment variable
|
| 34 |
token = os.getenv("HF_TOKEN")
|
| 35 |
+
|
| 36 |
if not token:
|
| 37 |
+
print("\n" + "="*60)
|
| 38 |
+
print("ERROR: HF_TOKEN not found!")
|
| 39 |
+
print("="*60)
|
| 40 |
+
print(f"\nChecked locations:")
|
| 41 |
+
print(f" 1. {env_file} {'✓' if env_file.exists() else '✗ (not found)'}")
|
| 42 |
+
print(f" 2. {spaces_env} {'✓' if spaces_env.exists() else '✗ (not found)'}")
|
| 43 |
+
print(f" 3. Environment variable: {'✓' if os.getenv('HF_TOKEN') else '✗ (not set)'}")
|
| 44 |
+
print("\nTo fix this:")
|
| 45 |
+
print("1. Create or edit .env file in the project root:")
|
| 46 |
+
print(f" {env_file}")
|
| 47 |
+
print("2. Add your Hugging Face token (must start with 'hf_'):")
|
| 48 |
+
print(" HF_TOKEN=hf_your_actual_token_here")
|
| 49 |
+
print("\nOr set it as an environment variable:")
|
| 50 |
+
print(" $env:HF_TOKEN='hf_your_token' # PowerShell")
|
| 51 |
+
print(" export HF_TOKEN='hf_your_token' # Bash")
|
| 52 |
+
print("\nGet your token from: https://huggingface.co/settings/tokens")
|
| 53 |
+
print("="*60 + "\n")
|
| 54 |
raise EnvironmentError("HF_TOKEN is not set in environment or .env file.")
|
| 55 |
+
|
| 56 |
+
# Remove any quotes or whitespace
|
| 57 |
+
token = token.strip().strip('"').strip("'").strip()
|
| 58 |
+
|
| 59 |
+
# Show first few characters for debugging (without exposing full token)
|
| 60 |
+
token_preview = token[:10] + "..." if len(token) > 10 else token[:len(token)]
|
| 61 |
+
print(f"Token loaded: {token_preview} (length: {len(token)})")
|
| 62 |
+
|
| 63 |
+
# Basic validation
|
| 64 |
+
if not token.startswith("hf_"):
|
| 65 |
+
print("\n" + "="*60)
|
| 66 |
+
print("ERROR: Invalid token format!")
|
| 67 |
+
print("="*60)
|
| 68 |
+
print(f"\nYour token starts with: '{token[:5]}...'")
|
| 69 |
+
print("But it should start with: 'hf_'")
|
| 70 |
+
print("\nCommon issues:")
|
| 71 |
+
print("1. Token might have extra spaces or quotes")
|
| 72 |
+
print("2. Token might be incomplete")
|
| 73 |
+
print("3. You might be using an old token format")
|
| 74 |
+
print("\nGet a new token from: https://huggingface.co/settings/tokens")
|
| 75 |
+
print("Make sure to copy the FULL token (it should start with 'hf_')")
|
| 76 |
+
print("="*60 + "\n")
|
| 77 |
+
raise ValueError("HF_TOKEN must start with 'hf_'. Please check your token.")
|
| 78 |
+
|
| 79 |
return token
|
| 80 |
|
| 81 |
|
|
|
|
| 117 |
private: bool = False,
|
| 118 |
) -> None:
|
| 119 |
"""Deploy the demo to Hugging Face Spaces."""
|
| 120 |
+
api = HfApi(token=token)
|
| 121 |
+
|
| 122 |
+
# Verify token works by checking user info
|
| 123 |
+
try:
|
| 124 |
+
user_info = api.whoami(token=token)
|
| 125 |
+
print(f"Authenticated as: {user_info.get('name', 'Unknown')}")
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f"\n❌ Authentication failed: {e}")
|
| 128 |
+
print("\nTroubleshooting:")
|
| 129 |
+
print("1. Your HF_TOKEN may be invalid or expired")
|
| 130 |
+
print("2. Get a new token at: https://huggingface.co/settings/tokens")
|
| 131 |
+
print("3. Make sure the token has 'write' permissions")
|
| 132 |
+
print("4. Update your .env file with the new token")
|
| 133 |
+
raise
|
| 134 |
|
| 135 |
# Check if repo exists
|
| 136 |
try:
|
|
|
|
| 138 |
print(f"Space '{repo_id}' already exists. Updating...")
|
| 139 |
except Exception:
|
| 140 |
print(f"Creating new Space '{repo_id}'...")
|
| 141 |
+
try:
|
| 142 |
+
api.create_repo(
|
| 143 |
+
repo_id=repo_id,
|
| 144 |
+
token=token,
|
| 145 |
+
repo_type="space",
|
| 146 |
+
exist_ok=True,
|
| 147 |
+
space_sdk="gradio",
|
| 148 |
+
)
|
| 149 |
+
print(f"✅ Space created successfully!")
|
| 150 |
+
except Exception as create_error:
|
| 151 |
+
print(f"\n❌ Error creating Space: {create_error}")
|
| 152 |
+
print("\nTroubleshooting:")
|
| 153 |
+
print("1. Verify your HF_TOKEN is valid and not expired")
|
| 154 |
+
print("2. Check that the token has 'write' permissions")
|
| 155 |
+
print("3. Get a new token at: https://huggingface.co/settings/tokens")
|
| 156 |
+
username = repo_id.split('/')[0]
|
| 157 |
+
print(f"4. Make sure your username '{username}' is correct")
|
| 158 |
+
print(f"5. Check if you have permission to create Spaces under '{username}'")
|
| 159 |
+
raise
|
| 160 |
|
| 161 |
# Copy example audio files from generated dataset
|
| 162 |
examples_dir = spaces_demo_dir / "examples"
|
|
|
|
| 166 |
else:
|
| 167 |
print(f"Warning: Output directory not found at {output_dir}. Skipping example files.")
|
| 168 |
|
| 169 |
+
# Ensure README.md doesn't have emoji field (fix encoding issues)
|
| 170 |
+
readme_path = spaces_demo_dir / "README.md"
|
| 171 |
+
if readme_path.exists():
|
| 172 |
+
print("Ensuring README.md has correct format...")
|
| 173 |
+
with open(readme_path, "r", encoding="utf-8") as f:
|
| 174 |
+
content = f.read()
|
| 175 |
+
|
| 176 |
+
# Remove emoji line if it exists (to avoid encoding issues)
|
| 177 |
+
lines = content.split("\n")
|
| 178 |
+
filtered_lines = []
|
| 179 |
+
for line in lines:
|
| 180 |
+
if line.strip().startswith("emoji:"):
|
| 181 |
+
print(f" Removing emoji line: {line.strip()}")
|
| 182 |
+
continue
|
| 183 |
+
filtered_lines.append(line)
|
| 184 |
+
|
| 185 |
+
# Write back without emoji
|
| 186 |
+
content_fixed = "\n".join(filtered_lines)
|
| 187 |
+
with open(readme_path, "w", encoding="utf-8", newline="\n") as f:
|
| 188 |
+
f.write(content_fixed)
|
| 189 |
+
print("README.md cleaned and saved with UTF-8 encoding.")
|
| 190 |
+
|
| 191 |
# Upload all files in spaces_demo directory
|
| 192 |
print(f"Uploading files from {spaces_demo_dir}...")
|
| 193 |
upload_folder(
|
|
|
|
| 235 |
# Determine paths
|
| 236 |
script_dir = Path(__file__).resolve().parent
|
| 237 |
spaces_demo_dir = script_dir
|
| 238 |
+
project_root = script_dir.parent
|
| 239 |
output_dir = Path(args.output_dir).resolve()
|
| 240 |
if not output_dir.is_absolute():
|
| 241 |
+
output_dir = project_root / args.output_dir
|
| 242 |
|
| 243 |
+
token = ensure_hf_token(project_root)
|
| 244 |
|
| 245 |
deploy_space(
|
| 246 |
spaces_demo_dir=spaces_demo_dir,
|