Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,6 @@ import uuid
|
|
| 8 |
from requests.exceptions import RequestException, HTTPError, Timeout, ConnectionError
|
| 9 |
from datasets import load_dataset
|
| 10 |
|
| 11 |
-
# Assuming necessary imports are done above
|
| 12 |
-
|
| 13 |
# Global initialization
|
| 14 |
print("Initializing GPT-2 Tokenizer and Model...")
|
| 15 |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
|
@@ -20,33 +18,25 @@ print("Loading dataset...")
|
|
| 20 |
dataset = load_dataset("visionlab/block-towers-10k-3s-trajectory-scale1", split='train')
|
| 21 |
print("Dataset loaded successfully.")
|
| 22 |
|
| 23 |
-
# Preprocess dataset to build a lookup table for fast access
|
| 24 |
-
lookup_table = {}
|
| 25 |
-
grid_size = 1.0 # Adjust based on your dataset's scale and distribution
|
| 26 |
-
print("Building lookup table...")
|
| 27 |
-
for item in dataset:
|
| 28 |
-
# Adjusting to access 'final_positions' as provided in the dataset structure
|
| 29 |
-
for position in item['data']['final_positions']:
|
| 30 |
-
xyz = [position['x'], position['y'], position['z']]
|
| 31 |
-
grid_key = (round(xyz[0]/grid_size), round(xyz[1]/grid_size), round(xyz[2]/grid_size))
|
| 32 |
-
if grid_key not in lookup_table:
|
| 33 |
-
lookup_table[grid_key] = xyz
|
| 34 |
-
else:
|
| 35 |
-
# For simplicity, this keeps the first encountered position for each grid key.
|
| 36 |
-
# You might want to handle multiple positions within the same grid cell differently.
|
| 37 |
-
continue
|
| 38 |
-
print("Lookup table built.")
|
| 39 |
-
|
| 40 |
class SecondLifeNavigator:
|
| 41 |
def __init__(self):
|
| 42 |
-
self.
|
| 43 |
-
print("SecondLifeNavigator initialized with the
|
| 44 |
|
| 45 |
def determine_action_sequence(self, current_xyz):
|
| 46 |
print(f"Determining action sequence for position: {current_xyz}")
|
| 47 |
-
|
| 48 |
-
nearest_position =
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if nearest_position:
|
| 51 |
action_sequence = f"Move to position {nearest_position[0]}, {nearest_position[1]}, {nearest_position[2]}"
|
| 52 |
print(f"Action sequence determined: {action_sequence}")
|
|
@@ -78,13 +68,12 @@ class SecondLifeNavigator:
|
|
| 78 |
except RequestException as req_err:
|
| 79 |
print(f"Error sending command to Corrade: {req_err}")
|
| 80 |
return None
|
| 81 |
-
|
| 82 |
def create_interface():
|
| 83 |
def navigate(current_state):
|
| 84 |
endpoint = os.getenv('CORRADE_ENDPOINT', '') # Ensure this environment variable is correctly set.
|
| 85 |
sl_navigator = SecondLifeNavigator()
|
| 86 |
-
|
| 87 |
-
action_sequence = sl_navigator.determine_action_sequence(current_state)
|
| 88 |
random_id = str(uuid.uuid4())
|
| 89 |
return random_id + ": " + action_sequence
|
| 90 |
|
|
|
|
| 8 |
from requests.exceptions import RequestException, HTTPError, Timeout, ConnectionError
|
| 9 |
from datasets import load_dataset
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Global initialization
|
| 12 |
print("Initializing GPT-2 Tokenizer and Model...")
|
| 13 |
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
|
|
|
| 18 |
dataset = load_dataset("visionlab/block-towers-10k-3s-trajectory-scale1", split='train')
|
| 19 |
print("Dataset loaded successfully.")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
class SecondLifeNavigator:
|
| 22 |
def __init__(self):
|
| 23 |
+
self.dataset = dataset
|
| 24 |
+
print("SecondLifeNavigator initialized with the dataset.")
|
| 25 |
|
| 26 |
def determine_action_sequence(self, current_xyz):
|
| 27 |
print(f"Determining action sequence for position: {current_xyz}")
|
| 28 |
+
min_distance = float('inf')
|
| 29 |
+
nearest_position = None
|
| 30 |
+
|
| 31 |
+
# Iterate directly through dataset items to find the nearest position
|
| 32 |
+
for item in self.dataset:
|
| 33 |
+
for position in item['data']['final_positions']:
|
| 34 |
+
xyz = [position['x'], position['y'], position['z']]
|
| 35 |
+
distance = np.sqrt((xyz[0] - current_xyz[0])**2 + (xyz[1] - current_xyz[1])**2 + (xyz[2] - current_xyz[2])**2)
|
| 36 |
+
if distance < min_distance:
|
| 37 |
+
min_distance = distance
|
| 38 |
+
nearest_position = xyz
|
| 39 |
+
|
| 40 |
if nearest_position:
|
| 41 |
action_sequence = f"Move to position {nearest_position[0]}, {nearest_position[1]}, {nearest_position[2]}"
|
| 42 |
print(f"Action sequence determined: {action_sequence}")
|
|
|
|
| 68 |
except RequestException as req_err:
|
| 69 |
print(f"Error sending command to Corrade: {req_err}")
|
| 70 |
return None
|
| 71 |
+
|
| 72 |
def create_interface():
|
| 73 |
def navigate(current_state):
|
| 74 |
endpoint = os.getenv('CORRADE_ENDPOINT', '') # Ensure this environment variable is correctly set.
|
| 75 |
sl_navigator = SecondLifeNavigator()
|
| 76 |
+
action_sequence = sl_navigator.determine_action_sequence(json.loads(current_state))
|
|
|
|
| 77 |
random_id = str(uuid.uuid4())
|
| 78 |
return random_id + ": " + action_sequence
|
| 79 |
|