File size: 4,679 Bytes
12d64f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f16aa1
12d64f8
 
 
 
 
6f16aa1
12d64f8
 
 
6f16aa1
12d64f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f16aa1
12d64f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f16aa1
12d64f8
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
"""
Automatic model downloader for Qwen2.5-0.5B-Instruct
Downloads from HuggingFace and places in correct location
"""
import os
import sys
import urllib.request
from pathlib import Path
from typing import Optional

try:
    from tqdm import tqdm
    HAS_TQDM = True
except ImportError:
    HAS_TQDM = False


class DownloadProgressBar:
    """Progress bar for download"""
    def __init__(self):
        self.pbar = None

    def __call__(self, block_num, block_size, total_size):
        if HAS_TQDM:
            if not self.pbar:
                self.pbar = tqdm(
                    total=total_size,
                    unit='B',
                    unit_scale=True,
                    unit_divisor=1024,
                    desc='Downloading model'
                )
            downloaded = block_num * block_size
            if downloaded < total_size:
                self.pbar.update(block_size)
            else:
                self.pbar.close()
        else:
            # Simple progress without tqdm
            if total_size > 0:
                downloaded = block_num * block_size
                percent = min(100, (downloaded / total_size) * 100)
                if block_num % 100 == 0:  # Print every 100 blocks
                    print(f"Progress: {percent:.1f}%", end='\r')


def download_model(destination: Optional[str] = None):
    """
    Download Qwen2.5-0.5B-Instruct-Q4_0 GGUF model
    
    Args:
        destination: Target path for model file. 
                    Defaults to /home/luigi/rts/qwen2.5-coder-1.5b-instruct-q4_0.gguf
    
    Returns:
        str: Path to downloaded model
    """
    # Model URL from HuggingFace
    MODEL_URL = "https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF/resolve/main/qwen2.5-coder-1.5b-instruct-q4_0.gguf"
    
    # Default destination
    if destination is None:
        destination = "/home/luigi/rts/qwen2.5-coder-1.5b-instruct-q4_0.gguf"
    
    destination_path = Path(destination)
    
    # Check if already exists
    if destination_path.exists():
        size_mb = destination_path.stat().st_size / (1024 * 1024)
        print(f"โœ… Model already exists: {destination_path}")
        print(f"   Size: {size_mb:.1f} MB")
        return str(destination_path)
    
    # Create parent directory if needed
    destination_path.parent.mkdir(parents=True, exist_ok=True)
    
    print(f"๐Ÿ“ฆ Downloading Qwen2.5-0.5B-Instruct model...")
    print(f"   Source: {MODEL_URL}")
    print(f"   Destination: {destination_path}")
    print(f"   Expected size: ~350 MB")
    print()
    
    try:
        # Download with progress bar
        urllib.request.urlretrieve(
            MODEL_URL,
            destination_path,
            reporthook=DownloadProgressBar()
        )
        
        # Verify download
        if destination_path.exists():
            size_mb = destination_path.stat().st_size / (1024 * 1024)
            print()
            print(f"โœ… Model downloaded successfully!")
            print(f"   Location: {destination_path}")
            print(f"   Size: {size_mb:.1f} MB")
            return str(destination_path)
        else:
            print("โŒ Download failed: File not created")
            return None
            
    except Exception as e:
        print(f"โŒ Download error: {e}")
        # Clean up partial download
        if destination_path.exists():
            destination_path.unlink()
        return None


def main():
    """Main entry point"""
    import argparse
    
    parser = argparse.ArgumentParser(
        description='Download Qwen2.5-0.5B-Instruct model for tactical analysis'
    )
    parser.add_argument(
        '--destination',
        '-d',
        type=str,
        default=None,
        help='Destination path (default: /home/luigi/rts/qwen2.5-coder-1.5b-instruct-q4_0.gguf)'
    )
    
    args = parser.parse_args()
    
    print("=" * 60)
    print("  Qwen2.5-0.5B-Instruct Model Downloader")
    print("=" * 60)
    print()
    
    model_path = download_model(args.destination)
    
    if model_path:
        print()
        print("๐ŸŽ‰ Setup complete! The AI tactical analysis system is ready.")
        print()
        print("To use:")
        print("  1. Restart the web server: python3 web/app.py")
        print("  2. The model will be automatically loaded")
        print("  3. Tactical analysis will run every 30 seconds")
        sys.exit(0)
    else:
        print()
        print("โŒ Setup failed. Please try again or download manually from:")
        print("   https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF")
        sys.exit(1)


if __name__ == '__main__':
    main()