| Marc Kupietz | 8604485 | 2025-11-29 10:19:03 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Download spaCy model with progress bar |
| 4 | """ |
| 5 | import sys |
| 6 | import subprocess |
| 7 | import re |
| 8 | |
| 9 | def main(): |
| 10 | if len(sys.argv) < 2: |
| 11 | print("Usage: download_with_progress.py MODEL_NAME") |
| 12 | sys.exit(1) |
| 13 | |
| 14 | model_name = sys.argv[1] |
| 15 | |
| 16 | print(f"Downloading {model_name}...", file=sys.stderr) |
| Marc Kupietz | 624b9c5 | 2025-11-29 11:09:08 +0100 | [diff] [blame] | 17 | print("This may take several minutes depending on model size", file=sys.stderr) |
| Marc Kupietz | 8604485 | 2025-11-29 10:19:03 +0100 | [diff] [blame] | 18 | print("", file=sys.stderr) |
| 19 | |
| 20 | # Run spacy download with unbuffered output |
| 21 | process = subprocess.Popen( |
| 22 | [sys.executable, "-u", "-m", "spacy", "download", model_name, "--no-cache-dir"], |
| 23 | stdout=subprocess.PIPE, |
| 24 | stderr=subprocess.STDOUT, |
| 25 | universal_newlines=True, |
| 26 | bufsize=1 |
| 27 | ) |
| 28 | |
| 29 | download_started = False |
| 30 | |
| 31 | for line in iter(process.stdout.readline, ''): |
| 32 | if not line: |
| 33 | break |
| 34 | |
| 35 | # Print the line |
| 36 | print(line.rstrip(), file=sys.stderr) |
| 37 | |
| 38 | # Detect download progress |
| 39 | if 'Downloading' in line and not download_started: |
| 40 | download_started = True |
| 41 | print("Download in progress...", file=sys.stderr) |
| 42 | |
| 43 | # Look for percentage or size indicators |
| 44 | if '%' in line or 'MB' in line or 'KB' in line: |
| 45 | # Extract and show progress |
| 46 | match = re.search(r'(\d+)%', line) |
| 47 | if match: |
| 48 | percent = match.group(1) |
| 49 | bar_length = 40 |
| 50 | filled = int(bar_length * int(percent) / 100) |
| 51 | bar = 'ā' * filled + 'ā' * (bar_length - filled) |
| 52 | print(f"\rProgress: [{bar}] {percent}%", end='', file=sys.stderr) |
| 53 | |
| 54 | process.stdout.close() |
| 55 | return_code = process.wait() |
| 56 | |
| 57 | if return_code != 0: |
| 58 | print(f"\nError: Download failed with code {return_code}", file=sys.stderr) |
| 59 | sys.exit(return_code) |
| 60 | |
| 61 | print("\nā Download complete!", file=sys.stderr) |
| 62 | return 0 |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | sys.exit(main()) |