File size: 2,444 Bytes
ac5551c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Download model during Docker build on Hugging Face Spaces

"""
import os
import requests
import gc

R2_BASE_URL = "https://r2-worker.eczemanage.workers.dev"
OUTPUT_DIR = "./derm_foundation/"

def download_model():
    print("=" * 70)
    print("DOWNLOADING DERM FOUNDATION MODEL (Hugging Face Spaces Build)")
    print("=" * 70)
    
    os.makedirs(OUTPUT_DIR, exist_ok=True)
    
    files = [
        "saved_model.pb",
        "variables/variables.index",
        "variables/variables.data-00000-of-00001"
    ]
    
    for file_path in files:
        print(f"\n📥 Downloading {file_path}...")
        url = f"{R2_BASE_URL}/{file_path}"
        local_path = os.path.join(OUTPUT_DIR, file_path)
        os.makedirs(os.path.dirname(local_path), exist_ok=True)
        
        try:
            with requests.get(url, stream=True, timeout=1800) as r:
                r.raise_for_status()
                total_size = int(r.headers.get('content-length', 0))
                downloaded = 0
                chunk_count = 0
                
                with open(local_path, 'wb') as f:
                    for chunk in r.iter_content(chunk_size=2*1024*1024):  # 2MB chunks (HF has RAM)
                        if chunk:
                            f.write(chunk)
                            f.flush()
                            downloaded += len(chunk)
                            chunk_count += 1
                            
                            if chunk_count % 5 == 0:
                                gc.collect()
                            
                            if total_size > 0 and chunk_count % 10 == 0:
                                progress = (downloaded / total_size) * 100
                                mb_downloaded = downloaded / (1024*1024)
                                mb_total = total_size / (1024*1024)
                                print(f"  Progress: {progress:.1f}% ({mb_downloaded:.1f}/{mb_total:.1f} MB)")
                
                gc.collect()
            
            print(f"✅ Successfully downloaded: {file_path}")
            
        except Exception as e:
            print(f"❌ Error downloading {file_path}: {e}")
            raise
    
    print("\n" + "=" * 70)
    print("✅ MODEL DOWNLOAD COMPLETE! Ready to serve predictions.")
    print("=" * 70)

if __name__ == "__main__":
    download_model()