File size: 6,015 Bytes
957df8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
"""
Setup script for Diabetic Retinopathy Detection App
Helps with installation and environment setup
"""

import subprocess
import sys
import os
import platform

def check_python_version():
    """Check if Python version is compatible."""
    print("🐍 Checking Python version...")
    version = sys.version_info
    if version.major < 3 or (version.major == 3 and version.minor < 8):
        print(f"❌ Python {version.major}.{version.minor} detected. Python 3.8+ is required.")
        return False
    print(f"βœ… Python {version.major}.{version.minor}.{version.micro} - Compatible!")
    return True

def check_pip():
    """Check if pip is available."""
    print("πŸ“¦ Checking pip...")
    try:
        subprocess.run([sys.executable, "-m", "pip", "--version"], 
                      check=True, capture_output=True)
        print("βœ… pip is available!")
        return True
    except subprocess.CalledProcessError:
        print("❌ pip not found. Please install pip first.")
        return False

def upgrade_pip():
    """Upgrade pip to latest version."""
    print("πŸ”„ Upgrading pip...")
    try:
        subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], 
                      check=True)
        print("βœ… pip upgraded successfully!")
        return True
    except subprocess.CalledProcessError as e:
        print(f"⚠️  Warning: Could not upgrade pip: {e}")
        return False

def install_requirements():
    """Install required packages from requirements.txt."""
    print("πŸ“₯ Installing required packages...")
    
    if not os.path.exists("requirements.txt"):
        print("❌ requirements.txt not found!")
        return False
    
    try:
        subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], 
                      check=True)
        print("βœ… All packages installed successfully!")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ Error installing packages: {e}")
        return False

def check_model_file():
    """Check if the model file exists."""
    print("πŸ€– Checking model file...")
    if os.path.exists("resnet50_dr_classifier.pth"):
        file_size = os.path.getsize("resnet50_dr_classifier.pth") / (1024 * 1024)
        print(f"βœ… Model file found: resnet50_dr_classifier.pth ({file_size:.2f} MB)")
        return True
    else:
        print("❌ Model file 'resnet50_dr_classifier.pth' not found!")
        print("   Please ensure the model file is in the current directory.")
        return False

def create_directories():
    """Create necessary directories."""
    print("πŸ“ Creating directories...")
    
    directories = ["saved_predictions", "batch_results"]
    
    for directory in directories:
        if not os.path.exists(directory):
            os.makedirs(directory)
            print(f"   βœ… Created: {directory}/")
        else:
            print(f"   πŸ“ Exists: {directory}/")

def test_imports():
    """Test if all required packages can be imported."""
    print("πŸ§ͺ Testing imports...")
    
    required_packages = [
        ("torch", "PyTorch"),
        ("torchvision", "TorchVision"),
        ("PIL", "Pillow"),
        ("numpy", "NumPy"),
        ("gradio", "Gradio"),
        ("pytorch_grad_cam", "PyTorch Grad-CAM")
    ]
    
    all_good = True
    
    for package, name in required_packages:
        try:
            if package == "PIL":
                import PIL
                print(f"   βœ… {name} imported successfully")
            else:
                __import__(package)
                print(f"   βœ… {name} imported successfully")
        except ImportError:
            print(f"   ❌ {name} import failed")
            all_good = False
    
    return all_good

def run_test():
    """Run the test script to verify everything works."""
    print("πŸ§ͺ Running model test...")
    try:
        result = subprocess.run([sys.executable, "test_model.py"], 
                              capture_output=True, text=True)
        if result.returncode == 0:
            print("βœ… Model test passed!")
            return True
        else:
            print("❌ Model test failed!")
            print("Output:", result.stdout)
            print("Errors:", result.stderr)
            return False
    except Exception as e:
        print(f"❌ Could not run test: {e}")
        return False

def main():
    """Main setup function."""
    print("πŸš€ Diabetic Retinopathy Detection - Setup")
    print("=" * 50)
    
    # Check system info
    print(f"πŸ’» System: {platform.system()} {platform.release()}")
    print(f"🐍 Python: {sys.executable}")
    print()
    
    # Step 1: Check Python version
    if not check_python_version():
        return False
    
    # Step 2: Check pip
    if not check_pip():
        return False
    
    # Step 3: Upgrade pip
    upgrade_pip()
    
    # Step 4: Install requirements
    if not install_requirements():
        return False
    
    # Step 5: Check model file
    if not check_model_file():
        return False
    
    # Step 6: Create directories
    create_directories()
    
    # Step 7: Test imports
    if not test_imports():
        print("❌ Some packages failed to import. Please check the installation.")
        return False
    
    # Step 8: Run test
    if not run_test():
        print("❌ Model test failed. Please check the model file and installation.")
        return False
    
    print("\nπŸŽ‰ Setup completed successfully!")
    print("\nπŸ“‹ Next steps:")
    print("   1. Run the web app: python app.py")
    print("   2. Open your browser to the URL shown in the terminal")
    print("   3. Upload OCT images for analysis")
    print("\nπŸ”„ For batch processing:")
    print("   python batch_process.py")
    
    return True

if __name__ == "__main__":
    success = main()
    if not success:
        print("\n❌ Setup failed. Please check the error messages above.")
        sys.exit(1)