repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import json |
| #2 | import os |
| #3 | import shutil |
| #4 | import signal |
| #5 | import subprocess |
| #6 | import sys |
| #7 | import tempfile |
| #8 | import time |
| #9 | import zipfile |
| #10 | from pathlib import Path |
| #11 | |
| #12 | import click |
| #13 | import requests |
| #14 | from rich.console import Console |
| #15 | |
| #16 | from embedchain.telemetry.posthog import AnonymousTelemetry |
| #17 | from embedchain.utils.cli import ( |
| #18 | deploy_fly, |
| #19 | deploy_gradio_app, |
| #20 | deploy_hf_spaces, |
| #21 | deploy_modal, |
| #22 | deploy_render, |
| #23 | deploy_streamlit, |
| #24 | get_pkg_path_from_name, |
| #25 | setup_fly_io_app, |
| #26 | setup_gradio_app, |
| #27 | setup_hf_app, |
| #28 | setup_modal_com_app, |
| #29 | setup_render_com_app, |
| #30 | setup_streamlit_io_app, |
| #31 | ) |
| #32 | |
| #33 | console = Console() |
| #34 | api_process = None |
| #35 | ui_process = None |
| #36 | |
| #37 | anonymous_telemetry = AnonymousTelemetry() |
| #38 | |
| #39 | |
| #40 | def signal_handler(sig, frame): |
| #41 | """Signal handler to catch termination signals and kill server processes.""" |
| #42 | global api_process, ui_process |
| #43 | console.print("\n🛑 [bold yellow]Stopping servers...[/bold yellow]") |
| #44 | if api_process: |
| #45 | api_process.terminate() |
| #46 | console.print("🛑 [bold yellow]API server stopped.[/bold yellow]") |
| #47 | if ui_process: |
| #48 | ui_process.terminate() |
| #49 | console.print("🛑 [bold yellow]UI server stopped.[/bold yellow]") |
| #50 | sys.exit(0) |
| #51 | |
| #52 | |
| #53 | @click.group() |
| #54 | def cli(): |
| #55 | pass |
| #56 | |
| #57 | |
| #58 | @cli.command() |
| #59 | @click.argument("app_name") |
| #60 | @click.option("--docker", is_flag=True, help="Use docker to create the app.") |
| #61 | @click.pass_context |
| #62 | def create_app(ctx, app_name, docker): |
| #63 | if Path(app_name).exists(): |
| #64 | console.print( |
| #65 | f"❌ [red]Directory '{app_name}' already exists. Try using a new directory name, or remove it.[/red]" |
| #66 | ) |
| #67 | return |
| #68 | |
| #69 | os.makedirs(app_name) |
| #70 | os.chdir(app_name) |
| #71 | |
| #72 | # Step 1: Download the zip file |
| #73 | zip_url = "http://github.com/embedchain/ec-admin/archive/main.zip" |
| #74 | console.print(f"Creating a new embedchain app in [green]{Path().resolve()}[/green]\n") |
| #75 | try: |
| #76 | response = requests.get(zip_url) |
| #77 | response.raise_for_status() |
| #78 | with tempfile.NamedTemporaryFile(delete=False) as tmp_file: |
| #79 | tmp_file.write(response.content) |
| #80 | zip_file_path = tmp_file.name |
| #81 | console.print("✅ [bold green]Fetched template successfully.[/bold green]") |
| #82 | except requests.RequestException as e: |
| #83 | console.print(f"❌ [bold red]Failed to download zip file: {e}[/bold red]") |
| #84 | anonymous_telemetry.capture(event_name="ec_create_app", properties={"success": False}) |
| #85 | return |
| #86 | |
| #87 | # Step 2: Extract the zip file |
| #88 | try: |
| #89 | with zipfile.ZipFile(zip_file_path, "r") as zip_ref: |
| #90 | # Get the name of the root directory inside the zip file |
| #91 | root_dir = Path(zip_ref.namelist()[0]) |
| #92 | for member in zip_ref.infolist(): |
| #93 | # Build the path to extract the file to, skipping the root directory |
| #94 | target_file = Path(member.filename).relative_to(root_dir) |
| #95 | source_file = zip_ref.open(member, "r") |
| #96 | if member.is_dir(): |
| #97 | # Create directory if it doesn't exist |
| #98 | os.makedirs(target_file, exist_ok=True) |
| #99 | else: |
| #100 | with open(target_file, "wb") as file: |
| #101 | # Write the file |
| #102 | shutil.copyfileobj(source_file, file) |
| #103 | console.print("✅ [bold green]Extracted zip file successfully.[/bold green]") |
| #104 | anonymous_telemetry.capture(event_name="ec_create_app", properties={"success": True}) |
| #105 | except zipfile.BadZipFile: |
| #106 | console.print("❌ [bold red]Error in extracting zip file. The file might be corrupted.[/bold red]") |
| #107 | anonymous_telemetry.capture(event_name="ec_create_app", properties={"success": False}) |
| #108 | return |
| #109 | |
| #110 | if docker: |
| #111 | subprocess.run(["docker-compose", "build"], check=True) |
| #112 | else: |
| #113 | ctx.invoke(install_reqs) |
| #114 | |
| #115 | |
| #116 | @cli.command() |
| #117 | def install_reqs(): |
| #118 | try: |
| #119 | console.print("Installing python requirements...\n") |
| #120 | time.sleep(2) |
| #121 | os.chdir("api") |
| #122 | subprocess.run(["pip", "install", "-r", "requirements.txt"], check=True) |
| #123 | os.chdir("..") |
| #124 | console.print("\n ✅ [bold green]Installed API requirements successfully.[/bold green]\n") |
| #125 | except Exception as e: |
| #126 | console.print(f"❌ [bold red]Failed to install API requirements: {e}[/bold red]") |
| #127 | anonymous_telemetry.capture(event_name="ec_install_reqs", properties={"success": False}) |
| #128 | return |
| #129 | |
| #130 | try: |
| #131 | os.chdir("ui") |
| #132 | subprocess.run(["yarn"], check=True) |
| #133 | console.print("\n✅ [bold green]Successfully installed frontend requirements.[/bold green]") |
| #134 | anonymous_telemetry.capture(event_name="ec_install_reqs", properties={"success": True}) |
| #135 | except Exception as e: |
| #136 | console.print(f"❌ [bold red]Failed to install frontend requirements. Error: {e}[/bold red]") |
| #137 | anonymous_telemetry.capture(event_name="ec_install_reqs", properties={"success": False}) |
| #138 | |
| #139 | |
| #140 | @cli.command() |
| #141 | @click.option("--docker", is_flag=True, help="Run inside docker.") |
| #142 | def start(docker): |
| #143 | if docker: |
| #144 | subprocess.run(["docker-compose", "up"], check=True) |
| #145 | return |
| #146 | |
| #147 | # Set up signal handling |
| #148 | signal.signal(signal.SIGINT, signal_handler) |
| #149 | signal.signal(signal.SIGTERM, signal_handler) |
| #150 | |
| #151 | # Step 1: Start the API server |
| #152 | try: |
| #153 | os.chdir("api") |
| #154 | api_process = subprocess.Popen(["python", "-m", "main"], stdout=None, stderr=None) |
| #155 | os.chdir("..") |
| #156 | console.print("✅ [bold green]API server started successfully.[/bold green]") |
| #157 | except Exception as e: |
| #158 | console.print(f"❌ [bold red]Failed to start the API server: {e}[/bold red]") |
| #159 | anonymous_telemetry.capture(event_name="ec_start", properties={"success": False}) |
| #160 | return |
| #161 | |
| #162 | # Sleep for 2 seconds to give the user time to read the message |
| #163 | time.sleep(2) |
| #164 | |
| #165 | # Step 2: Install UI requirements and start the UI server |
| #166 | try: |
| #167 | os.chdir("ui") |
| #168 | subprocess.run(["yarn"], check=True) |
| #169 | ui_process = subprocess.Popen(["yarn", "dev"]) |
| #170 | console.print("✅ [bold green]UI server started successfully.[/bold green]") |
| #171 | anonymous_telemetry.capture(event_name="ec_start", properties={"success": True}) |
| #172 | except Exception as e: |
| #173 | console.print(f"❌ [bold red]Failed to start the UI server: {e}[/bold red]") |
| #174 | anonymous_telemetry.capture(event_name="ec_start", properties={"success": False}) |
| #175 | |
| #176 | # Keep the script running until it receives a kill signal |
| #177 | try: |
| #178 | api_process.wait() |
| #179 | ui_process.wait() |
| #180 | except KeyboardInterrupt: |
| #181 | console.print("\n🛑 [bold yellow]Stopping server...[/bold yellow]") |
| #182 | |
| #183 | |
| #184 | @cli.command() |
| #185 | @click.option("--template", default="fly.io", help="The template to use.") |
| #186 | @click.argument("extra_args", nargs=-1, type=click.UNPROCESSED) |
| #187 | def create(template, extra_args): |
| #188 | anonymous_telemetry.capture(event_name="ec_create", properties={"template_used": template}) |
| #189 | template_dir = template |
| #190 | if "/" in template_dir: |
| #191 | template_dir = template.split("/")[1] |
| #192 | src_path = get_pkg_path_from_name(template_dir) |
| #193 | shutil.copytree(src_path, os.getcwd(), dirs_exist_ok=True) |
| #194 | console.print(f"✅ [bold green]Successfully created app from template '{template}'.[/bold green]") |
| #195 | |
| #196 | if template == "fly.io": |
| #197 | setup_fly_io_app(extra_args) |
| #198 | elif template == "modal.com": |
| #199 | setup_modal_com_app(extra_args) |
| #200 | elif template == "render.com": |
| #201 | setup_render_com_app() |
| #202 | elif template == "streamlit.io": |
| #203 | setup_streamlit_io_app() |
| #204 | elif template == "gradio.app": |
| #205 | setup_gradio_app() |
| #206 | elif template == "hf/gradio.app" or template == "hf/streamlit.io": |
| #207 | setup_hf_app() |
| #208 | else: |
| #209 | raise ValueError(f"Unknown template '{template}'.") |
| #210 | |
| #211 | embedchain_config = {"provider": template} |
| #212 | with open("embedchain.json", "w") as file: |
| #213 | json.dump(embedchain_config, file, indent=4) |
| #214 | console.print( |
| #215 | f"🎉 [green]All done! Successfully created `embedchain.json` with '{template}' as provider.[/green]" |
| #216 | ) |
| #217 | |
| #218 | |
| #219 | def run_dev_fly_io(debug, host, port): |
| #220 | uvicorn_command = ["uvicorn", "app:app"] |
| #221 | |
| #222 | if debug: |
| #223 | uvicorn_command.append("--reload") |
| #224 | |
| #225 | uvicorn_command.extend(["--host", host, "--port", str(port)]) |
| #226 | |
| #227 | try: |
| #228 | console.print(f"🚀 [bold cyan]Running FastAPI app with command: {' '.join(uvicorn_command)}[/bold cyan]") |
| #229 | subprocess.run(uvicorn_command, check=True) |
| #230 | except subprocess.CalledProcessError as e: |
| #231 | console.print(f"❌ [bold red]An error occurred: {e}[/bold red]") |
| #232 | except KeyboardInterrupt: |
| #233 | console.print("\n🛑 [bold yellow]FastAPI server stopped[/bold yellow]") |
| #234 | |
| #235 | |
| #236 | def run_dev_modal_com(): |
| #237 | modal_run_cmd = ["modal", "serve", "app"] |
| #238 | try: |
| #239 | console.print(f"🚀 [bold cyan]Running FastAPI app with command: {' '.join(modal_run_cmd)}[/bold cyan]") |
| #240 | subprocess.run(modal_run_cmd, check=True) |
| #241 | except subprocess.CalledProcessError as e: |
| #242 | console.print(f"❌ [bold red]An error occurred: {e}[/bold red]") |
| #243 | except KeyboardInterrupt: |
| #244 | console.print("\n🛑 [bold yellow]FastAPI server stopped[/bold yellow]") |
| #245 | |
| #246 | |
| #247 | def run_dev_streamlit_io(): |
| #248 | streamlit_run_cmd = ["streamlit", "run", "app.py"] |
| #249 | try: |
| #250 | console.print(f"🚀 [bold cyan]Running Streamlit app with command: {' '.join(streamlit_run_cmd)}[/bold cyan]") |
| #251 | subprocess.run(streamlit_run_cmd, check=True) |
| #252 | except subprocess.CalledProcessError as e: |
| #253 | console.print(f"❌ [bold red]An error occurred: {e}[/bold red]") |
| #254 | except KeyboardInterrupt: |
| #255 | console.print("\n🛑 [bold yellow]Streamlit server stopped[/bold yellow]") |
| #256 | |
| #257 | |
| #258 | def run_dev_render_com(debug, host, port): |
| #259 | uvicorn_command = ["uvicorn", "app:app"] |
| #260 | |
| #261 | if debug: |
| #262 | uvicorn_command.append("--reload") |
| #263 | |
| #264 | uvicorn_command.extend(["--host", host, "--port", str(port)]) |
| #265 | |
| #266 | try: |
| #267 | console.print(f"🚀 [bold cyan]Running FastAPI app with command: {' '.join(uvicorn_command)}[/bold cyan]") |
| #268 | subprocess.run(uvicorn_command, check=True) |
| #269 | except subprocess.CalledProcessError as e: |
| #270 | console.print(f"❌ [bold red]An error occurred: {e}[/bold red]") |
| #271 | except KeyboardInterrupt: |
| #272 | console.print("\n🛑 [bold yellow]FastAPI server stopped[/bold yellow]") |
| #273 | |
| #274 | |
| #275 | def run_dev_gradio(): |
| #276 | gradio_run_cmd = ["gradio", "app.py"] |
| #277 | try: |
| #278 | console.print(f"🚀 [bold cyan]Running Gradio app with command: {' '.join(gradio_run_cmd)}[/bold cyan]") |
| #279 | subprocess.run(gradio_run_cmd, check=True) |
| #280 | except subprocess.CalledProcessError as e: |
| #281 | console.print(f"❌ [bold red]An error occurred: {e}[/bold red]") |
| #282 | except KeyboardInterrupt: |
| #283 | console.print("\n🛑 [bold yellow]Gradio server stopped[/bold yellow]") |
| #284 | |
| #285 | |
| #286 | @cli.command() |
| #287 | @click.option("--debug", is_flag=True, help="Enable or disable debug mode.") |
| #288 | @click.option("--host", default="127.0.0.1", help="The host address to run the FastAPI app on.") |
| #289 | @click.option("--port", default=8000, help="The port to run the FastAPI app on.") |
| #290 | def dev(debug, host, port): |
| #291 | template = "" |
| #292 | with open("embedchain.json", "r") as file: |
| #293 | embedchain_config = json.load(file) |
| #294 | template = embedchain_config["provider"] |
| #295 | |
| #296 | anonymous_telemetry.capture(event_name="ec_dev", properties={"template_used": template}) |
| #297 | if template == "fly.io": |
| #298 | run_dev_fly_io(debug, host, port) |
| #299 | elif template == "modal.com": |
| #300 | run_dev_modal_com() |
| #301 | elif template == "render.com": |
| #302 | run_dev_render_com(debug, host, port) |
| #303 | elif template == "streamlit.io" or template == "hf/streamlit.io": |
| #304 | run_dev_streamlit_io() |
| #305 | elif template == "gradio.app" or template == "hf/gradio.app": |
| #306 | run_dev_gradio() |
| #307 | else: |
| #308 | raise ValueError(f"Unknown template '{template}'.") |
| #309 | |
| #310 | |
| #311 | @cli.command() |
| #312 | def deploy(): |
| #313 | # Check for platform-specific files |
| #314 | template = "" |
| #315 | ec_app_name = "" |
| #316 | with open("embedchain.json", "r") as file: |
| #317 | embedchain_config = json.load(file) |
| #318 | ec_app_name = embedchain_config["name"] if "name" in embedchain_config else None |
| #319 | template = embedchain_config["provider"] |
| #320 | |
| #321 | anonymous_telemetry.capture(event_name="ec_deploy", properties={"template_used": template}) |
| #322 | if template == "fly.io": |
| #323 | deploy_fly() |
| #324 | elif template == "modal.com": |
| #325 | deploy_modal() |
| #326 | elif template == "render.com": |
| #327 | deploy_render() |
| #328 | elif template == "streamlit.io": |
| #329 | deploy_streamlit() |
| #330 | elif template == "gradio.app": |
| #331 | deploy_gradio_app() |
| #332 | elif template.startswith("hf/"): |
| #333 | deploy_hf_spaces(ec_app_name) |
| #334 | else: |
| #335 | console.print("❌ [bold red]No recognized deployment platform found.[/bold red]") |
| #336 |