File size: 3,456 Bytes
157cb00
d078b7e
157cb00
 
 
376658d
157cb00
 
 
 
 
2e93b96
157cb00
2e93b96
134db29
157cb00
 
 
 
 
 
 
 
 
 
 
 
2e93b96
157cb00
 
 
 
 
 
 
 
 
 
 
 
2e93b96
157cb00
2e93b96
157cb00
 
 
 
 
 
 
 
 
 
 
 
2e93b96
157cb00
 
 
 
 
 
 
 
 
 
 
 
 
2e93b96
157cb00
 
 
 
 
2e93b96
 
157cb00
 
 
 
2e93b96
157cb00
 
630a481
157cb00
 
 
 
 
 
 
 
 
839548e
630a481
157cb00
 
 
a294fd3
 
2e93b96
157cb00
 
 
 
a294fd3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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
    )