Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +87 -38
deployer/gradio_generator.py
CHANGED
|
@@ -1,58 +1,107 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def __init__(self):
|
| 6 |
-
self.
|
| 7 |
self.interface = self._create_interface()
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def _create_interface(self):
|
| 20 |
-
"""
|
| 21 |
-
with gr.Blocks(
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
with gr.Row():
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
# Event handlers
|
| 31 |
-
self.
|
| 32 |
-
fn=self.
|
| 33 |
-
inputs=self.
|
| 34 |
-
outputs=self.
|
| 35 |
)
|
| 36 |
|
| 37 |
-
self.
|
| 38 |
-
fn=self.
|
| 39 |
-
inputs=self.
|
| 40 |
-
outputs=self.
|
| 41 |
)
|
| 42 |
-
|
| 43 |
-
return
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
server_name="0.0.0.0",
|
| 55 |
server_port=7860,
|
| 56 |
share=False,
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
|
|
|
| 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 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
|
| 9 |
+
class RoboSageInterface:
|
| 10 |
+
"""Main application interface with lifecycle management."""
|
| 11 |
+
|
| 12 |
def __init__(self):
|
| 13 |
+
self.robot = VirtualRobot()
|
| 14 |
self.interface = self._create_interface()
|
| 15 |
|
| 16 |
+
def _robot_response(self, user_input: str) -> str:
|
| 17 |
+
"""Process user input with error handling."""
|
| 18 |
+
if not user_input or not isinstance(user_input, str):
|
| 19 |
+
logging.warning("Invalid input received")
|
| 20 |
+
return "β Please enter valid text"
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
logging.info("Processing input: %s", user_input)
|
| 24 |
+
return self.robot.perform_action(user_input)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
logging.error("Response generation failed: %s", str(e))
|
| 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=".gradio-container {max-width: 600px !important}"
|
| 34 |
+
) as interface:
|
| 35 |
+
|
| 36 |
+
# Header section
|
| 37 |
+
gr.Markdown("""
|
| 38 |
+
# π€ RoboSage
|
| 39 |
+
### Your personal virtual assistant
|
| 40 |
+
""")
|
| 41 |
|
| 42 |
+
# Interaction panel
|
| 43 |
with gr.Row():
|
| 44 |
+
with gr.Column(scale=4):
|
| 45 |
+
self.input_box = gr.Textbox(
|
| 46 |
+
label="Command Input",
|
| 47 |
+
placeholder="Try 'hello' or 'say something'...",
|
| 48 |
+
max_lines=3
|
| 49 |
+
)
|
| 50 |
+
with gr.Column(scale=1):
|
| 51 |
+
self.submit_btn = gr.Button(
|
| 52 |
+
"Send",
|
| 53 |
+
variant="primary",
|
| 54 |
+
size="lg"
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
# Output section
|
| 58 |
+
self.output_box = gr.Textbox(
|
| 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.submit_btn.click(
|
| 73 |
+
fn=self._robot_response,
|
| 74 |
+
inputs=self.input_box,
|
| 75 |
+
outputs=self.output_box
|
| 76 |
)
|
| 77 |
|
| 78 |
+
self.input_box.submit(
|
| 79 |
+
fn=self._robot_response,
|
| 80 |
+
inputs=self.input_box,
|
| 81 |
+
outputs=self.output_box
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
return interface
|
| 85 |
|
| 86 |
+
def launch_application() -> gr.Blocks:
|
| 87 |
+
"""Initialize and return the application interface."""
|
| 88 |
+
try:
|
| 89 |
+
logging.info("Initializing RoboSage application")
|
| 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 |
+
# Production configuration
|
| 98 |
+
app = launch_application()
|
| 99 |
+
app.launch(
|
| 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 |
)
|