mgbam commited on
Commit
1d5d1d4
·
verified ·
1 Parent(s): ae848b5

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +35 -64
deployer/gradio_generator.py CHANGED
@@ -1,68 +1,39 @@
 
1
  import gradio as gr
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)
 
 
 
 
 
 
 
 
 
 
1
+ # gradio_generator.py - CPU & Spaces-compatible Gradio interface for RoboSage
2
  import gradio as gr
3
+ from deployer.simulator_interface import VirtualRobot
 
4
 
5
+ # Robot behavior logic
6
+ def robot_behavior(user_input: str) -> str:
7
+ """Bridge between user input and VirtualRobot actions."""
8
+ bot = VirtualRobot()
9
+ text = user_input.strip().lower()
10
+
11
+ # Greeting detection
12
+ if any(greet in text for greet in ["hello", "hi", "hey", "welcome"]):
13
+ return bot.perform_action("wave") + "\n" + bot.perform_action("say Hello there!")
14
+
15
+ # Direct speech command
16
+ if text.startswith("say "):
17
+ return bot.perform_action(text)
18
+
19
+ # Default fallback
20
+ return bot.perform_action("say I'm sorry, I didn't understand that.")
21
+
22
+ # Build Gradio app with proper Blocks context
23
+ def launch_gradio_app(title: str = "RoboSage App", description: str = "Your robot, your voice.") -> gr.Blocks:
24
+ with gr.Blocks() as demo:
25
  gr.Markdown(f"# 🤖 {title}\n\n{description}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ inp = gr.Textbox(label="Speak or Type", lines=1, placeholder="Type or speak your command...")
28
+ out = gr.Textbox(label="Robot Response", lines=4)
29
+ btn = gr.Button("Send", key="send-btn")
30
+
31
+ # Wire click event inside Blocks context
32
+ btn.click(fn=robot_behavior, inputs=[inp], outputs=[out])
33
+
34
+ return demo
35
+
36
+ # For local testing
37
+ if __name__ == "__main__":
38
+ app = launch_gradio_app()
39
+ app.launch()