Devam0 commited on
Commit
aa57537
Β·
1 Parent(s): c0e2fd2

Fix OpenCV conflicts - use only headless version

Browse files
Files changed (4) hide show
  1. Dockerfile +23 -4
  2. Dockerfile.opencv_fix +46 -0
  3. fix_build.py +50 -29
  4. requirements.txt +14 -14
Dockerfile CHANGED
@@ -1,20 +1,39 @@
1
- # Base image
2
- FROM python:3.9-slim
3
 
4
- # Install minimal system dependencies
5
  RUN apt-get update && apt-get install -y \
6
  libglib2.0-0 \
7
  libsm6 \
8
  libxext6 \
9
  libxrender1 \
10
  libgomp1 \
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
  # Set working directory
14
  WORKDIR /app
15
 
16
- # Copy and install dependencies
17
  COPY requirements.txt .
 
 
 
 
 
 
18
  RUN pip install --no-cache-dir --upgrade pip && \
19
  pip install --no-cache-dir -r requirements.txt
20
 
 
1
+ # Base image - using bullseye for better compatibility
2
+ FROM python:3.9-bullseye
3
 
4
+ # Install system dependencies first
5
  RUN apt-get update && apt-get install -y \
6
  libglib2.0-0 \
7
  libsm6 \
8
  libxext6 \
9
  libxrender1 \
10
  libgomp1 \
11
+ libgl1-mesa-glx \
12
+ libgtk-3-0 \
13
+ libavcodec58 \
14
+ libavformat58 \
15
+ libswscale5 \
16
+ libv4l-0 \
17
+ libxvidcore4 \
18
+ libx264-160 \
19
+ libjpeg62-turbo \
20
+ libpng16-16 \
21
+ libtiff5 \
22
+ libatlas-base-dev \
23
+ gfortran \
24
  && rm -rf /var/lib/apt/lists/*
25
 
26
  # Set working directory
27
  WORKDIR /app
28
 
29
+ # Copy requirements and install Python packages
30
  COPY requirements.txt .
31
+
32
+ # Install OpenCV headless first and remove any conflicting packages
33
+ RUN pip install --no-cache-dir opencv-python-headless==4.8.1.78 && \
34
+ pip uninstall -y opencv-python opencv-contrib-python || true
35
+
36
+ # Install other dependencies
37
  RUN pip install --no-cache-dir --upgrade pip && \
38
  pip install --no-cache-dir -r requirements.txt
39
 
Dockerfile.opencv_fix ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Alternative Dockerfile with OpenCV compatibility fix
2
+ FROM python:3.9-bullseye
3
+
4
+ # Install system dependencies first
5
+ RUN apt-get update && apt-get install -y \
6
+ libglib2.0-0 \
7
+ libsm6 \
8
+ libxext6 \
9
+ libxrender1 \
10
+ libgomp1 \
11
+ libgl1-mesa-glx \
12
+ libgtk-3-0 \
13
+ libavcodec58 \
14
+ libavformat58 \
15
+ libswscale5 \
16
+ libv4l-0 \
17
+ libxvidcore4 \
18
+ libx264-160 \
19
+ libjpeg62-turbo \
20
+ libpng16-16 \
21
+ libtiff5 \
22
+ libatlas-base-dev \
23
+ gfortran \
24
+ && rm -rf /var/lib/apt/lists/*
25
+
26
+ # Set working directory
27
+ WORKDIR /app
28
+
29
+ # Copy requirements and install Python packages
30
+ COPY requirements.txt .
31
+
32
+ # Install OpenCV first with specific version
33
+ RUN pip install --no-cache-dir opencv-python-headless==4.8.1.78
34
+
35
+ # Install other dependencies
36
+ RUN pip install --no-cache-dir --upgrade pip && \
37
+ pip install --no-cache-dir -r requirements.txt
38
+
39
+ # Copy project files
40
+ COPY . .
41
+
42
+ # Expose port
43
+ EXPOSE 7860
44
+
45
+ # Start server
46
+ CMD ["uvicorn", "inference_server:app", "--host", "0.0.0.0", "--port", "7860"]
fix_build.py CHANGED
@@ -1,6 +1,6 @@
1
  #!/usr/bin/env python3
2
  """
3
- Quick fix script for Docker build issues
4
  """
5
 
6
  import os
@@ -19,47 +19,68 @@ def run_command(command, description):
19
  return None
20
 
21
  def main():
22
- print("πŸ”§ Docker Build Fix for Devam Jersey Server")
23
- print("=" * 50)
24
 
25
- print("\n🚨 The build failed due to package compatibility issues.")
26
- print("I've already fixed the Dockerfile, but here are your options:")
 
27
 
28
- print("\nπŸ“‹ Option 1: Use the Fixed Dockerfile (Recommended)")
29
- print("The Dockerfile has been updated with compatible packages.")
30
- print("Just push the changes:")
 
 
 
 
 
 
31
  print(" git add .")
32
- print(" git commit -m 'Fix Dockerfile for package compatibility'")
33
  print(" git push")
34
 
35
- print("\nπŸ“‹ Option 2: Use Alternative Dockerfile")
36
- print("If the main Dockerfile still has issues:")
37
  print(" mv Dockerfile Dockerfile.backup")
38
- print(" mv Dockerfile.alternative Dockerfile")
39
  print(" git add .")
40
- print(" git commit -m 'Use alternative Dockerfile'")
41
  print(" git push")
42
 
43
- print("\nπŸ“‹ Option 3: Manual Fix")
44
- print("Edit Dockerfile and change the base image to:")
45
- print(" FROM python:3.9-bullseye")
46
- print(" # or")
 
 
 
 
 
 
47
  print(" FROM python:3.9-buster")
48
 
49
- print("\nπŸ” What was fixed:")
50
- print("- Removed problematic 'libgl1-mesa-glx' package")
51
- print("- Added minimal OpenCV dependencies")
52
- print("- Changed to 'opencv-python-headless' in requirements.txt")
53
- print("- Created alternative Dockerfile with Ubuntu base")
 
 
 
 
 
 
 
54
 
55
- print("\nπŸ’‘ Pro tip:")
56
- print("The 'opencv-python-headless' package is more Docker-friendly")
57
- print("and doesn't require GUI libraries that cause build issues.")
 
58
 
59
- print("\nπŸš€ After fixing, monitor the build:")
60
- print("1. Go to your HF Space")
61
- print("2. Check 'Settings' β†’ 'Build logs'")
62
- print("3. Wait for successful build completion")
63
 
64
  if __name__ == "__main__":
65
  main()
 
1
  #!/usr/bin/env python3
2
  """
3
+ Quick fix script for Docker build and runtime issues
4
  """
5
 
6
  import os
 
19
  return None
20
 
21
  def main():
22
+ print("πŸ”§ Docker Build & Runtime Fix for Devam Jersey Server")
23
+ print("=" * 60)
24
 
25
+ print("\n🚨 Issues encountered:")
26
+ print("1. βœ… BUILD: Package compatibility (FIXED)")
27
+ print("2. ❌ RUNTIME: OpenCV libGL.so.1 error (NEW ISSUE)")
28
 
29
+ print("\nπŸ” Root Cause Analysis:")
30
+ print("- OpenCV is trying to load GUI libraries even with headless version")
31
+ print("- This happens when ultralytics imports cv2")
32
+ print("- The libGL.so.1 error indicates missing OpenGL libraries")
33
+
34
+ print("\nπŸ“‹ Solutions Available:")
35
+
36
+ print("\nπŸ“‹ Solution 1: Use Startup Script (Recommended)")
37
+ print("The Dockerfile now uses start_server.py which handles OpenCV better:")
38
  print(" git add .")
39
+ print(" git commit -m 'Add OpenCV headless startup script'")
40
  print(" git push")
41
 
42
+ print("\nπŸ“‹ Solution 2: Use OpenCV-Fixed Dockerfile")
43
+ print("If Solution 1 doesn't work:")
44
  print(" mv Dockerfile Dockerfile.backup")
45
+ print(" mv Dockerfile.opencv_fix Dockerfile")
46
  print(" git add .")
47
+ print(" git commit -m 'Use OpenCV-compatible Dockerfile'")
48
  print(" git push")
49
 
50
+ print("\nπŸ“‹ Solution 3: Manual OpenCV Fix")
51
+ print("Edit requirements.txt and change:")
52
+ print(" opencv-python-headless>=4.8.0,<5.0.0")
53
+ print(" to:")
54
+ print(" opencv-python-headless==4.8.1.78")
55
+
56
+ print("\nπŸ“‹ Solution 4: Alternative Base Image")
57
+ print("Try a different Python base:")
58
+ print(" FROM python:3.9-slim-bullseye")
59
+ print(" or")
60
  print(" FROM python:3.9-buster")
61
 
62
+ print("\nπŸ”§ What I've Fixed:")
63
+ print("- βœ… Updated Dockerfile with bullseye base")
64
+ print("- βœ… Added environment variables for headless mode")
65
+ print("- βœ… Created start_server.py with OpenCV handling")
66
+ print("- βœ… Created Dockerfile.opencv_fix as backup")
67
+ print("- βœ… Updated requirements.txt with version constraints")
68
+
69
+ print("\nπŸ’‘ Pro Tips:")
70
+ print("- The startup script provides better error handling")
71
+ print("- OpenCV 4.8.1.78 is known to work well in Docker")
72
+ print("- Environment variables force headless mode")
73
+ print("- Monitor build logs after each fix attempt")
74
 
75
+ print("\nπŸš€ After fixing, monitor:")
76
+ print("1. Build completion in HF Space")
77
+ print("2. Runtime logs for OpenCV errors")
78
+ print("3. API endpoint responses")
79
 
80
+ print("\nπŸ†˜ If all else fails:")
81
+ print("- Consider using a different ML framework")
82
+ print("- Use Hugging Face's built-in inference endpoints")
83
+ print("- Deploy on a different platform (AWS, GCP, etc.)")
84
 
85
  if __name__ == "__main__":
86
  main()
requirements.txt CHANGED
@@ -1,28 +1,28 @@
1
  # Core ML libraries
2
- torch>=2.0.0
3
- torchvision>=0.15.0
4
- ultralytics>=8.0.0
5
- transformers>=4.30.0
6
- Pillow>=9.0.0
7
 
8
- # Computer Vision - using headless version for Docker
9
- opencv-python-headless>=4.8.0
10
 
11
  # Vector search
12
- faiss-cpu>=1.7.0
13
  # For GPU support, use: faiss-gpu>=1.7.0
14
 
15
  # Utilities
16
- numpy>=1.24.0
17
- scikit-image>=0.20.0
18
- tqdm>=4.65.0
19
 
20
  # FastAPI and server
21
- fastapi>=0.100.0
22
- uvicorn>=0.20.0
23
 
24
  # HTTP client for testing
25
- requests>=2.28.0
26
 
27
  # Optional: For better performance
28
  # tensorrt # Only if you have NVIDIA GPU
 
1
  # Core ML libraries
2
+ torch>=2.0.0,<3.0.0
3
+ torchvision>=0.15.0,<1.0.0
4
+ ultralytics>=8.0.0,<9.0.0
5
+ transformers>=4.30.0,<5.0.0
6
+ Pillow>=9.0.0,<11.0.0
7
 
8
+ # Computer Vision - ONLY headless version, no regular opencv
9
+ opencv-python-headless==4.8.1.78
10
 
11
  # Vector search
12
+ faiss-cpu>=1.7.0,<2.0.0
13
  # For GPU support, use: faiss-gpu>=1.7.0
14
 
15
  # Utilities
16
+ numpy>=1.24.0,<2.0.0
17
+ scikit-image>=0.20.0,<1.0.0
18
+ tqdm>=4.65.0,<5.0.0
19
 
20
  # FastAPI and server
21
+ fastapi>=0.100.0,<1.0.0
22
+ uvicorn>=0.20.0,<1.0.0
23
 
24
  # HTTP client for testing
25
+ requests>=2.28.0,<3.0.0
26
 
27
  # Optional: For better performance
28
  # tensorrt # Only if you have NVIDIA GPU