Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +74 -65
deployer/gradio_generator.py
CHANGED
|
@@ -1,107 +1,116 @@
|
|
| 1 |
-
# gradio_generator.py - Robust Gradio Interface
|
| 2 |
import gradio as gr
|
| 3 |
-
from simulator_interface import VirtualRobot
|
| 4 |
-
from typing import Optional
|
| 5 |
import logging
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class RoboSageInterface:
|
| 10 |
-
"""Main application interface with lifecycle management."""
|
| 11 |
|
| 12 |
def __init__(self):
|
|
|
|
| 13 |
self.robot = VirtualRobot()
|
| 14 |
-
self.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return f"❌ System error: {str(e)}"
|
| 28 |
-
|
| 29 |
-
def _create_interface(self) -> gr.Blocks:
|
| 30 |
-
"""Build Gradio interface with proper component binding."""
|
| 31 |
with gr.Blocks(
|
| 32 |
-
title="RoboSage",
|
| 33 |
-
css="
|
|
|
|
|
|
|
|
|
|
| 34 |
) as interface:
|
| 35 |
|
| 36 |
# Header section
|
| 37 |
gr.Markdown("""
|
| 38 |
-
# 🤖 RoboSage
|
| 39 |
-
### Your
|
| 40 |
""")
|
| 41 |
|
| 42 |
# Interaction panel
|
| 43 |
with gr.Row():
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Output section
|
| 58 |
-
self.
|
| 59 |
label="Robot Response",
|
| 60 |
interactive=False,
|
| 61 |
-
lines=5
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# History section
|
| 65 |
-
with gr.Accordion("Session History", open=False):
|
| 66 |
-
self.history = gr.JSON(
|
| 67 |
-
label="Command History",
|
| 68 |
-
value={"commands": []}
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
# Event handlers
|
| 72 |
-
self.
|
| 73 |
-
fn=self.
|
| 74 |
-
inputs=self.
|
| 75 |
-
outputs=self.
|
| 76 |
)
|
| 77 |
|
| 78 |
-
self.
|
| 79 |
-
fn=self.
|
| 80 |
-
inputs=self.
|
| 81 |
-
outputs=self.
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
return interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
def
|
| 87 |
"""Initialize and return the application interface."""
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
app = RoboSageInterface()
|
| 91 |
return app.interface
|
| 92 |
except Exception as e:
|
| 93 |
logging.critical("Application failed to initialize: %s", str(e))
|
| 94 |
raise
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
server_name="0.0.0.0",
|
| 101 |
server_port=7860,
|
| 102 |
share=False,
|
| 103 |
favicon_path=None,
|
| 104 |
-
auth=None,
|
| 105 |
-
ssl_verify=True,
|
| 106 |
debug=False
|
| 107 |
)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from .simulator_interface import VirtualRobot # Relative import
|
|
|
|
| 3 |
import logging
|
| 4 |
|
| 5 |
+
class RoboSageApp:
|
| 6 |
+
"""Main application class for Gradio interface."""
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def __init__(self):
|
| 9 |
+
"""Initialize application components."""
|
| 10 |
self.robot = VirtualRobot()
|
| 11 |
+
self._setup_logging()
|
| 12 |
+
self.interface = self._build_interface()
|
| 13 |
+
|
| 14 |
+
def _setup_logging(self):
|
| 15 |
+
"""Configure application logging."""
|
| 16 |
+
logging.basicConfig(
|
| 17 |
+
level=logging.INFO,
|
| 18 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 19 |
+
)
|
| 20 |
+
self.logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
def _build_interface(self) -> gr.Blocks:
|
| 23 |
+
"""Construct the Gradio interface."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
with gr.Blocks(
|
| 25 |
+
title="RoboSage Assistant",
|
| 26 |
+
css="""
|
| 27 |
+
.gradio-container {max-width: 600px !important}
|
| 28 |
+
.dark {background: #f9f9f9}
|
| 29 |
+
"""
|
| 30 |
) as interface:
|
| 31 |
|
| 32 |
# Header section
|
| 33 |
gr.Markdown("""
|
| 34 |
+
# 🤖 RoboSage
|
| 35 |
+
### Your Personal Virtual Assistant
|
| 36 |
""")
|
| 37 |
|
| 38 |
# Interaction panel
|
| 39 |
with gr.Row():
|
| 40 |
+
self.input_txt = gr.Textbox(
|
| 41 |
+
label="Your Command",
|
| 42 |
+
placeholder="Try 'wave' or 'say hello'...",
|
| 43 |
+
max_lines=3,
|
| 44 |
+
interactive=True
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
self.send_btn = gr.Button(
|
| 49 |
+
"Submit",
|
| 50 |
+
variant="primary",
|
| 51 |
+
size="lg"
|
| 52 |
+
)
|
| 53 |
+
self.clear_btn = gr.Button(
|
| 54 |
+
"Clear",
|
| 55 |
+
variant="secondary"
|
| 56 |
+
)
|
| 57 |
|
| 58 |
# Output section
|
| 59 |
+
self.output_txt = gr.Textbox(
|
| 60 |
label="Robot Response",
|
| 61 |
interactive=False,
|
| 62 |
+
lines=5,
|
| 63 |
+
show_copy_button=True
|
| 64 |
)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# Event handlers
|
| 67 |
+
self.send_btn.click(
|
| 68 |
+
fn=self._process_command,
|
| 69 |
+
inputs=self.input_txt,
|
| 70 |
+
outputs=self.output_txt
|
| 71 |
)
|
| 72 |
|
| 73 |
+
self.input_txt.submit(
|
| 74 |
+
fn=self._process_command,
|
| 75 |
+
inputs=self.input_txt,
|
| 76 |
+
outputs=self.output_txt
|
| 77 |
)
|
| 78 |
|
| 79 |
+
self.clear_btn.click(
|
| 80 |
+
fn=lambda: ("", ""),
|
| 81 |
+
inputs=None,
|
| 82 |
+
outputs=[self.input_txt, self.output_txt]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
return interface
|
| 86 |
+
|
| 87 |
+
def _process_command(self, command: str) -> str:
|
| 88 |
+
"""Handle user commands with logging."""
|
| 89 |
+
self.logger.info("Processing command: %s", command)
|
| 90 |
+
try:
|
| 91 |
+
response = self.robot.perform_action(command)
|
| 92 |
+
self.logger.info("Generated response: %s", response)
|
| 93 |
+
return response
|
| 94 |
+
except Exception as e:
|
| 95 |
+
self.logger.error("Command processing failed: %s", str(e))
|
| 96 |
+
return f"⚠️ System error: {str(e)}"
|
| 97 |
|
| 98 |
+
def launch_app() -> gr.Blocks:
|
| 99 |
"""Initialize and return the application interface."""
|
| 100 |
try:
|
| 101 |
+
app = RoboSageApp()
|
|
|
|
| 102 |
return app.interface
|
| 103 |
except Exception as e:
|
| 104 |
logging.critical("Application failed to initialize: %s", str(e))
|
| 105 |
raise
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
# Local development configuration
|
| 109 |
+
interface = launch_app()
|
| 110 |
+
interface.launch(
|
| 111 |
server_name="0.0.0.0",
|
| 112 |
server_port=7860,
|
| 113 |
share=False,
|
| 114 |
favicon_path=None,
|
|
|
|
|
|
|
| 115 |
debug=False
|
| 116 |
)
|