File size: 2,757 Bytes
353ecaa
 
 
 
 
 
 
 
 
 
4c58b1a
 
 
 
353ecaa
 
 
 
4c58b1a
353ecaa
 
 
 
 
 
 
 
 
4c58b1a
353ecaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c58b1a
 
 
 
 
 
 
 
 
 
 
 
 
353ecaa
4c58b1a
 
 
 
 
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
"""app.py — entrypoint used by Hugging Face Spaces

Creates a writable files directory for Chainlit and starts the Chainlit app.
This file is robust for both Docker and non-Docker runtimes:
- If CHAINLIT_FILES_DIR is set (e.g. by the Dockerfile), it will use that.
- Otherwise it tries /home/user/app/.files (the Docker image's path created at build-time).
- If neither is writable it will fall back to /tmp/mini_transformer_files.

It then runs the chainlit CLI against the packaged `mini_transformer.apps.chainlit_app`.
"""

import os
import subprocess
import sys
from pathlib import Path

# Prefer an externally provided writable directory
files_dir = os.environ.get("CHAINLIT_FILES_DIR")

if files_dir:
    files_path = Path(files_dir)
else:
    # prefer repo-level .files under /home/user/app (created by Dockerfile), else fallback to /tmp
    candidate = Path("/home/user/app/.files")
    if candidate.exists() and os.access(candidate, os.W_OK):
        files_path = candidate
    else:
        files_path = Path(os.environ.get("TMPDIR", "/tmp")) / "mini_transformer_files"

# Ensure directory exists and is writable
try:
    files_path.mkdir(parents=True, exist_ok=True)
    # Try to set permissive access for the runtime user
    try:
        files_path.chmod(0o700)
    except Exception:
        pass
except Exception as exc:  # pragma: no cover - permissive in container
    print(f"Failed to create files dir {files_path}: {exc}", file=sys.stderr)
    raise

# Export helpful env vars for Chainlit and other libs
os.environ.setdefault("CHAINLIT_FILES_DIR", str(files_path))
os.environ.setdefault("TMPDIR", str(files_path))
os.environ.setdefault("XDG_CACHE_HOME", str(files_path / "cache"))
os.environ.setdefault("XDG_STATE_HOME", str(files_path / "state"))

port = os.environ.get("PORT", os.environ.get("MINI_TRANSFORMER_UI_PORT", "7860"))
host = os.environ.get("HOST", "0.0.0.0")

# Resolve path to packaged chainlit app. If the source files aren't present, rely on installed package.
app_path = Path(__file__).parent / "src" / "mini_transformer" / "apps" / "chainlit_app.py"
if not app_path.exists():
    try:
        import mini_transformer.apps.chainlit_app as packed

        app_path = Path(packed.__file__)
    except Exception:
        print("Could not locate mini_transformer.apps.chainlit_app. Make sure package files are present.", file=sys.stderr)
        raise SystemExit(1)

cmd = ["chainlit", "run", str(app_path), "--host", host, "--port", str(port)]
print("Starting Chainlit with:\n", " ".join(cmd))

# Run Chainlit as foreground process
try:
    subprocess.run(cmd, check=True)
except FileNotFoundError:
    print("'chainlit' CLI not found. Ensure 'chainlit' is in requirements.txt and installed.", file=sys.stderr)
    raise