udbhav commited on
Commit
7844438
Β·
1 Parent(s): d8472b3

added trame reqs

Browse files
Files changed (2) hide show
  1. Dockerfile +8 -0
  2. app.py +78 -33
Dockerfile CHANGED
@@ -14,6 +14,10 @@ ENV OMP_NUM_THREADS=4
14
  # Environment variables for off-screen rendering
15
  ENV PYVISTA_OFF_SCREEN=true
16
  ENV VTK_DEFAULT_RENDER_WINDOW_OFFSCREEN=1
 
 
 
 
17
 
18
  # System deps + Deadsnakes PPA for Python 3.12
19
  RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -76,6 +80,10 @@ RUN chown -R 1000:1000 /app
76
  # Expose Gradio port
77
  EXPOSE 7860
78
 
 
 
 
 
79
  # Run app
80
  CMD ["python", "app.py"]
81
 
 
14
  # Environment variables for off-screen rendering
15
  ENV PYVISTA_OFF_SCREEN=true
16
  ENV VTK_DEFAULT_RENDER_WINDOW_OFFSCREEN=1
17
+ ENV DISPLAY=:99
18
+ ENV LIBGL_ALWAYS_SOFTWARE=1
19
+ ENV MESA_LOADER_DRIVER_OVERRIDE=llvmpipe
20
+ ENV MESA_GL_VERSION_OVERRIDE=3.3
21
 
22
  # System deps + Deadsnakes PPA for Python 3.12
23
  RUN apt-get update && apt-get install -y --no-install-recommends \
 
80
  # Expose Gradio port
81
  EXPOSE 7860
82
 
83
+ # Set HOST and PORT via environment variables
84
+ ENV HOST=0.0.0.0
85
+ ENV PORT=7860
86
+
87
  # Run app
88
  CMD ["python", "app.py"]
89
 
app.py CHANGED
@@ -1,10 +1,21 @@
1
 
2
-
3
  import os
 
 
 
 
 
 
 
 
 
 
4
  import sys
5
  import tempfile
6
  from pathlib import Path
7
  import traceback
 
8
 
9
 
10
  # Writable base dir
@@ -81,41 +92,75 @@ def load_private_app(repo_path):
81
  raise RuntimeError(f"Failed to load app from private repo: {e}")
82
 
83
  def main():
84
- hf_token = os.environ.get("HF_TOKEN")
85
- repo_id = os.environ.get("REPO_ID")
 
 
 
 
 
 
 
 
 
86
 
87
- if not hf_token or not repo_id:
88
- raise ValueError("HF_TOKEN and REPO_ID must be set")
89
 
90
- print(f"πŸš€ Starting app with repo_id: {repo_id}")
91
- print(f"πŸ“‚ Current working directory: {os.getcwd()}")
92
-
93
- cache_dir = setup_environment()
94
- repo_path = download_private_repo(cache_dir, repo_id, hf_token)
95
- demo = load_private_app(repo_path)
96
 
97
- # Check if it's a Trame app (has server.start method) or Gradio app (has launch method)
98
- if hasattr(demo, 'server') and hasattr(demo.server, 'start'):
99
- # Trame app
100
- port = int(os.environ.get("PORT", "7860"))
101
- demo.server.start(
102
- port=port,
103
- open_browser=False,
104
- show_connection_info=True,
105
- backend="aiohttp",
106
- exec_mode="main",
107
- )
108
- elif hasattr(demo, 'launch'):
109
- # Gradio app
110
- demo.launch(
111
- server_name="0.0.0.0",
112
- server_port=7860,
113
- share=False,
114
- show_error=True,
115
- allowed_paths=[str(repo_path), DATA_DIR]
116
- )
117
- else:
118
- raise RuntimeError("App does not have launch() or server.start() method")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
 
121
  if __name__ == "__main__":
 
1
 
2
+ # ========= Headless / Offscreen safety (before any VTK import) =========
3
  import os
4
+ # Set headless rendering environment variables BEFORE any VTK/pyvista imports
5
+ os.environ.setdefault("VTK_DEFAULT_RENDER_WINDOW_OFFSCREEN", "1")
6
+ os.environ.setdefault("LIBGL_ALWAYS_SOFTWARE", "1")
7
+ os.environ.setdefault("MESA_LOADER_DRIVER_OVERRIDE", "llvmpipe")
8
+ os.environ.setdefault("MESA_GL_VERSION_OVERRIDE", "3.3")
9
+ # Use dummy DISPLAY value instead of empty string to avoid X server connection attempts
10
+ if "DISPLAY" not in os.environ or os.environ.get("DISPLAY") == "":
11
+ os.environ["DISPLAY"] = ":99"
12
+ os.environ.setdefault("PYVISTA_OFF_SCREEN", "True")
13
+
14
  import sys
15
  import tempfile
16
  from pathlib import Path
17
  import traceback
18
+ import signal
19
 
20
 
21
  # Writable base dir
 
92
  raise RuntimeError(f"Failed to load app from private repo: {e}")
93
 
94
  def main():
95
+ # Setup signal handlers for graceful shutdown
96
+ def signal_handler(sig, frame):
97
+ print("\nπŸ›‘ Received shutdown signal, cleaning up...")
98
+ sys.exit(0)
99
+
100
+ signal.signal(signal.SIGINT, signal_handler)
101
+ signal.signal(signal.SIGTERM, signal_handler)
102
+
103
+ try:
104
+ hf_token = os.environ.get("HF_TOKEN")
105
+ repo_id = os.environ.get("REPO_ID")
106
 
107
+ if not hf_token or not repo_id:
108
+ raise ValueError("HF_TOKEN and REPO_ID must be set")
109
 
110
+ print(f"πŸš€ Starting app with repo_id: {repo_id}")
111
+ print(f"πŸ“‚ Current working directory: {os.getcwd()}")
112
+
113
+ cache_dir = setup_environment()
114
+ repo_path = download_private_repo(cache_dir, repo_id, hf_token)
115
+ demo = load_private_app(repo_path)
116
 
117
+ # Check if it's a Trame app (has server.start method) or Gradio app (has launch method)
118
+ if hasattr(demo, 'server') and hasattr(demo.server, 'start'):
119
+ # Trame app
120
+ port = int(os.environ.get("PORT", "7860"))
121
+ host = os.environ.get("HOST", "0.0.0.0") # Allow host to be configured via env var
122
+
123
+ print(f"πŸš€ Starting Trame server on {host}:{port}")
124
+
125
+ # Configure server to bind to the specified host
126
+ try:
127
+ # Try with host parameter first (if supported)
128
+ demo.server.start(
129
+ host=host,
130
+ port=port,
131
+ open_browser=False,
132
+ show_connection_info=True,
133
+ backend="aiohttp",
134
+ exec_mode="main",
135
+ )
136
+ except TypeError:
137
+ # If host parameter is not supported, configure via server options
138
+ # Most Trame versions bind to 0.0.0.0 by default with aiohttp
139
+ demo.server.start(
140
+ port=port,
141
+ open_browser=False,
142
+ show_connection_info=True,
143
+ backend="aiohttp",
144
+ exec_mode="main",
145
+ )
146
+ elif hasattr(demo, 'launch'):
147
+ # Gradio app
148
+ demo.launch(
149
+ server_name="0.0.0.0",
150
+ server_port=int(os.environ.get("PORT", "7860")),
151
+ share=False,
152
+ show_error=True,
153
+ allowed_paths=[str(repo_path), DATA_DIR]
154
+ )
155
+ else:
156
+ raise RuntimeError("App does not have launch() or server.start() method")
157
+ except KeyboardInterrupt:
158
+ print("\nπŸ›‘ Keyboard interrupt received, shutting down...")
159
+ sys.exit(0)
160
+ except Exception as e:
161
+ print(f"❌ Fatal error: {e}")
162
+ traceback.print_exc()
163
+ sys.exit(1)
164
 
165
 
166
  if __name__ == "__main__":