Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +38 -23
deployer/gradio_generator.py
CHANGED
|
@@ -2,52 +2,67 @@ import gradio as gr
|
|
| 2 |
from .simulator_interface import VirtualRobot
|
| 3 |
import logging
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 7 |
robot = VirtualRobot()
|
| 8 |
|
| 9 |
-
# Define
|
| 10 |
-
def process_command(
|
| 11 |
try:
|
| 12 |
-
return robot.perform_action(
|
| 13 |
except Exception as e:
|
| 14 |
-
logging.error(f"Command processing
|
| 15 |
return f"⚠️ Error: {str(e)}"
|
| 16 |
|
| 17 |
-
# Build interface in one atomic operation
|
| 18 |
-
with gr.Blocks(title=title) as interface:
|
| 19 |
gr.Markdown(f"# 🤖 {title}\n\n{description}")
|
| 20 |
|
| 21 |
-
# Create and use components
|
| 22 |
with gr.Row():
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Define all event handlers in the same context
|
| 30 |
submit_btn.click(
|
| 31 |
fn=process_command,
|
| 32 |
-
inputs=
|
| 33 |
-
outputs=
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
| 37 |
fn=process_command,
|
| 38 |
-
inputs=
|
| 39 |
-
outputs=
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
inputs=None,
|
| 45 |
-
outputs=[
|
| 46 |
)
|
| 47 |
|
| 48 |
return interface
|
| 49 |
|
| 50 |
def launch_gradio_app(title="RoboSage", description="Your virtual assistant"):
|
| 51 |
-
"""Public interface that
|
| 52 |
logging.basicConfig(level=logging.INFO)
|
| 53 |
-
return
|
|
|
|
| 2 |
from .simulator_interface import VirtualRobot
|
| 3 |
import logging
|
| 4 |
|
| 5 |
+
def create_standalone_interface(title="RoboSage", description="Your virtual assistant"):
|
| 6 |
+
"""
|
| 7 |
+
Creates a completely self-contained interface that avoids component reference issues
|
| 8 |
+
by defining and using all components in the same context.
|
| 9 |
+
"""
|
| 10 |
robot = VirtualRobot()
|
| 11 |
|
| 12 |
+
# Define the command processor
|
| 13 |
+
def process_command(cmd):
|
| 14 |
try:
|
| 15 |
+
return robot.perform_action(cmd)
|
| 16 |
except Exception as e:
|
| 17 |
+
logging.error(f"Command processing error: {e}")
|
| 18 |
return f"⚠️ Error: {str(e)}"
|
| 19 |
|
| 20 |
+
# Build the interface in one atomic operation
|
| 21 |
+
with gr.Blocks(title=title, analytics_enabled=False) as interface:
|
| 22 |
gr.Markdown(f"# 🤖 {title}\n\n{description}")
|
| 23 |
|
| 24 |
+
# Create and immediately use components
|
| 25 |
with gr.Row():
|
| 26 |
+
cmd_input = gr.Textbox(
|
| 27 |
+
label="Command Input",
|
| 28 |
+
placeholder="Try 'wave' or 'say hello'...",
|
| 29 |
+
elem_id="cmd_input"
|
| 30 |
+
)
|
| 31 |
+
submit_btn = gr.Button(
|
| 32 |
+
"Submit",
|
| 33 |
+
variant="primary",
|
| 34 |
+
elem_id="submit_btn"
|
| 35 |
+
)
|
| 36 |
|
| 37 |
+
response_output = gr.Textbox(
|
| 38 |
+
label="Robot Response",
|
| 39 |
+
interactive=False,
|
| 40 |
+
elem_id="response_output"
|
| 41 |
+
)
|
| 42 |
|
| 43 |
# Define all event handlers in the same context
|
| 44 |
submit_btn.click(
|
| 45 |
fn=process_command,
|
| 46 |
+
inputs=cmd_input,
|
| 47 |
+
outputs=response_output
|
| 48 |
)
|
| 49 |
|
| 50 |
+
cmd_input.submit(
|
| 51 |
fn=process_command,
|
| 52 |
+
inputs=cmd_input,
|
| 53 |
+
outputs=response_output
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Add clear functionality
|
| 57 |
+
gr.Button("Clear").click(
|
| 58 |
+
lambda: ("", ""),
|
| 59 |
inputs=None,
|
| 60 |
+
outputs=[cmd_input, response_output]
|
| 61 |
)
|
| 62 |
|
| 63 |
return interface
|
| 64 |
|
| 65 |
def launch_gradio_app(title="RoboSage", description="Your virtual assistant"):
|
| 66 |
+
"""Public interface that guarantees proper initialization"""
|
| 67 |
logging.basicConfig(level=logging.INFO)
|
| 68 |
+
return create_standalone_interface(title, description)
|