Spaces:
Runtime error
Runtime error
da03
commited on
Commit
·
ac4c384
1
Parent(s):
76ab7a4
- Dockerfile +102 -10
- static/index.html +16 -84
Dockerfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# Use the official Python 3.9 image
|
2 |
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
|
3 |
|
4 |
-
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl -y
|
5 |
|
6 |
# Set the working directory to /code
|
7 |
WORKDIR /code
|
@@ -15,6 +15,11 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
15 |
|
16 |
# Set up a new user named "user" with user ID 1000
|
17 |
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
|
|
|
|
18 |
# Switch to the "user" user
|
19 |
USER user
|
20 |
# Set home to the user's home directory
|
@@ -27,16 +32,72 @@ WORKDIR $HOME/app
|
|
27 |
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
28 |
COPY --chown=user . $HOME/app
|
29 |
|
30 |
-
# Create
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
32 |
echo 'set -e' >> start_hf_spaces.sh && \
|
33 |
echo '' >> start_hf_spaces.sh && \
|
34 |
-
echo 'echo "🚀 Starting Neural OS for HF Spaces with
|
35 |
echo 'echo "===================================="' >> start_hf_spaces.sh && \
|
36 |
echo '' >> start_hf_spaces.sh && \
|
37 |
-
echo '# Start
|
38 |
-
echo 'echo "
|
39 |
-
echo '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
echo 'DISPATCHER_PID=$!' >> start_hf_spaces.sh && \
|
41 |
echo 'echo "📊 Dispatcher PID: $DISPATCHER_PID"' >> start_hf_spaces.sh && \
|
42 |
echo '' >> start_hf_spaces.sh && \
|
@@ -44,9 +105,19 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
|
44 |
echo 'echo "⏳ Waiting for dispatcher to initialize..."' >> start_hf_spaces.sh && \
|
45 |
echo 'sleep 5' >> start_hf_spaces.sh && \
|
46 |
echo '' >> start_hf_spaces.sh && \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
echo '# Start worker' >> start_hf_spaces.sh && \
|
48 |
echo 'echo "🔧 Starting worker..."' >> start_hf_spaces.sh && \
|
49 |
-
echo 'python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:
|
50 |
echo 'WORKER_PID=$!' >> start_hf_spaces.sh && \
|
51 |
echo 'echo "📊 Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
|
52 |
echo '' >> start_hf_spaces.sh && \
|
@@ -54,11 +125,32 @@ RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
|
54 |
echo 'echo "⏳ Waiting for worker to initialize..."' >> start_hf_spaces.sh && \
|
55 |
echo 'sleep 30' >> start_hf_spaces.sh && \
|
56 |
echo '' >> start_hf_spaces.sh && \
|
57 |
-
echo '
|
58 |
-
echo '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
echo '' >> start_hf_spaces.sh && \
|
60 |
echo '# Keep running' >> start_hf_spaces.sh && \
|
61 |
-
echo '
|
|
|
62 |
echo 'wait $DISPATCHER_PID' >> start_hf_spaces.sh && \
|
63 |
chmod +x start_hf_spaces.sh
|
64 |
|
|
|
1 |
# Use the official Python 3.9 image
|
2 |
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
|
3 |
|
4 |
+
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl nginx -y
|
5 |
|
6 |
# Set the working directory to /code
|
7 |
WORKDIR /code
|
|
|
15 |
|
16 |
# Set up a new user named "user" with user ID 1000
|
17 |
RUN useradd -m -u 1000 user
|
18 |
+
|
19 |
+
# Create directories for nginx that user can write to
|
20 |
+
RUN mkdir -p /home/user/nginx/logs /home/user/nginx/cache /home/user/nginx/tmp && \
|
21 |
+
chown -R user:user /home/user/nginx
|
22 |
+
|
23 |
# Switch to the "user" user
|
24 |
USER user
|
25 |
# Set home to the user's home directory
|
|
|
32 |
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
33 |
COPY --chown=user . $HOME/app
|
34 |
|
35 |
+
# Create custom nginx.conf for non-root operation
|
36 |
+
RUN echo 'pid /home/user/nginx/nginx.pid;' > /home/user/nginx.conf && \
|
37 |
+
echo 'error_log /home/user/nginx/logs/error.log;' >> /home/user/nginx.conf && \
|
38 |
+
echo 'worker_processes 1;' >> /home/user/nginx.conf && \
|
39 |
+
echo '' >> /home/user/nginx.conf && \
|
40 |
+
echo 'events {' >> /home/user/nginx.conf && \
|
41 |
+
echo ' worker_connections 1024;' >> /home/user/nginx.conf && \
|
42 |
+
echo '}' >> /home/user/nginx.conf && \
|
43 |
+
echo '' >> /home/user/nginx.conf && \
|
44 |
+
echo 'http {' >> /home/user/nginx.conf && \
|
45 |
+
echo ' access_log /home/user/nginx/logs/access.log;' >> /home/user/nginx.conf && \
|
46 |
+
echo ' client_body_temp_path /home/user/nginx/tmp/client_body;' >> /home/user/nginx.conf && \
|
47 |
+
echo ' proxy_temp_path /home/user/nginx/tmp/proxy;' >> /home/user/nginx.conf && \
|
48 |
+
echo ' fastcgi_temp_path /home/user/nginx/tmp/fastcgi;' >> /home/user/nginx.conf && \
|
49 |
+
echo ' uwsgi_temp_path /home/user/nginx/tmp/uwsgi;' >> /home/user/nginx.conf && \
|
50 |
+
echo ' scgi_temp_path /home/user/nginx/tmp/scgi;' >> /home/user/nginx.conf && \
|
51 |
+
echo '' >> /home/user/nginx.conf && \
|
52 |
+
echo ' server {' >> /home/user/nginx.conf && \
|
53 |
+
echo ' listen 7860;' >> /home/user/nginx.conf && \
|
54 |
+
echo ' server_name localhost;' >> /home/user/nginx.conf && \
|
55 |
+
echo '' >> /home/user/nginx.conf && \
|
56 |
+
echo ' # WebSocket support' >> /home/user/nginx.conf && \
|
57 |
+
echo ' location /ws {' >> /home/user/nginx.conf && \
|
58 |
+
echo ' proxy_pass http://localhost:8080/ws;' >> /home/user/nginx.conf && \
|
59 |
+
echo ' proxy_http_version 1.1;' >> /home/user/nginx.conf && \
|
60 |
+
echo ' proxy_set_header Upgrade $http_upgrade;' >> /home/user/nginx.conf && \
|
61 |
+
echo ' proxy_set_header Connection "upgrade";' >> /home/user/nginx.conf && \
|
62 |
+
echo ' proxy_set_header Host $host;' >> /home/user/nginx.conf && \
|
63 |
+
echo ' proxy_set_header X-Real-IP $remote_addr;' >> /home/user/nginx.conf && \
|
64 |
+
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /home/user/nginx.conf && \
|
65 |
+
echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /home/user/nginx.conf && \
|
66 |
+
echo ' proxy_read_timeout 86400;' >> /home/user/nginx.conf && \
|
67 |
+
echo ' proxy_send_timeout 86400;' >> /home/user/nginx.conf && \
|
68 |
+
echo ' }' >> /home/user/nginx.conf && \
|
69 |
+
echo '' >> /home/user/nginx.conf && \
|
70 |
+
echo ' # Regular HTTP requests' >> /home/user/nginx.conf && \
|
71 |
+
echo ' location / {' >> /home/user/nginx.conf && \
|
72 |
+
echo ' proxy_pass http://localhost:8080;' >> /home/user/nginx.conf && \
|
73 |
+
echo ' proxy_set_header Host $host;' >> /home/user/nginx.conf && \
|
74 |
+
echo ' proxy_set_header X-Real-IP $remote_addr;' >> /home/user/nginx.conf && \
|
75 |
+
echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /home/user/nginx.conf && \
|
76 |
+
echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /home/user/nginx.conf && \
|
77 |
+
echo ' }' >> /home/user/nginx.conf && \
|
78 |
+
echo ' }' >> /home/user/nginx.conf && \
|
79 |
+
echo '}' >> /home/user/nginx.conf
|
80 |
+
|
81 |
+
# Create necessary temp directories
|
82 |
+
RUN mkdir -p /home/user/nginx/tmp/{client_body,proxy,fastcgi,uwsgi,scgi}
|
83 |
+
|
84 |
+
# Create startup script
|
85 |
RUN echo '#!/bin/bash' > start_hf_spaces.sh && \
|
86 |
echo 'set -e' >> start_hf_spaces.sh && \
|
87 |
echo '' >> start_hf_spaces.sh && \
|
88 |
+
echo 'echo "🚀 Starting Neural OS for HF Spaces with nginx proxy"' >> start_hf_spaces.sh && \
|
89 |
echo 'echo "===================================="' >> start_hf_spaces.sh && \
|
90 |
echo '' >> start_hf_spaces.sh && \
|
91 |
+
echo '# Start nginx as user' >> start_hf_spaces.sh && \
|
92 |
+
echo 'echo "🌐 Starting nginx proxy..."' >> start_hf_spaces.sh && \
|
93 |
+
echo 'nginx -c /home/user/nginx.conf -t' >> start_hf_spaces.sh && \
|
94 |
+
echo 'nginx -c /home/user/nginx.conf &' >> start_hf_spaces.sh && \
|
95 |
+
echo 'NGINX_PID=$!' >> start_hf_spaces.sh && \
|
96 |
+
echo 'echo "📊 Nginx PID: $NGINX_PID"' >> start_hf_spaces.sh && \
|
97 |
+
echo '' >> start_hf_spaces.sh && \
|
98 |
+
echo '# Start dispatcher' >> 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 && \
|
|
|
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 everything is working' >> start_hf_spaces.sh && \
|
109 |
+
echo 'echo "🔍 Testing system health..."' >> 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 "✅ System is responding"' >> start_hf_spaces.sh && \
|
113 |
+
echo 'else' >> start_hf_spaces.sh && \
|
114 |
+
echo ' echo "❌ System health check failed"' >> start_hf_spaces.sh && \
|
115 |
+
echo ' exit 1' >> start_hf_spaces.sh && \
|
116 |
+
echo 'fi' >> start_hf_spaces.sh && \
|
117 |
+
echo '' >> start_hf_spaces.sh && \
|
118 |
echo '# Start worker' >> start_hf_spaces.sh && \
|
119 |
echo 'echo "🔧 Starting worker..."' >> start_hf_spaces.sh && \
|
120 |
+
echo 'python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:8080 > worker.log 2>&1 &' >> start_hf_spaces.sh && \
|
121 |
echo 'WORKER_PID=$!' >> start_hf_spaces.sh && \
|
122 |
echo 'echo "📊 Worker PID: $WORKER_PID"' >> start_hf_spaces.sh && \
|
123 |
echo '' >> start_hf_spaces.sh && \
|
|
|
125 |
echo 'echo "⏳ Waiting for worker to initialize..."' >> start_hf_spaces.sh && \
|
126 |
echo 'sleep 30' >> start_hf_spaces.sh && \
|
127 |
echo '' >> start_hf_spaces.sh && \
|
128 |
+
echo '# Final health check' >> start_hf_spaces.sh && \
|
129 |
+
echo 'if kill -0 $DISPATCHER_PID 2>/dev/null && kill -0 $WORKER_PID 2>/dev/null; then' >> start_hf_spaces.sh && \
|
130 |
+
echo ' echo "✅ System ready!"' >> start_hf_spaces.sh && \
|
131 |
+
echo ' echo "🌍 Web interface: http://localhost:7860"' >> start_hf_spaces.sh && \
|
132 |
+
echo ' echo "📊 Dispatcher: http://localhost:8080"' >> start_hf_spaces.sh && \
|
133 |
+
echo 'else' >> start_hf_spaces.sh && \
|
134 |
+
echo ' echo "❌ System failed to start properly"' >> start_hf_spaces.sh && \
|
135 |
+
echo ' echo "📋 Dispatcher log:"' >> start_hf_spaces.sh && \
|
136 |
+
echo ' tail -n 20 dispatcher.log' >> start_hf_spaces.sh && \
|
137 |
+
echo ' echo "📋 Worker log:"' >> start_hf_spaces.sh && \
|
138 |
+
echo ' tail -n 20 worker.log' >> start_hf_spaces.sh && \
|
139 |
+
echo ' exit 1' >> start_hf_spaces.sh && \
|
140 |
+
echo 'fi' >> start_hf_spaces.sh && \
|
141 |
+
echo '' >> start_hf_spaces.sh && \
|
142 |
+
echo '# Function to cleanup' >> start_hf_spaces.sh && \
|
143 |
+
echo 'cleanup() {' >> start_hf_spaces.sh && \
|
144 |
+
echo ' echo "🛑 Shutting down..."' >> start_hf_spaces.sh && \
|
145 |
+
echo ' kill $NGINX_PID $DISPATCHER_PID $WORKER_PID 2>/dev/null || true' >> start_hf_spaces.sh && \
|
146 |
+
echo ' exit 0' >> start_hf_spaces.sh && \
|
147 |
+
echo '}' >> start_hf_spaces.sh && \
|
148 |
+
echo '' >> start_hf_spaces.sh && \
|
149 |
+
echo 'trap cleanup SIGINT SIGTERM' >> start_hf_spaces.sh && \
|
150 |
echo '' >> start_hf_spaces.sh && \
|
151 |
echo '# Keep running' >> start_hf_spaces.sh && \
|
152 |
+
echo 'echo "📋 Following logs (Ctrl+C to stop):"' >> start_hf_spaces.sh && \
|
153 |
+
echo 'tail -f dispatcher.log worker.log /home/user/nginx/logs/error.log &' >> start_hf_spaces.sh && \
|
154 |
echo 'wait $DISPATCHER_PID' >> start_hf_spaces.sh && \
|
155 |
chmod +x start_hf_spaces.sh
|
156 |
|
static/index.html
CHANGED
@@ -188,8 +188,22 @@
|
|
188 |
function connect() {
|
189 |
console.log("connect() function called - creating new WebSocket connection");
|
190 |
//const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
socket.onopen = function(event) {
|
195 |
console.log("WebSocket connection established");
|
@@ -291,9 +305,6 @@
|
|
291 |
console.log(`Queue update: Position ${data.position}/${data.total_waiting}, wait: ${data.maximum_wait_seconds.toFixed(1)} seconds`);
|
292 |
const waitSeconds = Math.ceil(data.maximum_wait_seconds);
|
293 |
|
294 |
-
// Disable canvas interaction while in queue
|
295 |
-
disableCanvasInteraction();
|
296 |
-
|
297 |
if (waitSeconds === 0) {
|
298 |
showConnectionStatus("Starting soon...");
|
299 |
stopQueueCountdown();
|
@@ -317,8 +328,6 @@
|
|
317 |
console.log("Session started, clearing queue display");
|
318 |
// Stop queue countdown and clear the display
|
319 |
stopQueueCountdown();
|
320 |
-
// Enable canvas interaction when session starts
|
321 |
-
enableCanvasInteraction();
|
322 |
//ctx.clearRect(0, 0, canvas.width, canvas.height);
|
323 |
} else if (data.type === "session_warning") {
|
324 |
console.log(`Session time warning: ${data.time_remaining} seconds remaining`);
|
@@ -397,10 +406,6 @@
|
|
397 |
let autoInputEnabled = true; // Default to enabled
|
398 |
let userHasInteracted = false; // Track if user has moved mouse inside canvas
|
399 |
|
400 |
-
// Session state tracking
|
401 |
-
let sessionState = 'queued'; // 'queued', 'active', 'disconnected'
|
402 |
-
let canvasInteractionEnabled = false;
|
403 |
-
|
404 |
// Timeout countdown mechanism - support concurrent timeouts
|
405 |
let timeoutCountdownInterval = null;
|
406 |
let timeoutCountdown = 10;
|
@@ -720,44 +725,6 @@
|
|
720 |
queueCountdownActive = false;
|
721 |
queueWaitTime = 0;
|
722 |
}
|
723 |
-
|
724 |
-
function enableCanvasInteraction() {
|
725 |
-
canvasInteractionEnabled = true;
|
726 |
-
sessionState = 'active';
|
727 |
-
|
728 |
-
// Remove visual queue indicator
|
729 |
-
if (canvas) {
|
730 |
-
canvas.style.opacity = '1';
|
731 |
-
canvas.style.cursor = 'crosshair';
|
732 |
-
canvas.style.pointerEvents = 'auto';
|
733 |
-
}
|
734 |
-
|
735 |
-
// Update status
|
736 |
-
const statusElement = document.getElementById('connectionStatus');
|
737 |
-
if (statusElement) {
|
738 |
-
statusElement.textContent = 'Active';
|
739 |
-
statusElement.className = 'connected';
|
740 |
-
}
|
741 |
-
}
|
742 |
-
|
743 |
-
function disableCanvasInteraction() {
|
744 |
-
canvasInteractionEnabled = false;
|
745 |
-
sessionState = 'queued';
|
746 |
-
|
747 |
-
// Add visual queue indicator
|
748 |
-
if (canvas) {
|
749 |
-
canvas.style.opacity = '0.5';
|
750 |
-
canvas.style.cursor = 'not-allowed';
|
751 |
-
canvas.style.pointerEvents = 'none';
|
752 |
-
}
|
753 |
-
|
754 |
-
// Update status
|
755 |
-
const statusElement = document.getElementById('connectionStatus');
|
756 |
-
if (statusElement) {
|
757 |
-
statusElement.textContent = 'Queued';
|
758 |
-
statusElement.className = 'connecting';
|
759 |
-
}
|
760 |
-
}
|
761 |
|
762 |
function updateQueueCountdownDisplay() {
|
763 |
if (queueWaitTime <= 0) {
|
@@ -863,13 +830,6 @@
|
|
863 |
}
|
864 |
|
865 |
if (!isConnected || isProcessing) return;
|
866 |
-
|
867 |
-
// Check if canvas interaction is enabled (not queued)
|
868 |
-
if (!canvasInteractionEnabled) {
|
869 |
-
console.log("Canvas interaction disabled - user is queued");
|
870 |
-
return;
|
871 |
-
}
|
872 |
-
|
873 |
let rect = canvas.getBoundingClientRect();
|
874 |
let x = event.clientX - rect.left;
|
875 |
let y = event.clientY - rect.top;
|
@@ -887,13 +847,6 @@
|
|
887 |
|
888 |
canvas.addEventListener("click", function (event) {
|
889 |
if (!isConnected || isProcessing) return;
|
890 |
-
|
891 |
-
// Check if canvas interaction is enabled (not queued)
|
892 |
-
if (!canvasInteractionEnabled) {
|
893 |
-
console.log("Canvas interaction disabled - user is queued");
|
894 |
-
return;
|
895 |
-
}
|
896 |
-
|
897 |
let rect = canvas.getBoundingClientRect();
|
898 |
let x = event.clientX - rect.left;
|
899 |
let y = event.clientY - rect.top;
|
@@ -906,12 +859,6 @@
|
|
906 |
event.preventDefault(); // Prevent default context menu
|
907 |
if (!isConnected || isProcessing) return;
|
908 |
|
909 |
-
// Check if canvas interaction is enabled (not queued)
|
910 |
-
if (!canvasInteractionEnabled) {
|
911 |
-
console.log("Canvas interaction disabled - user is queued");
|
912 |
-
return;
|
913 |
-
}
|
914 |
-
|
915 |
let rect = canvas.getBoundingClientRect();
|
916 |
let x = event.clientX - rect.left;
|
917 |
let y = event.clientY - rect.top;
|
@@ -975,12 +922,6 @@
|
|
975 |
}
|
976 |
if (!isConnected || isProcessing || !userHasInteracted) return;
|
977 |
|
978 |
-
// Check if canvas interaction is enabled (not queued)
|
979 |
-
if (!canvasInteractionEnabled) {
|
980 |
-
console.log("Canvas interaction disabled - user is queued");
|
981 |
-
return;
|
982 |
-
}
|
983 |
-
|
984 |
// Get the current mouse position
|
985 |
let rect = canvas.getBoundingClientRect();
|
986 |
let x = lastSentPosition ? lastSentPosition.x : canvas.width / 2;
|
@@ -997,12 +938,6 @@
|
|
997 |
}
|
998 |
if (!isConnected || socket.readyState !== WebSocket.OPEN || !userHasInteracted) return;
|
999 |
|
1000 |
-
// Check if canvas interaction is enabled (not queued)
|
1001 |
-
if (!canvasInteractionEnabled) {
|
1002 |
-
console.log("Canvas interaction disabled - user is queued");
|
1003 |
-
return;
|
1004 |
-
}
|
1005 |
-
|
1006 |
// Get the current mouse position
|
1007 |
let rect = canvas.getBoundingClientRect();
|
1008 |
let x = lastSentPosition ? lastSentPosition.x : canvas.width / 2;
|
@@ -1110,9 +1045,6 @@
|
|
1110 |
}
|
1111 |
}
|
1112 |
});
|
1113 |
-
|
1114 |
-
// Initialize canvas in disabled state (user starts queued)
|
1115 |
-
disableCanvasInteraction();
|
1116 |
</script>
|
1117 |
|
1118 |
<!-- Bootstrap JS (optional) -->
|
|
|
188 |
function connect() {
|
189 |
console.log("connect() function called - creating new WebSocket connection");
|
190 |
//const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
191 |
+
//socket = new WebSocket(`${protocol}//${window.location.host}/ws`);
|
192 |
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
193 |
+
|
194 |
+
// Fix for HF Spaces - construct correct domain from path
|
195 |
+
let host = window.location.host;
|
196 |
+
if (host === 'huggingface.co' && window.location.pathname.startsWith('/spaces/')) {
|
197 |
+
// Extract username and spacename from path like /spaces/username/spacename
|
198 |
+
const pathParts = window.location.pathname.split('/');
|
199 |
+
if (pathParts.length >= 4) {
|
200 |
+
const username = pathParts[2];
|
201 |
+
const spacename = pathParts[3];
|
202 |
+
host = `${username}-${spacename}.hf.space`;
|
203 |
+
}
|
204 |
+
}
|
205 |
+
|
206 |
+
socket = new WebSocket(`${protocol}//${host}/ws`);
|
207 |
|
208 |
socket.onopen = function(event) {
|
209 |
console.log("WebSocket connection established");
|
|
|
305 |
console.log(`Queue update: Position ${data.position}/${data.total_waiting}, wait: ${data.maximum_wait_seconds.toFixed(1)} seconds`);
|
306 |
const waitSeconds = Math.ceil(data.maximum_wait_seconds);
|
307 |
|
|
|
|
|
|
|
308 |
if (waitSeconds === 0) {
|
309 |
showConnectionStatus("Starting soon...");
|
310 |
stopQueueCountdown();
|
|
|
328 |
console.log("Session started, clearing queue display");
|
329 |
// Stop queue countdown and clear the display
|
330 |
stopQueueCountdown();
|
|
|
|
|
331 |
//ctx.clearRect(0, 0, canvas.width, canvas.height);
|
332 |
} else if (data.type === "session_warning") {
|
333 |
console.log(`Session time warning: ${data.time_remaining} seconds remaining`);
|
|
|
406 |
let autoInputEnabled = true; // Default to enabled
|
407 |
let userHasInteracted = false; // Track if user has moved mouse inside canvas
|
408 |
|
|
|
|
|
|
|
|
|
409 |
// Timeout countdown mechanism - support concurrent timeouts
|
410 |
let timeoutCountdownInterval = null;
|
411 |
let timeoutCountdown = 10;
|
|
|
725 |
queueCountdownActive = false;
|
726 |
queueWaitTime = 0;
|
727 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
728 |
|
729 |
function updateQueueCountdownDisplay() {
|
730 |
if (queueWaitTime <= 0) {
|
|
|
830 |
}
|
831 |
|
832 |
if (!isConnected || isProcessing) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
833 |
let rect = canvas.getBoundingClientRect();
|
834 |
let x = event.clientX - rect.left;
|
835 |
let y = event.clientY - rect.top;
|
|
|
847 |
|
848 |
canvas.addEventListener("click", function (event) {
|
849 |
if (!isConnected || isProcessing) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
850 |
let rect = canvas.getBoundingClientRect();
|
851 |
let x = event.clientX - rect.left;
|
852 |
let y = event.clientY - rect.top;
|
|
|
859 |
event.preventDefault(); // Prevent default context menu
|
860 |
if (!isConnected || isProcessing) return;
|
861 |
|
|
|
|
|
|
|
|
|
|
|
|
|
862 |
let rect = canvas.getBoundingClientRect();
|
863 |
let x = event.clientX - rect.left;
|
864 |
let y = event.clientY - rect.top;
|
|
|
922 |
}
|
923 |
if (!isConnected || isProcessing || !userHasInteracted) return;
|
924 |
|
|
|
|
|
|
|
|
|
|
|
|
|
925 |
// Get the current mouse position
|
926 |
let rect = canvas.getBoundingClientRect();
|
927 |
let x = lastSentPosition ? lastSentPosition.x : canvas.width / 2;
|
|
|
938 |
}
|
939 |
if (!isConnected || socket.readyState !== WebSocket.OPEN || !userHasInteracted) return;
|
940 |
|
|
|
|
|
|
|
|
|
|
|
|
|
941 |
// Get the current mouse position
|
942 |
let rect = canvas.getBoundingClientRect();
|
943 |
let x = lastSentPosition ? lastSentPosition.x : canvas.width / 2;
|
|
|
1045 |
}
|
1046 |
}
|
1047 |
});
|
|
|
|
|
|
|
1048 |
</script>
|
1049 |
|
1050 |
<!-- Bootstrap JS (optional) -->
|