HistoPath / clean_requirements.py
ryanDing26
Please work
be11320
"""
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)