AEUPH commited on
Commit
eb79158
·
verified ·
1 Parent(s): bbe473e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
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": os.environ.get('pass')
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
- print(f"Command {command} executed successfully.")
26
- return response.json() # Assuming Corrade returns JSON
27
  else:
28
- print(f"Failed to execute command {command}: HTTP {response.status_code}")
29
- return None
30
  except Exception as e:
31
- print(f"Error sending command to Corrade: {e}")
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
- def execute_actions(self, corrade_endpoint, action_sequence):
42
- actions = action_sequence.split(';')
43
- for action in actions:
44
- if action.strip() == "move_forward":
45
- self.send_command_to_corrade(corrade_endpoint, "move", {"direction": "forward"})
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
- def run(self, corrade_endpoint):
51
- try:
52
- current_states = ["I am at the starting point.", "I see an object nearby.", "I am near the DJ equipment."]
53
- for state in current_states:
54
- print(f"Current state: {state}")
55
- action_sequence = self.generate_action_sequence(state)
56
- print(f"Generated action sequence: {action_sequence}")
57
- self.execute_actions(corrade_endpoint, action_sequence)
58
- except Exception as e:
59
- print(f"An error occurred: {e}")
 
 
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
- corrade_endpoint = os.environ.get('url') # Define the Corrade endpoint here
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()