Soumik Bose commited on
Commit
8775295
·
1 Parent(s): 2c48def
Files changed (2) hide show
  1. Dockerfile +22 -25
  2. controller.py +7 -3
Dockerfile CHANGED
@@ -1,46 +1,43 @@
1
  # Use the official Python 3.11 slim image
2
  FROM python:3.11-slim
3
 
4
- # Install curl for keep-alive script and clean up
 
5
  RUN apt-get update && \
6
- apt-get install -y curl && \
7
- rm -rf /var/lib/apt/lists/*
 
 
 
 
8
 
9
  # Set the working directory inside the container
10
  WORKDIR /app
11
 
12
- # Create all required directories with proper permissions upfront
13
- RUN mkdir -p /app/generated_outputs && \
14
- mkdir -p /app/generated_charts && \
15
- mkdir -p /app/cache && \
16
- chmod -R 777 /app/generated_outputs && \
17
- chmod -R 777 /app/generated_charts && \
18
- chmod -R 777 /app/cache
19
 
20
- # Create log files with proper permissions
21
- RUN touch /app/pandasai.log && \
22
- touch /app/api_key_rotation.log && \
23
- chmod 666 /app/pandasai.log && \
24
- chmod 666 /app/api_key_rotation.log
25
 
26
  # Set environment variables
27
  ENV MPLCONFIGDIR=/app/cache
28
 
29
- # Copy the requirements file first
30
  COPY requirements.txt .
31
-
32
- # Install dependencies
33
  RUN pip install --no-cache-dir -r requirements.txt
34
 
35
- # Copy the rest of the application code
36
  COPY . .
37
 
38
- # Ensure the user has write permissions to all directories
39
- RUN chown -R 1000:1000 /app && \
40
- chmod -R 777 /app
41
 
42
- # Expose port 7860 (required by Hugging Face Spaces)
43
  EXPOSE 7860
44
 
45
- # Keep-alive command (pings every 5 minutes) + start Uvicorn
46
- CMD bash -c "while true; do curl -s https://code-api-executor.hf.space/ping >/dev/null && sleep 300; done & uvicorn controller:app --host 0.0.0.0 --port 7860"
 
 
1
  # Use the official Python 3.11 slim image
2
  FROM python:3.11-slim
3
 
4
+ # Install system dependencies required for Math/Data libraries
5
+ # libgomp1 is CRITICAL for scikit-learn & numpy parallelization to prevent segfaults
6
  RUN apt-get update && \
7
+ apt-get install -y --no-install-recommends \
8
+ curl \
9
+ gcc \
10
+ g++ \
11
+ libgomp1 \
12
+ && rm -rf /var/lib/apt/lists/*
13
 
14
  # Set the working directory inside the container
15
  WORKDIR /app
16
 
17
+ # Create required directories with permissions
18
+ RUN mkdir -p /app/generated_outputs /app/generated_charts /app/cache && \
19
+ chmod -R 777 /app/generated_outputs /app/generated_charts /app/cache
 
 
 
 
20
 
21
+ # Create log files
22
+ RUN touch /app/pandasai.log /app/api_key_rotation.log && \
23
+ chmod 666 /app/pandasai.log /app/api_key_rotation.log
 
 
24
 
25
  # Set environment variables
26
  ENV MPLCONFIGDIR=/app/cache
27
 
28
+ # Copy requirements and install
29
  COPY requirements.txt .
 
 
30
  RUN pip install --no-cache-dir -r requirements.txt
31
 
32
+ # Copy application code
33
  COPY . .
34
 
35
+ # Ensure permissions
36
+ RUN chown -R 1000:1000 /app && chmod -R 777 /app
 
37
 
38
+ # Expose port
39
  EXPOSE 7860
40
 
41
+ # CMD: Run the python script directly.
42
+ # This ensures the 'if __name__ == "__main__":' block in your code actually runs.
43
+ CMD bash -c "while true; do curl -s https://code-api-executor.hf.space/ping >/dev/null && sleep 300; done & python controller.py"
controller.py CHANGED
@@ -25,6 +25,7 @@ from bson import ObjectId
25
  import mysql.connector
26
  import psycopg2
27
  from psycopg2.extras import RealDictCursor
 
28
 
29
  # --- Existing Services ---
30
  from csv_analysis_service import execute_analysis_logic
@@ -467,19 +468,22 @@ async def batch_execute_mongo(payload: BatchRequest[ExecutorPayload], token: str
467
  # ==============================================================================
468
 
469
  if __name__ == "__main__":
470
- import uvicorn
471
  host = os.getenv("HOST", "0.0.0.0")
472
  port = int(os.getenv("PORT", 7860))
473
 
474
- # Scale processes: 16 Cores - 2 = 14 Workers
 
475
  num_workers = max(1, multiprocessing.cpu_count() - 2)
476
 
477
  print(f"Starting production server on {host}:{port} with {num_workers} workers...")
 
478
 
479
  uvicorn.run(
480
  "controller:app",
481
  host=host,
482
  port=port,
483
  workers=num_workers,
484
- loop="auto",
 
 
485
  )
 
25
  import mysql.connector
26
  import psycopg2
27
  from psycopg2.extras import RealDictCursor
28
+ import uvicorn
29
 
30
  # --- Existing Services ---
31
  from csv_analysis_service import execute_analysis_logic
 
468
  # ==============================================================================
469
 
470
  if __name__ == "__main__":
 
471
  host = os.getenv("HOST", "0.0.0.0")
472
  port = int(os.getenv("PORT", 7860))
473
 
474
+ # Calculate workers (save 2 cores for system overhead)
475
+ # Ensure at least 1 worker exists
476
  num_workers = max(1, multiprocessing.cpu_count() - 2)
477
 
478
  print(f"Starting production server on {host}:{port} with {num_workers} workers...")
479
+ print("Using 'asyncio' loop to prevent Pandas/Numpy segfaults.")
480
 
481
  uvicorn.run(
482
  "controller:app",
483
  host=host,
484
  port=port,
485
  workers=num_workers,
486
+ loop="asyncio",
487
+ log_level="info",
488
+ access_log=False
489
  )