mgbam commited on
Commit
52c1638
Β·
verified Β·
1 Parent(s): 8313465

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +41 -19
deployer/gradio_generator.py CHANGED
@@ -1,42 +1,64 @@
1
  import os
2
  import shutil
3
  import tempfile
 
 
 
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
- Generates the robot app for the given idea, zips it, and returns
10
- (status_message, path_to_zip_file or None).
11
  """
12
  try:
13
- # Run your pipeline
14
- creator = VoiceToAppCreator(idea)
15
- assets = creator.run_pipeline()
 
 
 
 
 
 
 
16
 
17
- # Extract output directory & title
18
- app_dir = assets.get("app_dir")
19
  title = assets.get("blueprint", {}).get("title", "<unknown>")
20
 
21
- if not app_dir or not os.path.isdir(app_dir):
22
- return f"❌ Failed to generate app directory for: {idea}", None
23
 
24
- # Create a temp ZIP file (will persist until process exits)
25
- tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
26
- tmp_path = tmp.name
27
- tmp.close()
28
 
29
- # Make archive: shutil wants base name without extension
30
- base = tmp_path[:-4]
31
- shutil.make_archive(base, 'zip', root_dir=app_dir)
 
 
 
 
32
 
33
- return f"βœ… Generated app: {title}", tmp_path
34
 
35
  except Exception as e:
36
- return f"❌ Error generating app: {e}", None
37
 
38
  def robot_behavior(user_input: str) -> str:
39
  """
40
- Simulate robot behavior.
41
  """
42
  return VirtualRobot().perform_action(user_input)
 
1
  import os
2
  import shutil
3
  import tempfile
4
+ import zipfile
5
+ import asyncio
6
+
7
  from core_creator.voice_to_app import VoiceToAppCreator
8
  from deployer.simulator_interface import VirtualRobot
9
 
10
+ async def _run_pipeline(idea: str) -> dict:
11
+ """
12
+ Asynchronously run the voice-to-app pipeline.
13
+ Returns the assets dict from VoiceToAppCreator.run_pipeline().
14
+ """
15
+ creator = VoiceToAppCreator(idea)
16
+ return creator.run_pipeline()
17
+
18
  def deploy_callback(idea: str):
19
  """
20
+ Synchronous wrapper for Gradio: runs the pipeline, packages the generated app
21
+ into a ZIP, and returns (status_message, zip_file_path).
22
  """
23
  try:
24
+ # Run pipeline in the existing event loop or new one
25
+ try:
26
+ loop = asyncio.get_running_loop()
27
+ except RuntimeError:
28
+ loop = None
29
+
30
+ if loop and loop.is_running():
31
+ assets = asyncio.run_coroutine_threadsafe(_run_pipeline(idea), loop).result()
32
+ else:
33
+ assets = asyncio.new_event_loop().run_until_complete(_run_pipeline(idea))
34
 
35
+ # Get the generated project directory from assets
36
+ project_dir = assets.get("project_dir")
37
  title = assets.get("blueprint", {}).get("title", "<unknown>")
38
 
39
+ if not project_dir or not os.path.isdir(project_dir):
40
+ return f"❌ No generated directory found for: {idea}", None
41
 
42
+ # Copy into a temp dir to zip
43
+ tmp_root = tempfile.mkdtemp(prefix="robosage_")
44
+ dst_dir = os.path.join(tmp_root, "app")
45
+ shutil.copytree(project_dir, dst_dir)
46
 
47
+ zip_path = os.path.join(tmp_root, "app.zip")
48
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
49
+ for root, _, files in os.walk(dst_dir):
50
+ for fname in files:
51
+ full_path = os.path.join(root, fname)
52
+ rel_path = os.path.relpath(full_path, dst_dir)
53
+ zf.write(full_path, arcname=os.path.join(title, rel_path))
54
 
55
+ return f"βœ… Generated app: {title}", zip_path
56
 
57
  except Exception as e:
58
+ return f"❌ Failed to generate app for '{idea}': {e}", None
59
 
60
  def robot_behavior(user_input: str) -> str:
61
  """
62
+ Hook into the VirtualRobot simulator.
63
  """
64
  return VirtualRobot().perform_action(user_input)