Spaces:
Runtime error

Ali20267's picture
Update app.py
3a145b3 verified
Raw History Blame Contribute Delete
2.32 kB
import os
import sys
import shutil
import subprocess
from pathlib import Path
APP_DIR = Path("/app")
REPO_DIR = APP_DIR / "MoneyPrinterTurbo"
REPO_URL = (
"https://github.com/harry0703/"
"MoneyPrinterTurbo.git"
)
PORT = int(os.environ.get("PORT", "7860"))
def run(command, cwd=None):
print(
"Running:",
" ".join(map(str, command)),
flush=True
)
subprocess.run(
command,
cwd=cwd,
check=True
)
def main():
# 1. Clone the official repository
if not (REPO_DIR / ".git").exists():
if REPO_DIR.exists():
shutil.rmtree(REPO_DIR)
run([
"git",
"clone",
"--depth", "1",
REPO_URL,
str(REPO_DIR)
])
# 2. Install Python dependencies
requirements = REPO_DIR / "requirements.txt"
if requirements.exists():
run([
sys.executable,
"-m", "pip",
"install",
"--no-cache-dir",
"--root-user-action=ignore",
"-r", str(requirements)
])
# 3. Prepare the API configuration
example_config = REPO_DIR / "config.example.toml"
config_file = REPO_DIR / "config.toml"
if not config_file.exists():
if not example_config.exists():
raise FileNotFoundError(
"config.example.toml is missing"
)
shutil.copyfile(
example_config,
config_file
)
# Set API host and port in config.toml
import re
config_text = config_file.read_text(
encoding="utf-8"
)
config_text = re.sub(
r"(?m)^listen_host\s*=.*$",
'listen_host = "0.0.0.0"',
config_text,
count=1
)
config_text = re.sub(
r"(?m)^listen_port\s*=.*$",
f"listen_port = {PORT}",
config_text,
count=1
)
config_file.write_text(
config_text,
encoding="utf-8"
)
# 4. Launch the official FastAPI service
print(
f"Starting MoneyPrinterTurbo API "
f"on 0.0.0.0:{PORT}",
flush=True
)
os.chdir(REPO_DIR)
os.execv(
sys.executable,
[
sys.executable,
"main.py"
]
)
if __name__ == "__main__":
main()