File size: 10,851 Bytes
c7df5be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash

# FitTürkAI RAG System - VM Startup Script
# CPU Optimized with High RAM Configuration

set -e  # Exit on any error

LOG_FILE="/var/log/fitturkrai-setup.log"
exec > >(tee -a $LOG_FILE)
exec 2>&1

echo "=================================================="
echo "🚀 FitTürkAI RAG System Setup Starting..."
echo "Timestamp: $(date)"
echo "=================================================="

# Update system
echo "📦 Updating system packages..."
apt-get update && apt-get upgrade -y

# Install essential packages
echo "🔧 Installing essential packages..."
apt-get install -y \
    python3 \
    python3-pip \
    python3-venv \
    python3-dev \
    git \
    curl \
    wget \
    htop \
    tree \
    unzip \
    vim \
    build-essential \
    software-properties-common \
    apt-transport-https \
    ca-certificates \
    gnupg \
    lsb-release

# Install monitoring tools
echo "📊 Installing monitoring tools..."
apt-get install -y \
    iotop \
    nethogs \
    nload \
    glances \
    ncdu

# Upgrade pip
echo "🐍 Setting up Python environment..."
python3 -m pip install --upgrade pip

# Install CPU-optimized PyTorch
echo "🔥 Installing PyTorch (CPU version)..."
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

# Install core ML packages for CPU
echo "🤖 Installing ML packages..."
pip3 install \
    transformers>=4.36.0,\<4.40.0 \
    sentence-transformers>=2.2.2,\<3.0.0 \
    accelerate>=0.24.0,\<0.26.0 \
    peft>=0.7.0,\<0.8.0 \
    nltk>=3.8 \
    faiss-cpu>=1.7.4 \
    numpy>=1.24.0,\<2.0.0 \
    scipy>=1.9.0 \
    PyPDF2>=3.0.0 \
    PyMuPDF>=1.23.0 \
    tqdm>=4.64.0 \
    requests>=2.28.0 \
    datasets>=2.14.0

# System optimizations for high RAM
echo "⚡ Configuring system for high RAM usage..."

# Swap settings (minimize swap usage)
echo 'vm.swappiness=1' >> /etc/sysctl.conf
echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.conf
echo 'vm.dirty_ratio=15' >> /etc/sysctl.conf
echo 'vm.dirty_background_ratio=5' >> /etc/sysctl.conf

# Memory overcommit (allow more memory allocation)
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf
echo 'vm.overcommit_ratio=80' >> /etc/sysctl.conf

# Network optimizations
echo 'net.core.rmem_max=16777216' >> /etc/sysctl.conf
echo 'net.core.wmem_max=16777216' >> /etc/sysctl.conf

# Apply system settings
sysctl -p

# Configure user limits for high memory usage
echo "🔒 Configuring user limits..."
cat >> /etc/security/limits.conf << EOF
ubuntu soft memlock unlimited
ubuntu hard memlock unlimited
ubuntu soft stack unlimited
ubuntu hard stack unlimited
EOF

# Create FitTürkAI directory structure
echo "📁 Creating directory structure..."
sudo -u ubuntu mkdir -p /home/ubuntu/FitTurkAI-RAG
sudo -u ubuntu mkdir -p /home/ubuntu/FitTurkAI-RAG/indirilen_pdfler
sudo -u ubuntu mkdir -p /home/ubuntu/FitTurkAI-RAG/DATA
sudo -u ubuntu mkdir -p /home/ubuntu/FitTurkAI-RAG/fitness_rag_store_merged
sudo -u ubuntu mkdir -p /home/ubuntu/FitTurkAI-RAG/fine_tuned_FitTurkAI_QLoRA

# Set ownership
chown -R ubuntu:ubuntu /home/ubuntu/FitTurkAI-RAG

# Create demo data
echo "📝 Creating demo data..."
sudo -u ubuntu cat > /home/ubuntu/FitTurkAI-RAG/DATA/demo_fitness_data.json << 'EOF'
[
  {
    "soru": "Sağlıklı kahvaltı için ne önerirsiniz?",
    "cevap": "Sağlıklı bir kahvaltı protein, kompleks karbonhidrat ve sağlıklı yağlar içermelidir. Yumurta, tam tahıllı ekmek, avokado, meyveler ve kuruyemişler iyi seçeneklerdir. Ayrıca bol su içmeyi unutmayın."
  },
  {
    "soru": "Günde kaç bardak su içmeliyim?",
    "cevap": "Genel olarak günde 8-10 bardak (2-2.5 litre) su içmek önerilir. Aktivite düzeyinize, hava durumuna ve vücut ağırlığınıza göre bu miktar artabilir. Egzersiz yaptığınız günlerde daha fazla su tüketmelisiniz."
  },
  {
    "soru": "Egzersiz sonrası ne yemeli?",
    "cevap": "Egzersiz sonrası 30-60 dakika içinde protein ve karbonhidrat içeren besinler tüketin. Protein kas onarımı için, karbonhidrat ise enerji depolarını yenilemek için gereklidir. Örneğin protein smoothie, yoğurt ile meyve veya tavuk göğsü ile pirinç iyi seçeneklerdir."
  },
  {
    "soru": "Kilo vermek için hangi egzersizleri yapmalıyım?",
    "cevap": "Kilo vermek için kardiyovasküler egzersizler (yürüyüş, koşu, bisiklet) ve direnç antrenmanlarını (ağırlık kaldırma) birleştirin. Haftada en az 150 dakika orta yoğunlukta kardiyovasküler aktivite yapın. Ayrıca kas kütlesini korumak için haftada 2-3 gün kuvvet antrenmanı ekleyin."
  }
]
EOF

# Create system info script
echo "ℹ️ Creating system info script..."
sudo -u ubuntu cat > /home/ubuntu/system_info.sh << 'EOF'
#!/bin/bash
echo "=== FitTürkAI VM System Information ==="
echo "Date: $(date)"
echo "Uptime: $(uptime)"
echo ""
echo "=== CPU Information ==="
lscpu | grep -E 'Model name|CPU\(s\)|Thread'
echo ""
echo "=== Memory Information ==="
free -h
echo ""
echo "=== Disk Usage ==="
df -h
echo ""
echo "=== GPU Information ==="
if command -v nvidia-smi &> /dev/null; then
    nvidia-smi
else
    echo "No GPU detected (CPU-only configuration)"
fi
echo ""
echo "=== Python Packages ==="
pip3 list | grep -E 'torch|transformers|sentence|faiss|peft'
echo ""
echo "=== System Load ==="
top -n 1 -b | head -20
EOF

chmod +x /home/ubuntu/system_info.sh
chown ubuntu:ubuntu /home/ubuntu/system_info.sh

# Create RAM monitoring script
echo "📊 Creating RAM monitoring script..."
sudo -u ubuntu cat > /home/ubuntu/monitor_ram.sh << 'EOF'
#!/bin/bash
echo "=== Real-time RAM Monitoring ==="
while true; do
    clear
    echo "FitTürkAI RAM Monitor - $(date)"
    echo "================================"
    free -h
    echo ""
    echo "Top 10 Memory Consuming Processes:"
    ps aux --sort=-%mem | head -11
    echo ""
    echo "Press Ctrl+C to exit"
    sleep 5
done
EOF

chmod +x /home/ubuntu/monitor_ram.sh
chown ubuntu:ubuntu /home/ubuntu/monitor_ram.sh

# Install performance monitoring alias
echo "📈 Setting up monitoring aliases..."
sudo -u ubuntu cat >> /home/ubuntu/.bashrc << 'EOF'

# FitTürkAI Monitoring Aliases
alias sysinfo='/home/ubuntu/system_info.sh'
alias rammon='/home/ubuntu/monitor_ram.sh'
alias fitturkrai='cd /home/ubuntu/FitTurkAI-RAG'
alias pyfree='python3 -c "import psutil; print(f\"Available RAM: {psutil.virtual_memory().available/1024**3:.1f} GB\")"'

# Python environment
export PYTHONPATH=/home/ubuntu/FitTurkAI-RAG:$PYTHONPATH

echo "🏋️ Welcome to FitTürkAI High-RAM VM!"
echo "Available commands:"
echo "  sysinfo  - System information"
echo "  rammon   - RAM monitoring"
echo "  fitturkrai - Go to project directory"
echo "  pyfree   - Check available Python memory"
EOF

# Download NLTK data
echo "📚 Downloading NLTK data..."
sudo -u ubuntu python3 -c "
import nltk
import ssl
try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

try:
    nltk.download('punkt', quiet=True)
    nltk.download('punkt_tab', quiet=True)
    nltk.download('stopwords', quiet=True)
    print('NLTK data downloaded successfully!')
except Exception as e:
    print(f'NLTK download warning: {e}')
"

# Create performance test script
echo "🧪 Creating performance test script..."
sudo -u ubuntu cat > /home/ubuntu/performance_test.py << 'EOF'
#!/usr/bin/env python3
"""
FitTürkAI Performance Test Script
Tests system capabilities for CPU-based RAG operations
"""

import time
import psutil
import numpy as np
from sentence_transformers import SentenceTransformer

def test_memory():
    """Test available memory"""
    memory = psutil.virtual_memory()
    print(f"💾 Total RAM: {memory.total / 1024**3:.1f} GB")
    print(f"💾 Available RAM: {memory.available / 1024**3:.1f} GB")
    print(f"💾 Used RAM: {memory.percent}%")
    return memory.available > 2 * 1024**3  # At least 2GB free

def test_cpu():
    """Test CPU performance"""
    print(f"🔧 CPU Cores: {psutil.cpu_count()}")
    print(f"🔧 CPU Usage: {psutil.cpu_percent(interval=1)}%")
    return True

def test_sentence_transformer():
    """Test sentence transformer loading"""
    try:
        print("🤖 Testing Sentence Transformer...")
        model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
        
        # Test encoding
        texts = ["Bu bir test cümlesidir.", "FitTürkAI sistemi çalışıyor."]
        start_time = time.time()
        embeddings = model.encode(texts)
        encoding_time = time.time() - start_time
        
        print(f"✅ Sentence Transformer loaded successfully!")
        print(f"⏱️ Encoding time: {encoding_time:.2f}s")
        print(f"📏 Embedding shape: {embeddings.shape}")
        return True
    except Exception as e:
        print(f"❌ Sentence Transformer test failed: {e}")
        return False

def main():
    print("🚀 FitTürkAI Performance Test")
    print("=" * 40)
    
    # Test system components
    memory_ok = test_memory()
    cpu_ok = test_cpu()
    transformer_ok = test_sentence_transformer()
    
    print("\n" + "=" * 40)
    print("📊 Test Results:")
    print(f"💾 Memory: {'✅ OK' if memory_ok else '❌ INSUFFICIENT'}")
    print(f"🔧 CPU: {'✅ OK' if cpu_ok else '❌ PROBLEM'}")
    print(f"🤖 Transformers: {'✅ OK' if transformer_ok else '❌ FAILED'}")
    
    if all([memory_ok, cpu_ok, transformer_ok]):
        print("\n🎉 System ready for FitTürkAI!")
    else:
        print("\n⚠️ System needs optimization!")

if __name__ == "__main__":
    main()
EOF

chmod +x /home/ubuntu/performance_test.py
chown ubuntu:ubuntu /home/ubuntu/performance_test.py

# Configure automatic updates
echo "🔄 Configuring automatic security updates..."
echo 'Unattended-Upgrade::Automatic-Reboot "false";' >> /etc/apt/apt.conf.d/50unattended-upgrades

# Final system status
echo ""
echo "=================================================="
echo "✅ FitTürkAI VM Setup Completed!"
echo "=================================================="
echo "📊 System Status:"
echo "  - OS: $(lsb_release -d | cut -f2)"
echo "  - Memory: $(free -h | awk 'NR==2{printf \"%.1f GB total, %.1f GB available\", $2/1024/1024, $7/1024/1024}')"
echo "  - CPU: $(nproc) cores"
echo "  - Python: $(python3 --version)"
echo ""
echo "🎯 Next Steps:"
echo "1. SSH to the VM: gcloud compute ssh [VM_NAME] --zone=[ZONE]"
echo "2. Run performance test: python3 /home/ubuntu/performance_test.py"
echo "3. Clone your FitTürkAI repository"
echo "4. Start the RAG system: python3 test.py"
echo ""
echo "📚 Useful Commands:"
echo "  - sysinfo: System information"
echo "  - rammon: RAM monitoring"
echo "  - fitturkrai: Go to project directory"
echo ""
echo "Setup completed at: $(date)"
echo "=================================================="