Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
import uuid
|
| 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')
|
| 14 |
+
model = GPT2LMHeadModel.from_pretrained('gpt2')
|
| 15 |
+
|
| 16 |
+
from scipy.spatial import KDTree
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Load your dataset
|
| 20 |
+
print("Loading dataset...")
|
| 21 |
+
dataset = load_dataset("visionlab/block-towers-10k-3s-trajectory-scale1", split='train')
|
| 22 |
+
print("Dataset loaded successfully.")
|
| 23 |
+
|
| 24 |
+
from scipy.spatial import KDTree
|
| 25 |
+
|
| 26 |
+
print("Extracting positions and building KD-tree...")
|
| 27 |
+
|
| 28 |
+
# Assuming final_positions is structured as shown
|
| 29 |
+
final_positions = [position for item in dataset for position in item['data']['final_positions']]
|
| 30 |
+
positions_array = np.array([[p['x'], p['y'], p['z']] for p in final_positions])
|
| 31 |
+
|
| 32 |
+
# Build a KD-tree
|
| 33 |
+
tree = KDTree(positions_array)
|
| 34 |
+
|
| 35 |
+
print("KD-tree constructed successfully.")
|
| 36 |
+
|
| 37 |
+
# Example current_xyz coordinate
|
| 38 |
+
current_xyz = [1, 2, 3] # Replace with actual values as needed
|
| 39 |
+
|
| 40 |
+
print("Querying KD-tree for the nearest position...")
|
| 41 |
+
distance, index = tree.query(current_xyz)
|
| 42 |
+
nearest_position = final_positions[index]
|
| 43 |
+
|
| 44 |
+
print(f"Nearest position found: {nearest_position} at distance {distance}")
|
| 45 |
+
|
| 46 |
+
def safe_convert_single_to_double_quotes(s):
|
| 47 |
+
try:
|
| 48 |
+
temp_placeholder = "<TEMP_ESCAPED_SINGLE_QUOTE>"
|
| 49 |
+
s = s.replace("\\'", temp_placeholder)
|
| 50 |
+
s = s.replace("'", '"')
|
| 51 |
+
s = s.replace(temp_placeholder, "\\'")
|
| 52 |
+
return json.loads(s)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error converting or parsing JSON-like string: {e}")
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
class SecondLifeNavigator:
|
| 58 |
+
def __init__(self):
|
| 59 |
+
self.dataset = dataset
|
| 60 |
+
print("SecondLifeNavigator initialized with the dataset.")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def determine_action_sequence(current_xyz):
|
| 64 |
+
print(f"Determining action sequence for position: {current_xyz}")
|
| 65 |
+
|
| 66 |
+
# Query KD-tree for the nearest position
|
| 67 |
+
distance, index = tree.query(current_xyz)
|
| 68 |
+
nearest_position = final_positions[index]
|
| 69 |
+
|
| 70 |
+
# Construct the action sequence
|
| 71 |
+
action_sequence = f"Move to position {nearest_position['x']}, {nearest_position['y']}, {nearest_position['z']}"
|
| 72 |
+
print(f"Action sequence determined: {action_sequence}")
|
| 73 |
+
|
| 74 |
+
return action_sequence
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def send_command_to_corrade(self, corrade_endpoint, command, parameters):
|
| 78 |
+
print(f"Sending command to Corrade: {command}")
|
| 79 |
+
command_data = {
|
| 80 |
+
"command": command,
|
| 81 |
+
"group": "e269893f-a570-0087-930e-6ba2a0b77f9c",
|
| 82 |
+
"password": "nucleus",
|
| 83 |
+
}
|
| 84 |
+
command_data.update(parameters)
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
response = requests.post(corrade_endpoint, json=command_data, timeout=10)
|
| 88 |
+
response.raise_for_status()
|
| 89 |
+
print(f"Command {command} executed successfully.")
|
| 90 |
+
return response.json()
|
| 91 |
+
except Timeout:
|
| 92 |
+
print("Error: The request timed out.")
|
| 93 |
+
except HTTPError as http_err:
|
| 94 |
+
print(f"HTTP error occurred: {http_err}, Status Code: {response.status_code}")
|
| 95 |
+
except ConnectionError:
|
| 96 |
+
print("Error: A connection error occurred.")
|
| 97 |
+
except RequestException as req_err:
|
| 98 |
+
print(f"Error sending command to Corrade: {req_err}")
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
def create_interface():
|
| 102 |
+
|
| 103 |
+
def navigate(current_state):
|
| 104 |
+
# Correctly convert single quotes to double quotes and decode JSON
|
| 105 |
+
state_dict = safe_convert_single_to_double_quotes(current_state)
|
| 106 |
+
sl_navigator = SecondLifeNavigator()
|
| 107 |
+
# Pass the corrected and parsed JSON to determine_action_sequence
|
| 108 |
+
action_sequence = sl_navigator.determine_action_sequence(state_dict) # Use state_dict instead of current_state directly
|
| 109 |
+
random_id = str(uuid.uuid4())
|
| 110 |
+
return random_id + ": " + action_sequence
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
interface = gr.Interface(
|
| 115 |
+
fn=navigate,
|
| 116 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter current state..."),
|
| 117 |
+
outputs=gr.Textbox(label="Generated Action Sequence"),
|
| 118 |
+
title="Second Life Navigator",
|
| 119 |
+
description="Generates an action sequence based on the current state for navigating in Second Life.",
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
interface.launch()
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
create_interface()
|