Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +35 -28
deployer/gradio_generator.py
CHANGED
@@ -4,44 +4,51 @@ import zipfile
|
|
4 |
from core_creator.voice_to_app import VoiceToAppCreator
|
5 |
from deployer.simulator_interface import VirtualRobot
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def deploy_callback(idea: str):
|
8 |
"""
|
9 |
-
|
10 |
-
|
11 |
-
Returns (status_message, zip_file_path_or_None)
|
12 |
"""
|
13 |
-
# --- 1) Run pipeline ---
|
14 |
try:
|
15 |
creator = VoiceToAppCreator(idea)
|
16 |
-
assets = creator.run_pipeline()
|
|
|
17 |
title = assets.get("blueprint", {}).get("title", "<unknown>")
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
# create a fresh temp directory
|
24 |
-
base_dir = tempfile.mkdtemp(prefix="robosage_")
|
25 |
-
zip_path = os.path.join(base_dir, "app.zip")
|
26 |
-
|
27 |
-
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
28 |
-
for fname, content in assets.get("files", {}).items():
|
29 |
-
# ensure subdirs exist
|
30 |
-
full_path = os.path.join(base_dir, fname)
|
31 |
-
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
32 |
-
# write the generated file
|
33 |
-
with open(full_path, "w", encoding="utf-8") as f:
|
34 |
-
f.write(content)
|
35 |
-
# add to zip under its relative name
|
36 |
-
zf.write(full_path, arcname=fname)
|
37 |
|
|
|
38 |
return f"β
Generated app: {title}", zip_path
|
39 |
|
40 |
except Exception as e:
|
41 |
-
return f"β Failed to
|
42 |
|
43 |
-
def robot_behavior(
|
44 |
"""
|
45 |
-
|
46 |
"""
|
47 |
-
|
|
|
|
|
|
|
|
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 |
+
Generates an app from the user idea and returns:
|
28 |
+
(status_message: str, path_to_zip: Optional[str])
|
|
|
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 |
+
# Directory where the app was generated
|
36 |
+
project_dir = assets.get("project_dir") or assets.get("app_dir")
|
37 |
+
if project_dir is None or not os.path.isdir(project_dir):
|
38 |
+
# No directory to zip up
|
39 |
+
return f"β No generated directory found for '{title}'", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
zip_path = _make_app_zip(project_dir, title)
|
42 |
return f"β
Generated app: {title}", zip_path
|
43 |
|
44 |
except Exception as e:
|
45 |
+
return f"β Failed to generate app: {e}", None
|
46 |
|
47 |
+
def robot_behavior(command: str) -> str:
|
48 |
"""
|
49 |
+
Feeds the user command into the VirtualRobot simulator.
|
50 |
"""
|
51 |
+
try:
|
52 |
+
return VirtualRobot().perform_action(command)
|
53 |
+
except Exception as e:
|
54 |
+
return f"β Simulator error: {e}"
|