Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +66 -24
deployer/gradio_generator.py
CHANGED
|
@@ -2,38 +2,80 @@ import gradio as gr
|
|
| 2 |
from .simulator_interface import VirtualRobot
|
| 3 |
import logging
|
| 4 |
|
| 5 |
-
class
|
| 6 |
def __init__(self, title="RoboSage", description="Your virtual assistant"):
|
| 7 |
self.robot = VirtualRobot()
|
| 8 |
self.title = title
|
| 9 |
self.description = description
|
| 10 |
self.logger = logging.getLogger(__name__)
|
| 11 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
with gr.Blocks(title=self.title) as interface:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
input_txt = gr.Textbox(label="Command", placeholder="Try 'wave' or 'say hello'")
|
| 19 |
-
btn = gr.Button("Submit", variant="primary")
|
| 20 |
-
|
| 21 |
-
output_txt = gr.Textbox(label="Response", interactive=False)
|
| 22 |
-
|
| 23 |
-
def process_command(cmd: str) -> str:
|
| 24 |
-
self.logger.info(f"Processing: {cmd}")
|
| 25 |
-
return self.robot.perform_action(cmd)
|
| 26 |
-
|
| 27 |
-
btn.click(process_command, inputs=input_txt, outputs=output_txt)
|
| 28 |
-
input_txt.submit(process_command, inputs=input_txt, outputs=output_txt)
|
| 29 |
|
| 30 |
return interface
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from .simulator_interface import VirtualRobot
|
| 3 |
import logging
|
| 4 |
|
| 5 |
+
class RoboSageInterface:
|
| 6 |
def __init__(self, title="RoboSage", description="Your virtual assistant"):
|
| 7 |
self.robot = VirtualRobot()
|
| 8 |
self.title = title
|
| 9 |
self.description = description
|
| 10 |
self.logger = logging.getLogger(__name__)
|
| 11 |
+
self._setup_logging()
|
| 12 |
+
|
| 13 |
+
# Initialize components
|
| 14 |
+
self.input_txt = None
|
| 15 |
+
self.output_txt = None
|
| 16 |
+
self.btn = None
|
| 17 |
+
|
| 18 |
+
self.interface = self._create_interface()
|
| 19 |
+
|
| 20 |
+
def _setup_logging(self):
|
| 21 |
+
logging.basicConfig(
|
| 22 |
+
level=logging.INFO,
|
| 23 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
def _create_interface(self) -> gr.Blocks:
|
| 27 |
with gr.Blocks(title=self.title) as interface:
|
| 28 |
+
# Store references to components as instance attributes
|
| 29 |
+
self._create_components()
|
| 30 |
+
self._setup_event_handlers()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
return interface
|
| 33 |
|
| 34 |
+
def _create_components(self):
|
| 35 |
+
"""Create and store all interface components"""
|
| 36 |
+
gr.Markdown(f"# 🤖 {self.title}\n\n{self.description}")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
self.input_txt = gr.Textbox(
|
| 40 |
+
label="Command",
|
| 41 |
+
placeholder="Try 'wave' or 'say hello'",
|
| 42 |
+
elem_id="command_input"
|
| 43 |
+
)
|
| 44 |
+
self.btn = gr.Button(
|
| 45 |
+
"Submit",
|
| 46 |
+
variant="primary",
|
| 47 |
+
elem_id="submit_btn"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
self.output_txt = gr.Textbox(
|
| 51 |
+
label="Response",
|
| 52 |
+
interactive=False,
|
| 53 |
+
elem_id="response_output"
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
def _setup_event_handlers(self):
|
| 57 |
+
"""Configure all event handlers"""
|
| 58 |
+
def wrapped_process_command(cmd: str) -> str:
|
| 59 |
+
self.logger.info(f"Processing command: {cmd}")
|
| 60 |
+
return self.robot.perform_action(cmd)
|
| 61 |
+
|
| 62 |
+
# Reference components through instance attributes
|
| 63 |
+
self.btn.click(
|
| 64 |
+
fn=wrapped_process_command,
|
| 65 |
+
inputs=self.input_txt,
|
| 66 |
+
outputs=self.output_txt
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
self.input_txt.submit(
|
| 70 |
+
fn=wrapped_process_command,
|
| 71 |
+
inputs=self.input_txt,
|
| 72 |
+
outputs=self.output_txt
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def launch_gradio_app(title="RoboSage", description="Your virtual assistant") -> gr.Blocks:
|
| 76 |
+
"""Public interface for launching the app"""
|
| 77 |
+
try:
|
| 78 |
+
return RoboSageInterface(title, description).interface
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logging.critical(f"Failed to create interface: {str(e)}")
|
| 81 |
+
raise
|