import os import tempfile import shutil import zipfile def deploy_callback(idea: str): """ Generates a minimal robot app based on the user's idea, packages it into a ZIP in the system temp directory, and returns a status message along with the path to the ZIP file. """ try: # Sanitize and build a unique directory name title = idea.strip().lower().replace(" ", "_") or "app" base_dir = os.path.join(tempfile.gettempdir(), f"robosage_{title}") # Clean out any old generation shutil.rmtree(base_dir, ignore_errors=True) os.makedirs(base_dir, exist_ok=True) # 1) Create main.py with a simple on_command stub main_py = os.path.join(base_dir, "main.py") with open(main_py, "w") as f: f.write(f"""def on_command(cmd): cmd_lower = cmd.strip().lower() if "hello" in cmd_lower: return "🤖 Hello there!" else: return f"🤔 I don't understand: '{{cmd}}'" """) # 2) Create a README readme = os.path.join(base_dir, "README.md") with open(readme, "w") as f: f.write(f"# RoboSage App\nGenerated for idea: {idea}\n\nRun `main.on_command(...)` to simulate.") # 3) Zip the directory zip_name = f"robosage_{title}.zip" zip_path = os.path.join(tempfile.gettempdir(), zip_name) with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: for root, _, files in os.walk(base_dir): for fname in files: fullpath = os.path.join(root, fname) arcname = os.path.relpath(fullpath, base_dir) zf.write(fullpath, arcname) return f"✅ Generated app: {idea}", zip_path except Exception as e: return f"❌ Error: {e}", None def robot_behavior(cmd: str) -> str: """ Simulate a robot response. You can swap this out for your core_creator or simulator_interface logic if desired. """ # Very basic echo-like behavior lower = cmd.strip().lower() if "hello" in lower: return "🤖 *waves* Hello there!" elif lower: return f"🤔 I don't know how to '{cmd}'." else: return "❓ Please say something." # Optional entrypoint if you want to import a launcher function def launch_gradio_app(): from app import main main()