robosage / deployer /gradio_generator.py
mgbam's picture
Update deployer/gradio_generator.py
157cb00 verified
raw
history blame
3.46 kB
# gradio_generator.py - Robust Gradio Interface
import gradio as gr
from simulator_interface import VirtualRobot
from typing import Optional
import logging
logging.basicConfig(level=logging.INFO)
class RoboSageInterface:
"""Main application interface with lifecycle management."""
def __init__(self):
self.robot = VirtualRobot()
self.interface = self._create_interface()
def _robot_response(self, user_input: str) -> str:
"""Process user input with error handling."""
if not user_input or not isinstance(user_input, str):
logging.warning("Invalid input received")
return "❌ Please enter valid text"
try:
logging.info("Processing input: %s", user_input)
return self.robot.perform_action(user_input)
except Exception as e:
logging.error("Response generation failed: %s", str(e))
return f"❌ System error: {str(e)}"
def _create_interface(self) -> gr.Blocks:
"""Build Gradio interface with proper component binding."""
with gr.Blocks(
title="RoboSage",
css=".gradio-container {max-width: 600px !important}"
) as interface:
# Header section
gr.Markdown("""
# πŸ€– RoboSage
### Your personal virtual assistant
""")
# Interaction panel
with gr.Row():
with gr.Column(scale=4):
self.input_box = gr.Textbox(
label="Command Input",
placeholder="Try 'hello' or 'say something'...",
max_lines=3
)
with gr.Column(scale=1):
self.submit_btn = gr.Button(
"Send",
variant="primary",
size="lg"
)
# Output section
self.output_box = gr.Textbox(
label="Robot Response",
interactive=False,
lines=5
)
# History section
with gr.Accordion("Session History", open=False):
self.history = gr.JSON(
label="Command History",
value={"commands": []}
)
# Event handlers
self.submit_btn.click(
fn=self._robot_response,
inputs=self.input_box,
outputs=self.output_box
)
self.input_box.submit(
fn=self._robot_response,
inputs=self.input_box,
outputs=self.output_box
)
return interface
def launch_application() -> gr.Blocks:
"""Initialize and return the application interface."""
try:
logging.info("Initializing RoboSage application")
app = RoboSageInterface()
return app.interface
except Exception as e:
logging.critical("Application failed to initialize: %s", str(e))
raise
if __name__ == "__main__":
# Production configuration
app = launch_application()
app.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
favicon_path=None,
auth=None,
ssl_verify=True,
debug=False
)