da03 commited on
Commit
7c66a8f
Β·
1 Parent(s): f2020b4
Files changed (1) hide show
  1. Dockerfile +62 -29
Dockerfile CHANGED
@@ -2,7 +2,7 @@
2
  FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
3
 
4
 
5
- RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl -y
6
 
7
  # Set the working directory to /code
8
  WORKDIR /code
@@ -30,11 +30,44 @@ WORKDIR $HOME/app
30
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
31
  COPY --chown=user . $HOME/app
32
 
33
- # Create a startup script for HF Spaces using echo commands
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
35
  echo 'set -e' >> start_hf_spaces.sh && \
36
  echo '' >> start_hf_spaces.sh && \
37
- echo 'echo "πŸš€ Starting Neural OS for HF Spaces"' >> start_hf_spaces.sh && \
38
  echo 'echo "===================================="' >> start_hf_spaces.sh && \
39
  echo 'echo "πŸ“ Current directory: $(pwd)"' >> start_hf_spaces.sh && \
40
  echo 'echo "πŸ“‹ Files in current directory:"' >> start_hf_spaces.sh && \
@@ -58,35 +91,32 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
58
  echo '' >> start_hf_spaces.sh && \
59
  echo 'echo "βœ… All required files found"' >> start_hf_spaces.sh && \
60
  echo '' >> start_hf_spaces.sh && \
61
- echo '# Start dispatcher in background' >> start_hf_spaces.sh && \
 
 
 
 
62
  echo 'echo "🎯 Starting dispatcher..."' >> start_hf_spaces.sh && \
63
- echo 'python dispatcher.py --port 7860 > dispatcher.log 2>&1 &' >> start_hf_spaces.sh && \
64
  echo 'DISPATCHER_PID=$!' >> start_hf_spaces.sh && \
65
  echo 'echo "πŸ“Š Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
66
  echo '' >> start_hf_spaces.sh && \
67
- echo '# Wait for dispatcher to start and check if it is running' >> start_hf_spaces.sh && \
68
  echo 'echo "⏳ Waiting for dispatcher to initialize..."' >> start_hf_spaces.sh && \
69
  echo 'sleep 5' >> start_hf_spaces.sh && \
70
  echo '' >> start_hf_spaces.sh && \
71
- echo 'if ! kill -0 $DISPATCHER_PID 2>/dev/null; then' >> start_hf_spaces.sh && \
72
- echo ' echo "❌ Dispatcher failed to start"' >> start_hf_spaces.sh && \
73
- echo ' echo "πŸ“‹ Dispatcher log:"' >> start_hf_spaces.sh && \
74
- echo ' cat dispatcher.log' >> start_hf_spaces.sh && \
75
- echo ' exit 1' >> start_hf_spaces.sh && \
76
- echo 'fi' >> start_hf_spaces.sh && \
77
- echo '' >> start_hf_spaces.sh && \
78
  echo '# Test if dispatcher is responding' >> start_hf_spaces.sh && \
79
- echo 'echo "πŸ” Testing dispatcher health..."' >> start_hf_spaces.sh && \
80
  echo 'curl -f http://localhost:7860/ > /dev/null 2>&1' >> start_hf_spaces.sh && \
81
  echo 'if [ $? -eq 0 ]; then' >> start_hf_spaces.sh && \
82
- echo ' echo "βœ… Dispatcher is responding to HTTP requests"' >> start_hf_spaces.sh && \
83
  echo 'else' >> start_hf_spaces.sh && \
84
- echo ' echo "⚠️ Dispatcher HTTP test failed, but continuing..."' >> start_hf_spaces.sh && \
85
  echo 'fi' >> start_hf_spaces.sh && \
86
  echo '' >> start_hf_spaces.sh && \
87
- echo '# Start single worker' >> start_hf_spaces.sh && \
88
  echo 'echo "πŸ”§ Starting worker..."' >> start_hf_spaces.sh && \
89
- echo 'python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:7860 > worker.log 2>&1 &' >> start_hf_spaces.sh && \
90
  echo 'WORKER_PID=$!' >> start_hf_spaces.sh && \
91
  echo 'echo "πŸ“Š Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
92
  echo '' >> start_hf_spaces.sh && \
@@ -94,18 +124,19 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
94
  echo 'echo "⏳ Waiting for worker to initialize..."' >> start_hf_spaces.sh && \
95
  echo 'sleep 30' >> start_hf_spaces.sh && \
96
  echo '' >> start_hf_spaces.sh && \
97
- echo '# Check if worker is still running' >> start_hf_spaces.sh && \
98
  echo 'if ! kill -0 $WORKER_PID 2>/dev/null; then' >> start_hf_spaces.sh && \
99
  echo ' echo "❌ Worker failed to start"' >> start_hf_spaces.sh && \
100
  echo ' echo "πŸ“‹ Worker log:"' >> start_hf_spaces.sh && \
101
- echo ' cat worker.log' >> start_hf_spaces.sh && \
102
  echo ' echo "πŸ“‹ Dispatcher log:"' >> start_hf_spaces.sh && \
103
- echo ' cat dispatcher.log' >> start_hf_spaces.sh && \
104
  echo ' exit 1' >> start_hf_spaces.sh && \
105
  echo 'fi' >> start_hf_spaces.sh && \
106
  echo '' >> start_hf_spaces.sh && \
107
  echo 'echo "βœ… System ready!"' >> start_hf_spaces.sh && \
108
- echo 'echo "🌍 Web interface: http://localhost:7860"' >> start_hf_spaces.sh && \
 
109
  echo 'echo "πŸ“Š Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
110
  echo 'echo "πŸ“Š Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
111
  echo '' >> start_hf_spaces.sh && \
@@ -113,21 +144,23 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
113
  echo 'cleanup() {' >> start_hf_spaces.sh && \
114
  echo ' echo "πŸ›‘ Shutting down..."' >> start_hf_spaces.sh && \
115
  echo ' kill $DISPATCHER_PID $WORKER_PID 2>/dev/null || true' >> start_hf_spaces.sh && \
 
116
  echo ' exit 0' >> start_hf_spaces.sh && \
117
  echo '}' >> start_hf_spaces.sh && \
118
  echo '' >> start_hf_spaces.sh && \
119
  echo 'trap cleanup SIGINT SIGTERM' >> start_hf_spaces.sh && \
120
  echo '' >> start_hf_spaces.sh && \
121
- echo '# Keep the script running by following the dispatcher log' >> start_hf_spaces.sh && \
122
- echo 'echo "πŸ“‹ Following dispatcher log (Ctrl+C to stop):"' >> start_hf_spaces.sh && \
123
- echo 'tail -f dispatcher.log &' >> start_hf_spaces.sh && \
124
  echo 'TAIL_PID=$!' >> start_hf_spaces.sh && \
125
- echo '' >> start_hf_spaces.sh && \
126
- echo '# Wait for dispatcher (main process)' >> start_hf_spaces.sh && \
127
  echo 'wait $DISPATCHER_PID' >> start_hf_spaces.sh && \
128
- echo '' >> start_hf_spaces.sh && \
129
- echo '# Clean up tail process' >> start_hf_spaces.sh && \
130
  echo 'kill $TAIL_PID 2>/dev/null || true' >> start_hf_spaces.sh && \
131
  chmod +x start_hf_spaces.sh
132
 
 
 
 
 
 
133
  CMD ["bash", "start_hf_spaces.sh"]
 
2
  FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
3
 
4
 
5
+ RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl nginx -y
6
 
7
  # Set the working directory to /code
8
  WORKDIR /code
 
30
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
31
  COPY --chown=user . $HOME/app
32
 
33
+ # Create nginx configuration for WebSocket support
34
+ USER root
35
+ RUN cat > /etc/nginx/sites-available/default << 'EOF'
36
+ server {
37
+ listen 7860;
38
+ server_name _;
39
+
40
+ # WebSocket support
41
+ location /ws {
42
+ proxy_pass http://localhost:8080/ws;
43
+ proxy_http_version 1.1;
44
+ proxy_set_header Upgrade $http_upgrade;
45
+ proxy_set_header Connection "upgrade";
46
+ proxy_set_header Host $host;
47
+ proxy_set_header X-Real-IP $remote_addr;
48
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
49
+ proxy_set_header X-Forwarded-Proto $scheme;
50
+ proxy_read_timeout 86400;
51
+ }
52
+
53
+ # Regular HTTP requests
54
+ location / {
55
+ proxy_pass http://localhost:8080;
56
+ proxy_set_header Host $host;
57
+ proxy_set_header X-Real-IP $remote_addr;
58
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
59
+ proxy_set_header X-Forwarded-Proto $scheme;
60
+ }
61
+ }
62
+ EOF
63
+
64
+ USER user
65
+
66
+ # Create a startup script for HF Spaces with nginx
67
  RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
68
  echo 'set -e' >> start_hf_spaces.sh && \
69
  echo '' >> start_hf_spaces.sh && \
70
+ echo 'echo "πŸš€ Starting Neural OS for HF Spaces with nginx proxy"' >> start_hf_spaces.sh && \
71
  echo 'echo "===================================="' >> start_hf_spaces.sh && \
72
  echo 'echo "πŸ“ Current directory: $(pwd)"' >> start_hf_spaces.sh && \
73
  echo 'echo "πŸ“‹ Files in current directory:"' >> start_hf_spaces.sh && \
 
91
  echo '' >> start_hf_spaces.sh && \
92
  echo 'echo "βœ… All required files found"' >> start_hf_spaces.sh && \
93
  echo '' >> start_hf_spaces.sh && \
94
+ echo '# Start nginx' >> start_hf_spaces.sh && \
95
+ echo 'echo "🌐 Starting nginx proxy..."' >> start_hf_spaces.sh && \
96
+ echo 'sudo nginx -t && sudo nginx' >> start_hf_spaces.sh && \
97
+ echo '' >> start_hf_spaces.sh && \
98
+ echo '# Start dispatcher on port 8080 (behind nginx)' >> start_hf_spaces.sh && \
99
  echo 'echo "🎯 Starting dispatcher..."' >> start_hf_spaces.sh && \
100
+ echo 'python dispatcher.py --port 8080 > dispatcher.log 2>&1 &' >> start_hf_spaces.sh && \
101
  echo 'DISPATCHER_PID=$!' >> start_hf_spaces.sh && \
102
  echo 'echo "πŸ“Š Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
103
  echo '' >> start_hf_spaces.sh && \
104
+ echo '# Wait for dispatcher to start' >> start_hf_spaces.sh && \
105
  echo 'echo "⏳ Waiting for dispatcher to initialize..."' >> start_hf_spaces.sh && \
106
  echo 'sleep 5' >> start_hf_spaces.sh && \
107
  echo '' >> start_hf_spaces.sh && \
 
 
 
 
 
 
 
108
  echo '# Test if dispatcher is responding' >> start_hf_spaces.sh && \
109
+ echo 'echo "πŸ” Testing dispatcher health (via nginx)..."' >> start_hf_spaces.sh && \
110
  echo 'curl -f http://localhost:7860/ > /dev/null 2>&1' >> start_hf_spaces.sh && \
111
  echo 'if [ $? -eq 0 ]; then' >> start_hf_spaces.sh && \
112
+ echo ' echo "βœ… Nginx proxy is working"' >> start_hf_spaces.sh && \
113
  echo 'else' >> start_hf_spaces.sh && \
114
+ echo ' echo "⚠️ Nginx proxy test failed"' >> start_hf_spaces.sh && \
115
  echo 'fi' >> start_hf_spaces.sh && \
116
  echo '' >> start_hf_spaces.sh && \
117
+ echo '# Start worker' >> start_hf_spaces.sh && \
118
  echo 'echo "πŸ”§ Starting worker..."' >> start_hf_spaces.sh && \
119
+ echo 'python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:8080 > worker.log 2>&1 &' >> start_hf_spaces.sh && \
120
  echo 'WORKER_PID=$!' >> start_hf_spaces.sh && \
121
  echo 'echo "πŸ“Š Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
122
  echo '' >> start_hf_spaces.sh && \
 
124
  echo 'echo "⏳ Waiting for worker to initialize..."' >> start_hf_spaces.sh && \
125
  echo 'sleep 30' >> start_hf_spaces.sh && \
126
  echo '' >> start_hf_spaces.sh && \
127
+ echo '# Check if worker is running' >> start_hf_spaces.sh && \
128
  echo 'if ! kill -0 $WORKER_PID 2>/dev/null; then' >> start_hf_spaces.sh && \
129
  echo ' echo "❌ Worker failed to start"' >> start_hf_spaces.sh && \
130
  echo ' echo "πŸ“‹ Worker log:"' >> start_hf_spaces.sh && \
131
+ echo ' tail -n 50 worker.log' >> start_hf_spaces.sh && \
132
  echo ' echo "πŸ“‹ Dispatcher log:"' >> start_hf_spaces.sh && \
133
+ echo ' tail -n 50 dispatcher.log' >> start_hf_spaces.sh && \
134
  echo ' exit 1' >> start_hf_spaces.sh && \
135
  echo 'fi' >> start_hf_spaces.sh && \
136
  echo '' >> start_hf_spaces.sh && \
137
  echo 'echo "βœ… System ready!"' >> start_hf_spaces.sh && \
138
+ echo 'echo "🌍 Web interface: http://localhost:7860 (via nginx)"' >> start_hf_spaces.sh && \
139
+ echo 'echo "🎯 Dispatcher: http://localhost:8080 (direct)"' >> start_hf_spaces.sh && \
140
  echo 'echo "πŸ“Š Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
141
  echo 'echo "πŸ“Š Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
142
  echo '' >> start_hf_spaces.sh && \
 
144
  echo 'cleanup() {' >> start_hf_spaces.sh && \
145
  echo ' echo "πŸ›‘ Shutting down..."' >> start_hf_spaces.sh && \
146
  echo ' kill $DISPATCHER_PID $WORKER_PID 2>/dev/null || true' >> start_hf_spaces.sh && \
147
+ echo ' sudo nginx -s quit 2>/dev/null || true' >> start_hf_spaces.sh && \
148
  echo ' exit 0' >> start_hf_spaces.sh && \
149
  echo '}' >> start_hf_spaces.sh && \
150
  echo '' >> start_hf_spaces.sh && \
151
  echo 'trap cleanup SIGINT SIGTERM' >> start_hf_spaces.sh && \
152
  echo '' >> start_hf_spaces.sh && \
153
+ echo '# Keep the script running' >> start_hf_spaces.sh && \
154
+ echo 'echo "πŸ“‹ Following logs (Ctrl+C to stop):"' >> start_hf_spaces.sh && \
155
+ echo 'tail -f dispatcher.log worker.log &' >> start_hf_spaces.sh && \
156
  echo 'TAIL_PID=$!' >> start_hf_spaces.sh && \
 
 
157
  echo 'wait $DISPATCHER_PID' >> start_hf_spaces.sh && \
 
 
158
  echo 'kill $TAIL_PID 2>/dev/null || true' >> start_hf_spaces.sh && \
159
  chmod +x start_hf_spaces.sh
160
 
161
+ # Give user sudo permissions for nginx
162
+ USER root
163
+ RUN echo "user ALL=(ALL) NOPASSWD: /usr/sbin/nginx, /usr/sbin/nginx -t, /usr/sbin/nginx -s quit" >> /etc/sudoers
164
+ USER user
165
+
166
  CMD ["bash", "start_hf_spaces.sh"]