File size: 6,878 Bytes
ab4e093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash

# AI Knowledge Distillation Platform - Quick Start Script
# منصة تقطير المعرفة للذكاء الاصطناعي - سكريبت البدء السريع

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Unicode symbols
CHECK="✅"
CROSS="❌"
WARNING="⚠️"
INFO="ℹ️"
ROCKET="🚀"
GEAR="🔧"
MEMORY="💾"
CPU="🖥️"

echo -e "${PURPLE}================================================${NC}"
echo -e "${PURPLE}    AI Knowledge Distillation Platform${NC}"
echo -e "${PURPLE}    منصة تقطير المعرفة للذكاء الاصطناعي${NC}"
echo -e "${PURPLE}================================================${NC}"
echo ""

# Function to print colored output
print_status() {
    echo -e "${GREEN}${CHECK}${NC} $1"
}

print_error() {
    echo -e "${RED}${CROSS}${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}${WARNING}${NC} $1"
}

print_info() {
    echo -e "${BLUE}${INFO}${NC} $1"
}

# Check if Python is installed
check_python() {
    if command -v python3 &> /dev/null; then
        PYTHON_VERSION=$(python3 --version | cut -d' ' -f2)
        print_status "Python $PYTHON_VERSION found"
        return 0
    else
        print_error "Python 3 not found. Please install Python 3.9 or higher."
        return 1
    fi
}

# Check system requirements
check_system() {
    print_info "Checking system requirements..."
    
    # Check memory
    if command -v free &> /dev/null; then
        TOTAL_MEM=$(free -g | awk '/^Mem:/{print $2}')
        if [ "$TOTAL_MEM" -ge 4 ]; then
            print_status "Memory: ${TOTAL_MEM}GB (sufficient)"
        else
            print_warning "Memory: ${TOTAL_MEM}GB (minimum 4GB recommended)"
        fi
    fi
    
    # Check CPU cores
    if command -v nproc &> /dev/null; then
        CPU_CORES=$(nproc)
        print_status "CPU cores: $CPU_CORES"
    fi
    
    # Check disk space
    DISK_SPACE=$(df -h . | awk 'NR==2{print $4}')
    print_status "Available disk space: $DISK_SPACE"
}

# Create necessary directories
create_directories() {
    print_info "Creating necessary directories..."
    
    directories=(
        "cache"
        "cache/datasets"
        "cache/transformers"
        "cache/medical_datasets"
        "database"
        "logs"
        "models"
        "backups"
        "uploads"
        "temp"
    )
    
    for dir in "${directories[@]}"; do
        if [ ! -d "$dir" ]; then
            mkdir -p "$dir"
            print_status "Created directory: $dir"
        fi
    done
}

# Install dependencies
install_dependencies() {
    print_info "Checking dependencies..."
    
    if [ ! -f "requirements.txt" ]; then
        print_error "requirements.txt not found!"
        return 1
    fi
    
    # Check if virtual environment exists
    if [ ! -d "venv" ]; then
        print_info "Creating virtual environment..."
        python3 -m venv venv
        print_status "Virtual environment created"
    fi
    
    # Activate virtual environment
    source venv/bin/activate
    
    # Upgrade pip
    print_info "Upgrading pip..."
    pip install --upgrade pip
    
    # Install dependencies
    print_info "Installing dependencies..."
    pip install -r requirements.txt
    
    print_status "Dependencies installed"
}

# Set environment variables
set_environment() {
    print_info "Setting environment variables..."
    
    # CPU optimization
    export OMP_NUM_THREADS=$(nproc)
    export MKL_NUM_THREADS=$(nproc)
    export NUMEXPR_NUM_THREADS=$(nproc)
    export OPENBLAS_NUM_THREADS=$(nproc)
    
    # Memory optimization
    export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128
    export TOKENIZERS_PARALLELISM=false
    
    # Disable GPU (force CPU-only)
    export CUDA_VISIBLE_DEVICES=""
    
    # Cache directories
    export HF_DATASETS_CACHE=./cache/datasets
    export TRANSFORMERS_CACHE=./cache/transformers
    export HF_HOME=./cache/huggingface
    
    print_status "Environment variables set"
}

# Start the application
start_application() {
    print_info "Starting application..."
    
    # Check which runner to use
    if [ -f "run_optimized.py" ]; then
        print_status "Using optimized runner"
        python run_optimized.py
    elif [ -f "app.py" ]; then
        print_status "Using standard runner"
        python app.py
    else
        print_error "No application file found!"
        return 1
    fi
}

# Main execution
main() {
    echo -e "${CYAN}${ROCKET} Starting setup process...${NC}"
    echo ""
    
    # Check Python
    if ! check_python; then
        exit 1
    fi
    
    # Check system
    check_system
    echo ""
    
    # Create directories
    create_directories
    echo ""
    
    # Install dependencies
    if [ "$1" != "--skip-install" ]; then
        install_dependencies
        echo ""
    else
        print_info "Skipping dependency installation"
        # Still activate venv if it exists
        if [ -d "venv" ]; then
            source venv/bin/activate
        fi
    fi
    
    # Set environment
    set_environment
    echo ""

    # Setup tokens
    if [ -f "setup_tokens.py" ]; then
        print_info "Setting up Hugging Face tokens..."
        python setup_tokens.py
        echo ""
    fi

    # Final status
    echo -e "${GREEN}${CHECK} Setup completed successfully!${NC}"
    echo ""
    echo -e "${CYAN}${GEAR} System Information:${NC}"
    echo -e "  ${MEMORY} Memory optimization: Enabled"
    echo -e "  ${CPU} CPU threads: $OMP_NUM_THREADS"
    echo -e "  🔒 Security: Token encryption enabled"
    echo -e "  🏥 Medical AI: Supported"
    echo ""
    echo -e "${YELLOW}${ROCKET} Starting AI Knowledge Distillation Platform...${NC}"
    echo -e "${BLUE}🌐 Access the application at: http://localhost:8000${NC}"
    echo -e "${BLUE}🔑 Token management: http://localhost:8000/tokens${NC}"
    echo -e "${BLUE}🏥 Medical datasets: http://localhost:8000/medical-datasets${NC}"
    echo ""
    echo -e "${PURPLE}================================================${NC}"
    
    # Start application
    start_application
}

# Handle script arguments
case "$1" in
    --help|-h)
        echo "Usage: $0 [OPTIONS]"
        echo ""
        echo "Options:"
        echo "  --help, -h          Show this help message"
        echo "  --skip-install      Skip dependency installation"
        echo "  --check-only        Only check system requirements"
        echo ""
        echo "Examples:"
        echo "  $0                  Full setup and start"
        echo "  $0 --skip-install   Start without installing dependencies"
        echo "  $0 --check-only     Check system requirements only"
        exit 0
        ;;
    --check-only)
        check_python
        check_system
        exit 0
        ;;
    *)
        main "$@"
        ;;
esac