Update deployer/gradio_generator.py
Browse files- 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 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|