DaCrow13 commited on
Commit
834ca3f
·
1 Parent(s): f2c9d1c

feat: Add `start_space.sh` to automate DVC setup, model pulling, and launch FastAPI and Streamlit services.

Browse files
Files changed (1) hide show
  1. scripts/start_space.sh +27 -19
scripts/start_space.sh CHANGED
@@ -16,55 +16,63 @@ USER=${DAGSHUB_USERNAME:-$MLFLOW_TRACKING_USERNAME}
16
  PASS=${DAGSHUB_TOKEN:-$MLFLOW_TRACKING_PASSWORD}
17
 
18
  if [ -n "$USER" ] && [ -n "$PASS" ]; then
19
- echo "Configuring DVC authentication for DagsHub..."
20
  # Configure local config (not committed)
21
  dvc remote modify origin --local auth basic
22
  dvc remote modify origin --local user "$USER"
23
  dvc remote modify origin --local password "$PASS"
24
  else
25
- echo "WARNING: No DagsHub credentials found. DVC pull might fail if the remote is private."
26
  fi
27
 
28
- echo "Pulling models from DVC..."
29
  # Pull only the necessary files for inference
30
  dvc pull models/random_forest_tfidf_gridsearch.pkl.dvc \
31
  models/tfidf_vectorizer.pkl.dvc \
32
- models/label_names.pkl.dvc
33
 
34
  # Create Nginx temp directories
35
  mkdir -p /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp
36
 
37
- echo "Checking models existence..."
38
  ls -la models/
39
 
40
- echo "Starting FastAPI application in background..."
41
- uvicorn hopcroft_skill_classification_tool_competition.main:app --host 127.0.0.1 --port 8000 >> /tmp/fastapi.log 2>&1 &
 
42
 
43
  # Wait for API to start
44
- echo "Waiting for API to start..."
45
- sleep 15
 
 
 
 
 
 
 
46
 
47
- echo "Starting Nginx reverse proxy..."
48
- # Check if nginx is in path
49
  if ! command -v nginx &> /dev/null; then
50
- echo "ERROR: nginx not found in PATH"
51
  exit 1
52
  fi
53
  nginx -c /app/nginx.conf -g "daemon off;" >> /tmp/nginx_startup.log 2>&1 &
54
 
55
- echo "Waiting for Nginx to initialize..."
56
  sleep 5
57
 
58
  # Check if Nginx is running
59
  if ps aux | grep -v grep | grep -q "nginx"; then
60
- echo "Nginx is running."
61
  else
62
- echo "ERROR: Nginx failed to start. Logs:"
63
  cat /tmp/nginx_startup.log
64
- # Try to start it without the config to see if it's a binary issue
65
- # nginx -v
66
  fi
67
 
68
- echo "Starting Streamlit application..."
 
 
 
69
  export API_BASE_URL="http://127.0.0.1:8000"
70
- streamlit run hopcroft_skill_classification_tool_competition/streamlit_app.py --server.port 8501 --server.address 127.0.0.1
 
16
  PASS=${DAGSHUB_TOKEN:-$MLFLOW_TRACKING_PASSWORD}
17
 
18
  if [ -n "$USER" ] && [ -n "$PASS" ]; then
19
+ echo "$(date) - Configuring DVC authentication for DagsHub..."
20
  # Configure local config (not committed)
21
  dvc remote modify origin --local auth basic
22
  dvc remote modify origin --local user "$USER"
23
  dvc remote modify origin --local password "$PASS"
24
  else
25
+ echo "$(date) - WARNING: No DagsHub credentials found. DVC pull might fail if the remote is private."
26
  fi
27
 
28
+ echo "$(date) - Pulling models from DVC..."
29
  # Pull only the necessary files for inference
30
  dvc pull models/random_forest_tfidf_gridsearch.pkl.dvc \
31
  models/tfidf_vectorizer.pkl.dvc \
32
+ models/label_names.pkl.dvc || echo "DVC pull failed, but continuing..."
33
 
34
  # Create Nginx temp directories
35
  mkdir -p /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp
36
 
37
+ echo "$(date) - Checking models existence..."
38
  ls -la models/
39
 
40
+ echo "$(date) - Starting FastAPI application in background..."
41
+ # Using 0.0.0.0 to be safe
42
+ uvicorn hopcroft_skill_classification_tool_competition.main:app --host 0.0.0.0 --port 8000 >> /tmp/fastapi.log 2>&1 &
43
 
44
  # Wait for API to start
45
+ echo "$(date) - Waiting for API to start (30s)..."
46
+ for i in {1..30}; do
47
+ if curl -s http://127.0.0.1:8000/health > /dev/null; then
48
+ echo "$(date) - API is UP!"
49
+ break
50
+ fi
51
+ echo "$(date) - Waiting... ($i/30)"
52
+ sleep 2
53
+ done
54
 
55
+ echo "$(date) - Starting Nginx reverse proxy..."
 
56
  if ! command -v nginx &> /dev/null; then
57
+ echo "$(date) - ERROR: nginx not found in PATH"
58
  exit 1
59
  fi
60
  nginx -c /app/nginx.conf -g "daemon off;" >> /tmp/nginx_startup.log 2>&1 &
61
 
62
+ echo "$(date) - Waiting for Nginx to initialize..."
63
  sleep 5
64
 
65
  # Check if Nginx is running
66
  if ps aux | grep -v grep | grep -q "nginx"; then
67
+ echo "$(date) - Nginx is running."
68
  else
69
+ echo "$(date) - ERROR: Nginx failed to start. Logs:"
70
  cat /tmp/nginx_startup.log
 
 
71
  fi
72
 
73
+ echo "$(date) - Final backend check before starting Streamlit..."
74
+ curl -v http://127.0.0.1:8000/health || echo "FastAPI health check failed!"
75
+
76
+ echo "$(date) - Starting Streamlit application on 0.0.0.0:8501..."
77
  export API_BASE_URL="http://127.0.0.1:8000"
78
+ streamlit run hopcroft_skill_classification_tool_competition/streamlit_app.py --server.port 8501 --server.address 0.0.0.0