File size: 1,888 Bytes
be11320 |
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 |
"""
Clean pip freeze output for HuggingFace Spaces deployment
Usage: python clean_requirements.py < raw_freeze.txt > requirements.txt
"""
import sys
import re
def clean_requirements(lines):
"""Clean pip freeze output for deployment."""
cleaned = []
for line in lines:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith('#'):
continue
# Skip editable installs (local packages)
if line.startswith('-e') or line.startswith('file://'):
print(f"# Skipped local package: {line}", file=sys.stderr)
continue
# Skip packages installed from git
if 'git+' in line or '@' in line and 'git' in line:
print(f"# Skipped git package: {line}", file=sys.stderr)
continue
# Remove local version identifiers (+cpu, +cu118, etc)
# torch==2.0.1+cpu -> torch==2.0.1
line = re.sub(r'\+[a-z0-9]+', '', line)
# Remove platform-specific wheel info
# some-package @ file:///... -> some-package==version
if ' @ ' in line:
# Extract just package name and try to find version
pkg_name = line.split(' @ ')[0].strip()
print(f"# Simplified: {line} -> {pkg_name}", file=sys.stderr)
# Try to keep just the package name without version
# HF Spaces will resolve the version
cleaned.append(pkg_name)
continue
cleaned.append(line)
return cleaned
if __name__ == '__main__':
lines = sys.stdin.readlines()
cleaned = clean_requirements(lines)
print("# Requirements generated from pip freeze")
print("# Cleaned for HuggingFace Spaces deployment\n")
for line in sorted(set(cleaned)): # Remove duplicates and sort
print(line) |