Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +21 -98
deployer/gradio_generator.py
CHANGED
|
@@ -1,116 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from .simulator_interface import VirtualRobot
|
| 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 |
-
""
|
| 24 |
-
|
| 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 |
-
|
| 49 |
-
|
| 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 |
-
|
| 67 |
-
self.send_btn.click(
|
| 68 |
-
fn=self._process_command,
|
| 69 |
-
inputs=self.input_txt,
|
| 70 |
-
outputs=self.output_txt
|
| 71 |
-
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
outputs=self.output_txt
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 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 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
server_name="0.0.0.0",
|
| 112 |
-
server_port=7860,
|
| 113 |
-
share=False,
|
| 114 |
-
favicon_path=None,
|
| 115 |
-
debug=False
|
| 116 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from .simulator_interface import VirtualRobot
|
| 3 |
import logging
|
| 4 |
|
| 5 |
class RoboSageApp:
|
|
|
|
|
|
|
| 6 |
def __init__(self):
|
|
|
|
| 7 |
self.robot = VirtualRobot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
self.logger = logging.getLogger(__name__)
|
| 9 |
+
self.interface = self._build_interface()
|
| 10 |
+
|
| 11 |
def _build_interface(self) -> gr.Blocks:
|
| 12 |
+
with gr.Blocks(title="RoboSage") as interface:
|
| 13 |
+
gr.Markdown("# 🤖 RoboSage Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
with gr.Row():
|
| 16 |
+
input_txt = gr.Textbox(label="Command", placeholder="Try 'wave' or 'say hello'")
|
| 17 |
+
btn = gr.Button("Submit", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
output_txt = gr.Textbox(label="Response", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def process_command(cmd: str) -> str:
|
| 22 |
+
self.logger.info(f"Processing: {cmd}")
|
| 23 |
+
return self.robot.perform_action(cmd)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
btn.click(process_command, inputs=input_txt, outputs=output_txt)
|
| 26 |
+
input_txt.submit(process_command, inputs=input_txt, outputs=output_txt)
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
return interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# The exported function name must match what's imported
|
| 31 |
+
def launch_gradio_app() -> gr.Blocks:
|
| 32 |
+
"""The function that app.py imports must have this exact name"""
|
| 33 |
+
return RoboSageApp().interface
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# For local testing
|
| 36 |
if __name__ == "__main__":
|
| 37 |
+
logging.basicConfig(level=logging.INFO)
|
| 38 |
+
app = launch_gradio_app()
|
| 39 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|