File size: 2,376 Bytes
9730a79 1d0d679 cda81c6 52c1638 f1624e3 70154bd cda81c6 1b36f73 f1624e3 cda81c6 1d0d679 cda81c6 1d0d679 cda81c6 51dfad8 cda81c6 38e1d70 cda81c6 52c1638 cda81c6 52c1638 cda81c6 51dfad8 cda81c6 51dfad8 1d0d679 cda81c6 51dfad8 cda81c6 51dfad8 cda81c6 51dfad8 cda81c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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()
|