Spaces:
Runtime error

File size: 2,323 Bytes
f8ac996
 
3a145b3
 
9eff41e
f8ac996
3a145b3
 
 
9eff41e
 
 
 
 
3a145b3
9eff41e
 
3a145b3
 
 
 
 
 
9eff41e
 
 
 
 
 
 
 
 
3a145b3
9eff41e
 
 
 
3a145b3
9eff41e
 
3a145b3
9eff41e
 
 
 
3a145b3
9eff41e
 
 
3a145b3
9eff41e
3a145b3
9eff41e
 
 
3a145b3
9eff41e
 
3a145b3
 
 
9eff41e
3a145b3
 
 
 
 
9eff41e
3a145b3
 
 
9eff41e
 
3a145b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eff41e
3a145b3
 
 
 
 
9eff41e
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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()