File size: 2,389 Bytes
cbaaf3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check the real entry point on the port used by managed Gradio Spaces."""

import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import time
from urllib.error import URLError
from urllib.request import urlopen


def main() -> None:
    root = Path(__file__).resolve().parents[1]
    env = os.environ.copy()
    env.update(
        SPACE_ID="local/startup-check",
        GRADIO_SERVER_PORT="8000",
        GOOGLE_API_KEY="",
        GRADIO_ANALYTICS_ENABLED="False",
        PYTHONUNBUFFERED="1",
    )
    # An empty API key keeps this check independent of Gemini and private PDFs.
    with tempfile.TemporaryFile(mode="w+") as log:
        process = subprocess.Popen(
            [sys.executable, "app.py"], cwd=root, env=env,
            stdout=log, stderr=subprocess.STDOUT,
        )
        try:
            deadline = time.monotonic() + 60
            while time.monotonic() < deadline:
                if process.poll() is not None:
                    raise RuntimeError(f"App exited with code {process.returncode}")
                log.seek(0)
                started = "Running on local URL:" in log.read()
                if started:
                    try:
                        with urlopen("http://127.0.0.1:7860/", timeout=2) as response:
                            assert response.status == 200
                            assert b"gradio" in response.read().lower()
                        with urlopen("http://127.0.0.1:7860/config", timeout=2) as response:
                            config = json.load(response)
                            assert any(c["type"] == "chatbot" for c in config["components"])
                        print("PASS: app.py serves the UI and chatbot config on port 7860 without an API key.")
                        return
                    except (URLError, TimeoutError):
                        pass
                time.sleep(0.5)
            raise RuntimeError("App did not become healthy on port 7860 within 60 seconds")
        except Exception:
            log.seek(0)
            print(log.read(), file=sys.stderr)
            raise
        finally:
            process.terminate()
            try:
                process.wait(timeout=5)
            except subprocess.TimeoutExpired:
                process.kill()
                process.wait()


if __name__ == "__main__":
    main()