File size: 5,691 Bytes
98938e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# Launch cheap GCP spot (preemptible) instance for testing
# GCP spot instances são ~70-90% mais baratos

set -e

echo "========================================="
echo "GCP Spot Instance - Test OPTION A"
echo "========================================="

# Configuration
INSTANCE_NAME="ensemble-test-$(date +%s)"
MACHINE_TYPES=(
    "e2-micro"       # 0.25-2 vCPU, 1GB RAM - ~$0.0025/hr spot (~$0.01/hr normal)
    "e2-small"       # 0.5-2 vCPU, 2GB RAM - ~$0.005/hr spot (~$0.02/hr normal)
    "e2-medium"      # 1-2 vCPU, 4GB RAM - ~$0.01/hr spot (~$0.04/hr normal)
    "n1-standard-1"  # 1 vCPU, 3.75GB RAM - ~$0.01/hr spot
)

ZONE="us-central1-a"  # Cheapest zone
IMAGE_FAMILY="ubuntu-2204-lts"
IMAGE_PROJECT="ubuntu-os-cloud"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

# Check if gcloud is installed
if ! command -v gcloud &> /dev/null; then
    echo -e "${RED}Error: gcloud CLI not installed${NC}"
    echo "Install: https://cloud.google.com/sdk/docs/install"
    exit 1
fi

# Check if authenticated
if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q .; then
    echo -e "${YELLOW}Not authenticated. Running gcloud auth login...${NC}"
    gcloud auth login
fi

# Get current project
PROJECT=$(gcloud config get-value project)
if [ -z "$PROJECT" ]; then
    echo -e "${RED}No project set. Please run: gcloud config set project YOUR_PROJECT${NC}"
    exit 1
fi

echo -e "\n${YELLOW}Project: $PROJECT${NC}"
echo -e "${YELLOW}Zone: $ZONE${NC}"
echo ""

# Show pricing for spot instances
echo "Spot (Preemptible) Pricing:"
echo "  e2-micro:      ~\$0.0025/hr (1GB RAM) ⭐ CHEAPEST"
echo "  e2-small:      ~\$0.005/hr  (2GB RAM)"
echo "  e2-medium:     ~\$0.01/hr   (4GB RAM)"
echo "  n1-standard-1: ~\$0.01/hr   (3.75GB RAM)"
echo ""

# Default to cheapest
MACHINE_TYPE="e2-medium"  # 4GB needed for models
ESTIMATED_COST="0.01"

echo -e "${GREEN}Selected: $MACHINE_TYPE (~\$$ESTIMATED_COST/hr)${NC}"
echo ""

read -p "Launch spot instance? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Cancelled."
    exit 0
fi

# Create startup script
cat > /tmp/startup-script.sh << 'STARTUP'
#!/bin/bash

# Update system
apt-get update
apt-get install -y python3-pip git

# Install dependencies
pip3 install --upgrade pip

# Clone repository
cd /home/ensemble_test
git clone https://huggingface.co/marcosremar2/ensemble-tts-annotation
cd ensemble-tts-annotation

# Install requirements (basic only for quick test)
pip3 install -q torch --index-url https://download.pytorch.org/whl/cpu
pip3 install -q transformers datasets librosa soundfile numpy pandas tqdm scikit-learn

# Create test user
useradd -m -s /bin/bash ensemble_test || true
chown -R ensemble_test:ensemble_test /home/ensemble_test

echo "✅ Setup complete" > /tmp/setup-complete
date >> /tmp/setup-complete
STARTUP

# Launch instance
echo ""
echo "Launching spot instance..."

gcloud compute instances create "$INSTANCE_NAME" \
    --zone="$ZONE" \
    --machine-type="$MACHINE_TYPE" \
    --preemptible \
    --maintenance-policy=TERMINATE \
    --image-family="$IMAGE_FAMILY" \
    --image-project="$IMAGE_PROJECT" \
    --boot-disk-size=20GB \
    --boot-disk-type=pd-standard \
    --metadata-from-file startup-script=/tmp/startup-script.sh \
    --scopes=cloud-platform \
    --tags=ensemble-test

echo -e "${GREEN}✓ Instance created: $INSTANCE_NAME${NC}"

# Wait for instance to be running
echo ""
echo "Waiting for instance to be ready..."
sleep 10

# Get external IP
EXTERNAL_IP=$(gcloud compute instances describe "$INSTANCE_NAME" \
    --zone="$ZONE" \
    --format="get(networkInterfaces[0].accessConfigs[0].natIP)")

echo ""
echo "========================================="
echo -e "${GREEN}✓ Instance launched successfully!${NC}"
echo "========================================="
echo ""
echo "Instance Name: $INSTANCE_NAME"
echo "Machine Type: $MACHINE_TYPE"
echo "Cost: ~\$$ESTIMATED_COST/hr (spot/preemptible)"
echo "External IP: $EXTERNAL_IP"
echo "Zone: $ZONE"
echo ""
echo "SSH Command:"
echo "  gcloud compute ssh $INSTANCE_NAME --zone=$ZONE"
echo ""
echo "Run test (wait ~2min for setup):"
echo "  gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command='cd /home/ensemble_test/ensemble-tts-annotation && python3 test_local.py'"
echo ""
echo "Check setup status:"
echo "  gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command='cat /tmp/setup-complete'"
echo ""
echo "Delete instance:"
echo "  gcloud compute instances delete $INSTANCE_NAME --zone=$ZONE --quiet"
echo ""
echo "========================================="

# Save info
cat > /tmp/gcp-spot-info.txt << EOF
Instance Name: $INSTANCE_NAME
Machine Type: $MACHINE_TYPE
Cost: ~\$$ESTIMATED_COST/hr
External IP: $EXTERNAL_IP
Zone: $ZONE
Project: $PROJECT

SSH: gcloud compute ssh $INSTANCE_NAME --zone=$ZONE
Test: gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --command='cd /home/ensemble_test/ensemble-tts-annotation && python3 test_local.py'
Delete: gcloud compute instances delete $INSTANCE_NAME --zone=$ZONE --quiet
EOF

echo "Instance info saved to: /tmp/gcp-spot-info.txt"
echo ""

# Optionally run test automatically
read -p "Run test automatically? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo ""
    echo "Waiting for setup to complete (60s)..."
    sleep 60

    echo ""
    echo "Running test..."
    gcloud compute ssh "$INSTANCE_NAME" --zone="$ZONE" --command="cd /home/ensemble_test/ensemble-tts-annotation && python3 test_local.py" || true
fi

echo ""
echo -e "${YELLOW}Remember to delete the instance when done to avoid charges!${NC}"
echo "  gcloud compute instances delete $INSTANCE_NAME --zone=$ZONE --quiet"
echo ""