File size: 2,321 Bytes
979e977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Remote Persistent Inference
===========================
Questo script chiede al server di caricare il modello salvato in precedenza
(nella cartella persistente) e di eseguire un'inferenza.
Nessun file viene scaricato: solo i risultati testuali tornano indietro.
"""

from antigravity_sdk import RemoteGPU

INFERENCE_CODE = r'''
import torch
import torch.nn as nn
import numpy as np
import os
import sys

# 1. Re-define the Model (Same architecture)
class SimpleNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(2, 16),
            nn.ReLU(),
            nn.Linear(16, 16),
            nn.ReLU(),
            nn.Linear(16, 1),
            nn.Sigmoid()
        )
    def forward(self, x):
        return self.net(x)

# 2. Load Persisted Model
STORAGE_DIR = "/home/user/app/storage"
model_path = os.path.join(STORAGE_DIR, "persistent_model.pth")

print(f"๐Ÿ“‚ Loading model from SERVER storage: {model_path}...")

if not os.path.exists(model_path):
    print("โŒ Model not found! Have you run 'train_nn.py' in persistent mode?")
    sys.exit(1)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = SimpleNN().to(device)

try:
    model.load_state_dict(torch.load(model_path))
    model.eval()
    print("โœ… Model loaded successfully on GPU!")
except Exception as e:
    print(f"โŒ Error loading model: {e}")
    sys.exit(1)

# 3. Remote Inference
print("\n๐Ÿ”ฎ Running Inference remotely...")
test_points = np.array([
    [0.0, 0.0],
    [0.8, 0.8],
    [-0.5, 0.5],
    [2.0, 2.0]
], dtype=np.float32)

input_tensor = torch.tensor(test_points).to(device)

with torch.no_grad():
    predictions = model(input_tensor).cpu().numpy()

print("-" * 40)
print(f"{'X':<10} {'Y':<10} | {'Prob':<10} {'Class':<10}")
print("-" * 40)

for i, point in enumerate(test_points):
    prob = predictions[i][0]
    cls = 1 if prob > 0.5 else 0
    print(f"{point[0]:<10.2f} {point[1]:<10.2f} | {prob:<10.4f} {cls:<10}")
    
print("-" * 40)
print("โœ… Inference executed on SERVER.")
'''

def main():
    print("๐Ÿ“ก Connecting to Remote GPU for Inference...")
    gpu = RemoteGPU()
    
    # Eseguiamo e basta, non ci aspettiamo file di ritorno
    gpu.run(INFERENCE_CODE)

if __name__ == "__main__":
    main()