MikelWL commited on
Commit
beb189d
Β·
1 Parent(s): 3b85519

HF Space: Docker entrypoint + React hybrid fixes

Browse files
Files changed (3) hide show
  1. Dockerfile +18 -0
  2. frontend/react_gradio_hybrid.py +8 -5
  3. start_hf_space.sh +45 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ ENV PYTHONUNBUFFERED=1 \
4
+ PIP_NO_CACHE_DIR=1
5
+
6
+ WORKDIR /app
7
+
8
+ COPY requirements.txt /app/requirements.txt
9
+ RUN pip install --upgrade pip && pip install -r /app/requirements.txt
10
+
11
+ COPY . /app
12
+
13
+ RUN chmod +x /app/start_hf_space.sh
14
+
15
+ ENV API_HOST=0.0.0.0 \
16
+ API_PORT=8000
17
+
18
+ CMD ["/app/start_hf_space.sh"]
frontend/react_gradio_hybrid.py CHANGED
@@ -1,6 +1,7 @@
1
  import sys
2
  import json
3
  import time
 
4
  from pathlib import Path
5
  from typing import Dict, List
6
  import logging
@@ -88,7 +89,8 @@ async def serve_frontend():
88
 
89
  const connectWebSocket = () => {
90
  conversationIdRef.current = `react_conv_${Date.now()}`;
91
- const wsUrl = `ws://127.0.0.1:7860/ws/frontend/${conversationIdRef.current}`;
 
92
 
93
  const websocket = new WebSocket(wsUrl);
94
 
@@ -538,15 +540,16 @@ async def forward_backend_messages(conversation_id: str):
538
 
539
 
540
  if __name__ == "__main__":
 
541
  print("πŸš€ Starting AI Survey Simulator - React + WebSocket Manager")
542
  print(f"πŸ“‘ Backend URL: {settings.frontend.backend_base_url}")
543
- print(f"🌐 Frontend URL: http://127.0.0.1:7860")
544
  print(f"πŸ”§ Using WebSocketManager for backend connection")
545
  print("=" * 60)
546
 
547
  uvicorn.run(
548
  app,
549
- host="127.0.0.1",
550
- port=7860,
551
  log_level=settings.log_level.lower()
552
- )
 
1
  import sys
2
  import json
3
  import time
4
+ import os
5
  from pathlib import Path
6
  from typing import Dict, List
7
  import logging
 
89
 
90
  const connectWebSocket = () => {
91
  conversationIdRef.current = `react_conv_${Date.now()}`;
92
+ const wsScheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
93
+ const wsUrl = `${wsScheme}://${window.location.host}/ws/frontend/${conversationIdRef.current}`;
94
 
95
  const websocket = new WebSocket(wsUrl);
96
 
 
540
 
541
 
542
  if __name__ == "__main__":
543
+ port = int(os.environ.get("PORT", "7860"))
544
  print("πŸš€ Starting AI Survey Simulator - React + WebSocket Manager")
545
  print(f"πŸ“‘ Backend URL: {settings.frontend.backend_base_url}")
546
+ print(f"🌐 Frontend URL: http://127.0.0.1:{port}")
547
  print(f"πŸ”§ Using WebSocketManager for backend connection")
548
  print("=" * 60)
549
 
550
  uvicorn.run(
551
  app,
552
+ host="0.0.0.0",
553
+ port=port,
554
  log_level=settings.log_level.lower()
555
+ )
start_hf_space.sh ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ set -eu
3
+
4
+ ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
5
+
6
+ : "${API_HOST:=0.0.0.0}"
7
+ : "${API_PORT:=8000}"
8
+ : "${PORT:=7860}"
9
+ : "${LOG_LEVEL:=info}"
10
+
11
+ echo "Starting backend on ${API_HOST}:${API_PORT} ..."
12
+ (
13
+ cd "${ROOT_DIR}/backend"
14
+ uvicorn api.main:app --host "${API_HOST}" --port "${API_PORT}" --log-level "${LOG_LEVEL}"
15
+ ) &
16
+ BACKEND_PID=$!
17
+
18
+ echo "Starting frontend (React hybrid) on 0.0.0.0:${PORT} ..."
19
+ (
20
+ cd "${ROOT_DIR}"
21
+ PORT="${PORT}" python frontend/react_gradio_hybrid.py
22
+ ) &
23
+ FRONTEND_PID=$!
24
+
25
+ cleanup() {
26
+ echo "Shutting down..."
27
+ kill "${FRONTEND_PID}" 2>/dev/null || true
28
+ kill "${BACKEND_PID}" 2>/dev/null || true
29
+ }
30
+
31
+ trap cleanup EXIT SIGINT SIGTERM
32
+
33
+ (
34
+ wait "${BACKEND_PID}" 2>/dev/null || true
35
+ echo "Backend exited"
36
+ kill "${FRONTEND_PID}" 2>/dev/null || true
37
+ ) &
38
+
39
+ (
40
+ wait "${FRONTEND_PID}" 2>/dev/null || true
41
+ echo "Frontend exited"
42
+ kill "${BACKEND_PID}" 2>/dev/null || true
43
+ ) &
44
+
45
+ wait "${FRONTEND_PID}" "${BACKEND_PID}"