Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +55 -34
deployer/gradio_generator.py
CHANGED
@@ -1,54 +1,75 @@
|
|
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
|
4 |
from core_creator.voice_to_app import VoiceToAppCreator
|
5 |
from deployer.simulator_interface import VirtualRobot
|
6 |
|
7 |
-
def _make_app_zip(src_dir: str, title: str) -> str:
|
8 |
-
"""
|
9 |
-
Zips up the directory at src_dir into a file named <title>.zip
|
10 |
-
in the system temp directory, and returns the full path.
|
11 |
-
"""
|
12 |
-
safe_title = "".join(c if c.isalnum() or c in "-_." else "_" for c in title)
|
13 |
-
zip_filename = f"{safe_title}.zip"
|
14 |
-
tmp_dir = tempfile.gettempdir()
|
15 |
-
zip_path = os.path.join(tmp_dir, zip_filename)
|
16 |
-
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
17 |
-
for root, _, files in os.walk(src_dir):
|
18 |
-
for fname in files:
|
19 |
-
full_path = os.path.join(root, fname)
|
20 |
-
# store relative path within the zip
|
21 |
-
arcname = os.path.relpath(full_path, src_dir)
|
22 |
-
zf.write(full_path, arcname)
|
23 |
-
return zip_path
|
24 |
-
|
25 |
def deploy_callback(idea: str):
|
26 |
"""
|
27 |
-
|
28 |
-
|
29 |
"""
|
30 |
try:
|
31 |
creator = VoiceToAppCreator(idea)
|
32 |
assets = creator.run_pipeline()
|
33 |
-
# Title for status
|
34 |
title = assets.get("blueprint", {}).get("title", "<unknown>")
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
zip_path =
|
42 |
return f"β
Generated app: {title}", zip_path
|
43 |
-
|
44 |
except Exception as e:
|
45 |
-
return f"β
|
|
|
46 |
|
47 |
def robot_behavior(command: str) -> str:
|
48 |
"""
|
49 |
-
|
50 |
"""
|
51 |
try:
|
52 |
return VirtualRobot().perform_action(command)
|
53 |
except Exception as e:
|
54 |
-
return f"β Simulator error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### deployer/gradio_generator.py
|
2 |
import os
|
3 |
+
import shutil
|
4 |
+
tempfile
|
5 |
from core_creator.voice_to_app import VoiceToAppCreator
|
6 |
from deployer.simulator_interface import VirtualRobot
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def deploy_callback(idea: str):
|
9 |
"""
|
10 |
+
Generate robot app from idea and package into a ZIP.
|
11 |
+
Returns (status_message, zip_path)
|
12 |
"""
|
13 |
try:
|
14 |
creator = VoiceToAppCreator(idea)
|
15 |
assets = creator.run_pipeline()
|
|
|
16 |
title = assets.get("blueprint", {}).get("title", "<unknown>")
|
17 |
+
app_dir = assets.get("app_dir")
|
18 |
+
if not app_dir or not os.path.isdir(app_dir):
|
19 |
+
return f"β Failed to generate app: {idea}", None
|
20 |
+
# Create temporary directory for zip
|
21 |
+
tmpdir = tempfile.mkdtemp()
|
22 |
+
zip_base = os.path.join(tmpdir, "app")
|
23 |
+
zip_path = shutil.make_archive(zip_base, 'zip', app_dir)
|
24 |
return f"β
Generated app: {title}", zip_path
|
|
|
25 |
except Exception as e:
|
26 |
+
return f"β Error: {str(e)}", None
|
27 |
+
|
28 |
|
29 |
def robot_behavior(command: str) -> str:
|
30 |
"""
|
31 |
+
Simulate robot behavior based on input command.
|
32 |
"""
|
33 |
try:
|
34 |
return VirtualRobot().perform_action(command)
|
35 |
except Exception as e:
|
36 |
+
return f"β Simulator error: {str(e)}"
|
37 |
+
|
38 |
+
|
39 |
+
### app.py
|
40 |
+
import os
|
41 |
+
import gradio as gr
|
42 |
+
from deployer.gradio_generator import deploy_callback, robot_behavior
|
43 |
+
|
44 |
+
def generate_app(idea: str):
|
45 |
+
"""
|
46 |
+
Gradio interface callback to generate app and return status and ZIP file.
|
47 |
+
"""
|
48 |
+
status, zip_path = deploy_callback(idea)
|
49 |
+
return status, zip_path
|
50 |
+
|
51 |
+
# Build Gradio UI
|
52 |
+
with gr.Blocks(css="""
|
53 |
+
.gradio-container { max-width: 900px; margin: auto; }
|
54 |
+
.section { padding: 1rem; }
|
55 |
+
""") as demo:
|
56 |
+
gr.Markdown("# π RoboSage\nGenerate your custom robot app, download it, then test it live.")
|
57 |
+
|
58 |
+
with gr.Column():
|
59 |
+
gr.Markdown("## 1οΈβ£ Generate & Download App")
|
60 |
+
idea_input = gr.Textbox(label="Your Robot Idea", placeholder="e.g. A friendly greeting robot.", lines=2)
|
61 |
+
gen_btn = gr.Button("Generate App & ZIP")
|
62 |
+
status_out = gr.Textbox(label="Status", interactive=False)
|
63 |
+
zip_out = gr.File(label="Download App ZIP")
|
64 |
+
gen_btn.click(fn=generate_app, inputs=[idea_input], outputs=[status_out, zip_out])
|
65 |
+
|
66 |
+
with gr.Column():
|
67 |
+
gr.Markdown("## 2οΈβ£ Robot Simulator")
|
68 |
+
cmd_input = gr.Textbox(label="Command", placeholder="say 'hello' or 'You rock!'")
|
69 |
+
sim_btn = gr.Button("Send Command")
|
70 |
+
sim_out = gr.Textbox(label="Robot Response", interactive=False)
|
71 |
+
sim_btn.click(fn=robot_behavior, inputs=[cmd_input], outputs=[sim_out])
|
72 |
+
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)), share=False)
|