Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
import requests
|
|
@@ -10,55 +12,50 @@ class SecondLifeNavigator:
|
|
| 10 |
self.corrade_api_key = corrade_api_key
|
| 11 |
|
| 12 |
def send_command_to_corrade(self, corrade_endpoint, command, parameters):
|
| 13 |
-
# Construct the command data
|
| 14 |
command_data = {
|
| 15 |
"command": command,
|
| 16 |
"group": "e269893f-a570-0087-930e-6ba2a0b77f9c",
|
| 17 |
-
"password":
|
| 18 |
}
|
| 19 |
command_data.update(parameters)
|
| 20 |
|
| 21 |
-
# Send the command to Corrade
|
| 22 |
try:
|
| 23 |
response = requests.post(corrade_endpoint, json=command_data)
|
| 24 |
if response.status_code == 200:
|
| 25 |
-
|
| 26 |
-
return response.json() # Assuming Corrade returns JSON
|
| 27 |
else:
|
| 28 |
-
|
| 29 |
-
return None
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 32 |
-
return None
|
| 33 |
|
| 34 |
def generate_action_sequence(self, current_state):
|
| 35 |
-
print("Inside Action Sequence...")
|
| 36 |
inputs = self.tokenizer.encode(current_state, return_tensors='pt')
|
| 37 |
outputs = self.model.generate(inputs, max_length=40, num_return_sequences=1)
|
| 38 |
action_sequence = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
return action_sequence
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
elif action.strip() == "interact_with_object":
|
| 47 |
-
self.send_command_to_corrade(corrade_endpoint, "interact", {"item": "9e7c8164-b633-c0f4-23f0-916a0144d836"})
|
| 48 |
-
# Implement other actions as needed
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
-
|
| 63 |
-
sl_navigator = SecondLifeNavigator()
|
| 64 |
-
sl_navigator.run(corrade_endpoint)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
import numpy as np
|
| 4 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 5 |
import requests
|
|
|
|
| 12 |
self.corrade_api_key = corrade_api_key
|
| 13 |
|
| 14 |
def send_command_to_corrade(self, corrade_endpoint, command, parameters):
|
|
|
|
| 15 |
command_data = {
|
| 16 |
"command": command,
|
| 17 |
"group": "e269893f-a570-0087-930e-6ba2a0b77f9c",
|
| 18 |
+
"password": self.corrade_api_key,
|
| 19 |
}
|
| 20 |
command_data.update(parameters)
|
| 21 |
|
|
|
|
| 22 |
try:
|
| 23 |
response = requests.post(corrade_endpoint, json=command_data)
|
| 24 |
if response.status_code == 200:
|
| 25 |
+
return f"Command {command} executed successfully.", response.json()
|
|
|
|
| 26 |
else:
|
| 27 |
+
return f"Failed to execute command {command}: HTTP {response.status_code}", None
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
+
return f"Error sending command to Corrade: {e}", None
|
|
|
|
| 30 |
|
| 31 |
def generate_action_sequence(self, current_state):
|
|
|
|
| 32 |
inputs = self.tokenizer.encode(current_state, return_tensors='pt')
|
| 33 |
outputs = self.model.generate(inputs, max_length=40, num_return_sequences=1)
|
| 34 |
action_sequence = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
return action_sequence
|
| 36 |
|
| 37 |
+
# Simplify run method for Gradio compatibility
|
| 38 |
+
def run(self, current_state, corrade_endpoint):
|
| 39 |
+
action_sequence = self.generate_action_sequence(current_state)
|
| 40 |
+
# Removed automatic command execution for safety and demonstration purposes
|
| 41 |
+
return action_sequence
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
def create_interface():
|
| 44 |
+
def navigate(current_state, corrade_endpoint):
|
| 45 |
+
sl_navigator = SecondLifeNavigator()
|
| 46 |
+
action_sequence = sl_navigator.run(current_state, corrade_endpoint)
|
| 47 |
+
return action_sequence
|
| 48 |
+
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=navigate,
|
| 51 |
+
inputs=[gr.inputs.Textbox(lines=2, placeholder="Enter current state..."),
|
| 52 |
+
gr.inputs.Textbox(label="Corrade Endpoint URL")],
|
| 53 |
+
outputs=[gr.outputs.Textbox(label="Generated Action Sequence")],
|
| 54 |
+
title="Second Life Navigator",
|
| 55 |
+
description="Generates an action sequence based on the current state for navigating in Second Life.",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
interface.launch()
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
create_interface()
|
|
|
|
|
|