mgbam commited on
Commit
871a880
Β·
verified Β·
1 Parent(s): 2e93b96

Update deployer/simulator_interface.py

Browse files
Files changed (1) hide show
  1. deployer/simulator_interface.py +86 -34
deployer/simulator_interface.py CHANGED
@@ -1,43 +1,95 @@
1
- # simulator_interface.py - Robot app simulator for Hugging Face Space deployment
2
-
3
- # This version is placeholder logic using logging only. Replace with PyBullet/Isaac Sim for realism.
4
-
5
  import time
6
  import logging
 
7
 
8
- logging.basicConfig(level=logging.INFO)
 
 
 
 
 
9
 
10
  class VirtualRobot:
 
 
 
 
 
11
  def __init__(self):
12
  self.state = "IDLE"
13
- logging.info("[πŸ€–] Virtual Robot initialized.")
 
14
 
15
- def wave(self):
16
- self.state = "WAVING"
17
- logging.info("[πŸ–οΈ] Robot is waving arm.")
18
- time.sleep(1)
19
- self.state = "IDLE"
20
- return "πŸ€– *waves hello!*"
 
21
 
22
- def speak(self, phrase):
23
- self.state = "SPEAKING"
24
- logging.info(f"[πŸ’¬] Robot says: '{phrase}'")
25
- time.sleep(1)
26
- self.state = "IDLE"
27
- return f"πŸ—£οΈ {phrase}"
28
-
29
- def perform_action(self, action: str):
30
- if action.lower() == "wave":
31
- return self.wave()
32
- elif action.lower().startswith("say"):
33
- phrase = action[4:]
34
- return self.speak(phrase)
35
- else:
36
- logging.warning("[⚠️] Unknown command.")
37
- return "❓ Unknown action"
38
-
39
- # Example
40
- if __name__ == "__main__":
41
- bot = VirtualRobot()
42
- print(bot.perform_action("wave"))
43
- print(bot.perform_action("say Welcome to RoboSage!"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # simulator_interface.py - Enhanced Virtual Robot Implementation
 
 
 
2
  import time
3
  import logging
4
+ from typing import Optional
5
 
6
+ # Configure logging
7
+ logging.basicConfig(
8
+ level=logging.INFO,
9
+ format='%(asctime)s - %(levelname)s - %(message)s',
10
+ handlers=[logging.StreamHandler()]
11
+ )
12
 
13
  class VirtualRobot:
14
+ """Enhanced virtual robot with state management and error handling."""
15
+
16
+ VALID_STATES = {"IDLE", "WAVING", "SPEAKING", "ERROR"}
17
+ VALID_ACTIONS = {"wave", "say"}
18
+
19
  def __init__(self):
20
  self.state = "IDLE"
21
+ self.last_action_time = time.time()
22
+ logging.info("[πŸ€–] Virtual Robot initialized in state: %s", self.state)
23
 
24
+ def _validate_state(self) -> bool:
25
+ """Ensure robot is in a valid state."""
26
+ if self.state not in self.VALID_STATES:
27
+ self.state = "ERROR"
28
+ logging.error("Invalid robot state detected!")
29
+ return False
30
+ return True
31
 
32
+ def wave(self) -> str:
33
+ """Perform waving action with state management."""
34
+ if not self._validate_state():
35
+ return "❌ System error - invalid state"
36
+
37
+ try:
38
+ self.state = "WAVING"
39
+ self.last_action_time = time.time()
40
+ logging.info("[πŸ–οΈ] Performing wave action")
41
+ time.sleep(0.5) # Simulate action duration
42
+ response = "πŸ€– *waves hello!*"
43
+ return response
44
+ except Exception as e:
45
+ self.state = "ERROR"
46
+ logging.error("Wave action failed: %s", str(e))
47
+ return f"❌ Wave failed: {str(e)}"
48
+ finally:
49
+ self.state = "IDLE"
50
+
51
+ def speak(self, phrase: str) -> str:
52
+ """Perform speaking action with validation."""
53
+ if not phrase or not isinstance(phrase, str):
54
+ logging.warning("Invalid phrase received")
55
+ return "❌ Invalid phrase"
56
+
57
+ if not self._validate_state():
58
+ return "❌ System error - invalid state"
59
+
60
+ try:
61
+ self.state = "SPEAKING"
62
+ self.last_action_time = time.time()
63
+ logging.info("[πŸ’¬] Speaking: '%s'", phrase)
64
+ time.sleep(len(phrase) * 0.05) # Dynamic delay based on phrase length
65
+ return f"πŸ—£οΈ {phrase}"
66
+ except Exception as e:
67
+ self.state = "ERROR"
68
+ logging.error("Speak action failed: %s", str(e))
69
+ return f"❌ Speak failed: {str(e)}"
70
+ finally:
71
+ self.state = "IDLE"
72
+
73
+ def perform_action(self, action: str) -> str:
74
+ """Main action dispatcher with comprehensive validation."""
75
+ if not action or not isinstance(action, str):
76
+ logging.warning("Invalid action command received")
77
+ return "❌ Invalid action command"
78
+
79
+ action = action.strip().lower()
80
+
81
+ try:
82
+ if action == "wave":
83
+ return self.wave()
84
+ elif action.startswith("say"):
85
+ phrase = action[3:].strip()
86
+ if not phrase:
87
+ return "❌ Please provide something to say"
88
+ return self.speak(phrase)
89
+ else:
90
+ logging.warning("Unknown action: %s", action)
91
+ return "❓ Unknown action - try 'wave' or 'say [message]'"
92
+ except Exception as e:
93
+ self.state = "ERROR"
94
+ logging.error("Action processing failed: %s", str(e))
95
+ return f"❌ System error: {str(e)}"