syk101 commited on
Commit
755ef14
·
verified ·
1 Parent(s): ea597a2

Upload 244 files

Browse files
NewProject ali 9 dec 2/Dockerfile CHANGED
@@ -34,6 +34,7 @@ RUN if [ ! -s "./utils/__init__.py" ] || [ ! -f "./utils/__init__.py" ]; then ec
34
  # Debug: Show what we have
35
  RUN echo "=== CHECKING CONTROLLER DIRECTORY ===" && ls -la ./controller/
36
  RUN echo "=== CHECKING CONTROLLER MODELS DIRECTORY ===" && ls -la ./controller/models/
 
37
 
38
  # Install the package in development mode
39
  RUN pip install -e .
@@ -45,7 +46,7 @@ ENV PYTHONPATH=/app:/app/controller:/app/controller/models:/app/models:/app/util
45
  RUN echo "=== FINAL DIRECTORY STRUCTURE ===" && find . -name "*.py" | head -20
46
  RUN echo "=== PYTHON PATH ===" && echo $PYTHONPATH
47
 
48
- # Make startup script executable
49
  RUN chmod +x /app/startup.sh
50
 
51
  # Expose port
@@ -53,4 +54,4 @@ EXPOSE 7860
53
 
54
  # Run the startup script
55
  WORKDIR /app
56
- CMD ["/app/startup.sh"]
 
34
  # Debug: Show what we have
35
  RUN echo "=== CHECKING CONTROLLER DIRECTORY ===" && ls -la ./controller/
36
  RUN echo "=== CHECKING CONTROLLER MODELS DIRECTORY ===" && ls -la ./controller/models/
37
+ RUN echo "=== CHECKING STARTUP SCRIPT ===" && ls -la ./startup.sh
38
 
39
  # Install the package in development mode
40
  RUN pip install -e .
 
46
  RUN echo "=== FINAL DIRECTORY STRUCTURE ===" && find . -name "*.py" | head -20
47
  RUN echo "=== PYTHON PATH ===" && echo $PYTHONPATH
48
 
49
+ # Make startup script executable (redundant but safe)
50
  RUN chmod +x /app/startup.sh
51
 
52
  # Expose port
 
54
 
55
  # Run the startup script
56
  WORKDIR /app
57
+ CMD ["/bin/bash", "/app/startup.sh"]
NewProject ali 9 dec 2/FINAL_FIX_SUMMARY.md ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Final Fix for ModuleNotFoundError on Hugging Face Spaces
2
+
3
+ ## Problem Summary
4
+ The persistent `ModuleNotFoundError: No module named 'controller'` error was occurring because:
5
+ 1. Gunicorn was trying to import modules before the Python environment was properly configured
6
+ 2. There was no verification of imports before starting the application
7
+ 3. The Python path wasn't being set correctly in the container environment
8
+
9
+ ## Solution Implemented
10
+
11
+ ### 1. Dedicated Startup Script (`startup.sh`)
12
+ Created a robust startup script that:
13
+ - Explicitly sets `PYTHONPATH` to include all necessary directories
14
+ - Determines the correct Python command to use (`python3` or `python`)
15
+ - Verifies that critical imports work before starting the application
16
+ - Provides clear error messages if imports fail
17
+ - Only proceeds to start gunicorn if all imports succeed
18
+
19
+ ### 2. Updated Dockerfile
20
+ - Ensures the startup script is properly copied and made executable
21
+ - Adds debugging output to verify the script exists
22
+ - Uses the startup script as the entry point
23
+
24
+ ### 3. Proper Error Handling
25
+ The startup script follows this sequence:
26
+ 1. Set up environment variables
27
+ 2. Determine which Python command to use
28
+ 3. Test Python path setup
29
+ 4. Test controller module import
30
+ 5. Test specific controller import (pix2text_controller)
31
+ 6. Start gunicorn only if all tests pass
32
+
33
+ ## Why This Fix Works
34
+
35
+ ### Early Import Verification
36
+ By testing imports before starting gunicorn, we catch import issues early with clear diagnostics rather than letting them cause cryptic errors during worker initialization.
37
+
38
+ ### Explicit Path Management
39
+ The script explicitly manages the Python path, ensuring that all necessary directories are included before any imports are attempted.
40
+
41
+ ### Robust Command Detection
42
+ The script determines which Python command is available in the environment, making it compatible with different container setups.
43
+
44
+ ## Expected Outcomes
45
+
46
+ ### Success Case (in Hugging Face logs):
47
+ ```
48
+ === STARTUP.SH EXECUTING ===
49
+ Using python3
50
+ === TESTING PYTHON PATH SETUP ===
51
+ ✅ Python path setup successful
52
+ === TESTING CONTROLLER IMPORT ===
53
+ ✅ Controller module imported successfully
54
+ === TESTING PIX2TEXT CONTROLLER IMPORT ===
55
+ ✅ pix2text_controller imported successfully
56
+ === ALL IMPORTS SUCCESSFUL, STARTING GUNICORN ===
57
+ ```
58
+
59
+ ### Failure Case (in Hugging Face logs):
60
+ ```
61
+ === STARTUP.SH EXECUTING ===
62
+ Using python3
63
+ === TESTING PYTHON PATH SETUP ===
64
+ ✅ Python path setup successful
65
+ === TESTING CONTROLLER IMPORT ===
66
+ ❌ Failed to import controller module
67
+ [Detailed error information]
68
+ ```
69
+
70
+ ## Files Modified
71
+
72
+ 1. **`startup.sh`** - New startup script with import verification
73
+ 2. **`Dockerfile`** - Updated to use the startup script as entry point
74
+ 3. **No changes to `app.py`** - Keeping the original functionality
75
+
76
+ This approach follows the proven pattern from the memory "Hugging Face Spaces startup script pattern" and should resolve the persistent ModuleNotFoundError.
NewProject ali 9 dec 2/startup.sh CHANGED
@@ -1,26 +1,47 @@
1
  #!/bin/bash
2
 
3
  echo "=== STARTUP.SH EXECUTING ==="
 
 
4
  echo "Current directory: $(pwd)"
5
  echo "PYTHONPATH: $PYTHONPATH"
 
6
 
7
  # Set PYTHONPATH explicitly
8
  export PYTHONPATH=/app:/app/controller:/app/controller/models:/app/models:/app/utils
9
  echo "Updated PYTHONPATH: $PYTHONPATH"
10
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Add current directory to Python path
 
12
  PYTHON_CMD="import sys; sys.path.insert(0, '/app'); print('Python path:', sys.path[:5])"
13
- python -c "$PYTHON_CMD"
 
 
 
 
 
14
 
15
  # Test if we can import the controller module
16
  echo "=== TESTING CONTROLLER IMPORT ==="
17
  PYTHON_CMD="import sys; sys.path.insert(0, '/app'); import controller; print('Controller imported successfully from:', controller.__file__)"
18
- if python -c "$PYTHON_CMD"; then
19
  echo "✅ Controller module imported successfully"
20
  else
21
  echo "❌ Failed to import controller module"
22
  echo "Python path:"
23
- python -c "import sys; [print(i, p) for i, p in enumerate(sys.path)]"
24
  echo "Directory structure:"
25
  find /app -name "*.py" | head -20
26
  exit 1
@@ -29,7 +50,7 @@ fi
29
  # Test if we can import the specific controller we need
30
  echo "=== TESTING PIX2TEXT CONTROLLER IMPORT ==="
31
  PYTHON_CMD="import sys; sys.path.insert(0, '/app'); from controller.pix2text_controller import pix2text_bp; print('pix2text_controller imported successfully')"
32
- if python -c "$PYTHON_CMD"; then
33
  echo "✅ pix2text_controller imported successfully"
34
  else
35
  echo "❌ Failed to import pix2text_controller"
@@ -37,4 +58,4 @@ else
37
  fi
38
 
39
  echo "=== ALL IMPORTS SUCCESSFUL, STARTING GUNICORN ==="
40
- exec gunicorn --bind 0.0.0.0:7860 --chdir /app --timeout 120 --workers 1 app:app
 
1
  #!/bin/bash
2
 
3
  echo "=== STARTUP.SH EXECUTING ==="
4
+ echo "Script location: $(which bash)"
5
+ echo "Current user: $(whoami)"
6
  echo "Current directory: $(pwd)"
7
  echo "PYTHONPATH: $PYTHONPATH"
8
+ echo "PATH: $PATH"
9
 
10
  # Set PYTHONPATH explicitly
11
  export PYTHONPATH=/app:/app/controller:/app/controller/models:/app/models:/app/utils
12
  echo "Updated PYTHONPATH: $PYTHONPATH"
13
 
14
+ # Determine which Python command to use
15
+ if command -v python3 &> /dev/null; then
16
+ PYTHON_CMD_PREFIX="python3"
17
+ echo "Using python3"
18
+ elif command -v python &> /dev/null; then
19
+ PYTHON_CMD_PREFIX="python"
20
+ echo "Using python"
21
+ else
22
+ echo "❌ No Python interpreter found"
23
+ exit 1
24
+ fi
25
+
26
  # Add current directory to Python path
27
+ echo "=== TESTING PYTHON PATH SETUP ==="
28
  PYTHON_CMD="import sys; sys.path.insert(0, '/app'); print('Python path:', sys.path[:5])"
29
+ if $PYTHON_CMD_PREFIX -c "$PYTHON_CMD"; then
30
+ echo "✅ Python path setup successful"
31
+ else
32
+ echo "❌ Python path setup failed"
33
+ exit 1
34
+ fi
35
 
36
  # Test if we can import the controller module
37
  echo "=== TESTING CONTROLLER IMPORT ==="
38
  PYTHON_CMD="import sys; sys.path.insert(0, '/app'); import controller; print('Controller imported successfully from:', controller.__file__)"
39
+ if $PYTHON_CMD_PREFIX -c "$PYTHON_CMD"; then
40
  echo "✅ Controller module imported successfully"
41
  else
42
  echo "❌ Failed to import controller module"
43
  echo "Python path:"
44
+ $PYTHON_CMD_PREFIX -c "import sys; [print(i, p) for i, p in enumerate(sys.path)]"
45
  echo "Directory structure:"
46
  find /app -name "*.py" | head -20
47
  exit 1
 
50
  # Test if we can import the specific controller we need
51
  echo "=== TESTING PIX2TEXT CONTROLLER IMPORT ==="
52
  PYTHON_CMD="import sys; sys.path.insert(0, '/app'); from controller.pix2text_controller import pix2text_bp; print('pix2text_controller imported successfully')"
53
+ if $PYTHON_CMD_PREFIX -c "$PYTHON_CMD"; then
54
  echo "✅ pix2text_controller imported successfully"
55
  else
56
  echo "❌ Failed to import pix2text_controller"
 
58
  fi
59
 
60
  echo "=== ALL IMPORTS SUCCESSFUL, STARTING GUNICORN ==="
61
+ exec $PYTHON_CMD_PREFIX -m gunicorn --bind 0.0.0.0:7860 --chdir /app --timeout 120 --workers 1 app:app
NewProject ali 9 dec 2/test_startup_locally.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Test script to verify the startup approach works locally
4
+ """
5
+
6
+ import os
7
+ import sys
8
+ import subprocess
9
+
10
+ def test_startup_script():
11
+ print("=== TESTING STARTUP APPROACH LOCALLY ===")
12
+
13
+ # Change to the project directory
14
+ project_dir = "/Users/shayokhsarker/Downloads/NewProject ali 9 dec 2"
15
+ os.chdir(project_dir)
16
+ print(f"Changed to directory: {os.getcwd()}")
17
+
18
+ # Set up environment
19
+ env = os.environ.copy()
20
+ env["PYTHONPATH"] = "/Users/shayokhsarker/Downloads/NewProject ali 9 dec 2:/Users/shayokhsarker/Downloads/NewProject ali 9 dec 2/controller:/Users/shayokhsarker/Downloads/NewProject ali 9 dec 2/controller/models:/Users/shayokhsarker/Downloads/NewProject ali 9 dec 2/models:/Users/shayokhsarker/Downloads/NewProject ali 9 dec 2/utils"
21
+
22
+ # Test if we can import controller
23
+ print("\n=== TESTING CONTROLLER IMPORT ===")
24
+ try:
25
+ # Add current directory to path
26
+ sys.path.insert(0, project_dir)
27
+ import controller
28
+ print(f"✅ Controller imported successfully from: {controller.__file__}")
29
+ except Exception as e:
30
+ print(f"❌ Failed to import controller: {e}")
31
+ return False
32
+
33
+ # Test specific controller import
34
+ print("\n=== TESTING PIX2TEXT CONTROLLER IMPORT ===")
35
+ try:
36
+ from controller.pix2text_controller import pix2text_bp
37
+ print("✅ pix2text_controller imported successfully")
38
+ except Exception as e:
39
+ print(f"❌ Failed to import pix2text_controller: {e}")
40
+ return False
41
+
42
+ print("\n✅ All tests passed locally!")
43
+ return True
44
+
45
+ if __name__ == "__main__":
46
+ success = test_startup_script()
47
+ sys.exit(0 if success else 1)