AlaBoussoffara commited on
Commit
353ecaa
·
1 Parent(s): 8ce5a19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -11
app.py CHANGED
@@ -1,18 +1,56 @@
1
- # app.py — wrapper used by Hugging Face Spaces to start the Chainlit UI
2
- # This simple script locates the packaged chainlit app and invokes the chainlit CLI.
 
 
 
 
 
 
 
 
3
 
4
  import os
5
  import subprocess
6
- from pathlib import Path
7
  import sys
 
 
 
 
8
 
9
- # Optional: set a default model name here or use Space env vars to override
10
- # os.environ.setdefault("MINI_TRANSFORMER_MODEL_NAME", "small_model_v1")
 
 
 
 
 
 
 
11
 
12
- # Resolve path to packaged chainlit app inside the repository
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  app_path = Path(__file__).parent / "src" / "mini_transformer" / "apps" / "chainlit_app.py"
14
  if not app_path.exists():
15
- # If package is installed into env, try to find installed module
16
  try:
17
  import mini_transformer.apps.chainlit_app as packed
18
 
@@ -21,13 +59,10 @@ if not app_path.exists():
21
  print("Could not locate mini_transformer.apps.chainlit_app. Make sure package files are present.", file=sys.stderr)
22
  raise SystemExit(1)
23
 
24
- port = os.environ.get("PORT", os.environ.get("MINI_TRANSFORMER_UI_PORT", "7860"))
25
- host = os.environ.get("HOST", "0.0.0.0")
26
-
27
  cmd = ["chainlit", "run", str(app_path), "--host", host, "--port", str(port)]
28
  print("Starting Chainlit with:\n", " ".join(cmd))
29
 
30
- # Use subprocess.run so Chainlit becomes the foreground process
31
  try:
32
  subprocess.run(cmd, check=True)
33
  except FileNotFoundError:
 
1
+ """app.py — entrypoint used by Hugging Face Spaces
2
+
3
+ Creates a writable files directory for Chainlit and starts the Chainlit app.
4
+ This file is robust for both Docker and non-Docker runtimes:
5
+ - If CHAINLIT_FILES_DIR is set (e.g. by the Dockerfile), it will use that.
6
+ - Otherwise it tries /home/user/app/.files (the Docker image's path created at build-time).
7
+ - If neither is writable it will fall back to /tmp/mini_transformer_files.
8
+
9
+ It then runs the chainlit CLI against the packaged `mini_transformer.apps.chainlit_app`.
10
+ """
11
 
12
  import os
13
  import subprocess
 
14
  import sys
15
+ from pathlib import Path
16
+
17
+ # Prefer an externally provided writable directory
18
+ files_dir = os.environ.get("CHAINLIT_FILES_DIR")
19
 
20
+ if files_dir:
21
+ files_path = Path(files_dir)
22
+ else:
23
+ # prefer repo-level .files under /home/user/app (created by Dockerfile), else fallback to /tmp
24
+ candidate = Path("/home/user/app/.files")
25
+ if candidate.exists() and os.access(candidate, os.W_OK):
26
+ files_path = candidate
27
+ else:
28
+ files_path = Path(os.environ.get("TMPDIR", "/tmp")) / "mini_transformer_files"
29
 
30
+ # Ensure directory exists and is writable
31
+ try:
32
+ files_path.mkdir(parents=True, exist_ok=True)
33
+ # Try to set permissive access for the runtime user
34
+ try:
35
+ files_path.chmod(0o700)
36
+ except Exception:
37
+ pass
38
+ except Exception as exc: # pragma: no cover - permissive in container
39
+ print(f"Failed to create files dir {files_path}: {exc}", file=sys.stderr)
40
+ raise
41
+
42
+ # Export helpful env vars for Chainlit and other libs
43
+ os.environ.setdefault("CHAINLIT_FILES_DIR", str(files_path))
44
+ os.environ.setdefault("TMPDIR", str(files_path))
45
+ os.environ.setdefault("XDG_CACHE_HOME", str(files_path / "cache"))
46
+ os.environ.setdefault("XDG_STATE_HOME", str(files_path / "state"))
47
+
48
+ port = os.environ.get("PORT", os.environ.get("MINI_TRANSFORMER_UI_PORT", "7860"))
49
+ host = os.environ.get("HOST", "0.0.0.0")
50
+
51
+ # Resolve path to packaged chainlit app. If the source files aren't present, rely on installed package.
52
  app_path = Path(__file__).parent / "src" / "mini_transformer" / "apps" / "chainlit_app.py"
53
  if not app_path.exists():
 
54
  try:
55
  import mini_transformer.apps.chainlit_app as packed
56
 
 
59
  print("Could not locate mini_transformer.apps.chainlit_app. Make sure package files are present.", file=sys.stderr)
60
  raise SystemExit(1)
61
 
 
 
 
62
  cmd = ["chainlit", "run", str(app_path), "--host", host, "--port", str(port)]
63
  print("Starting Chainlit with:\n", " ".join(cmd))
64
 
65
+ # Run Chainlit as foreground process
66
  try:
67
  subprocess.run(cmd, check=True)
68
  except FileNotFoundError: