Natwar commited on
Commit
32c08bf
·
verified ·
1 Parent(s): aa66756

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -31
app.py CHANGED
@@ -2,41 +2,46 @@
2
  import os
3
  import subprocess
4
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Check if running in a standard environment (not Colab/Jupyter)
7
- # and install packages if needed
8
  if not os.path.exists("/.dockerenv") and not os.path.exists("/kaggle"):
9
- try:
10
- # First ensure numpy is correctly installed
11
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall", "numpy==1.23.5"])
12
-
13
- # Try importing the required packages
14
- import gradio
15
- import cv2
16
- import numpy as np
17
- import matplotlib
18
- import PIL
19
-
20
- # Special handling for TensorFlow and DeepFace dependencies
21
  try:
22
- # Install specific TensorFlow version
23
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall", "tensorflow==2.12.0"])
24
- import tensorflow as tf
25
-
26
- # Now try to import deepface
27
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall", "deepface"])
28
- import deepface
29
- except ImportError as e:
30
- print(f"Error importing dependencies: {str(e)}")
31
- print("Installing deepface with specific dependencies...")
32
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall", "deepface"])
33
-
34
- except ImportError:
35
- print("Installing required packages...")
36
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall",
37
- "numpy==1.23.5", "gradio", "opencv-python-headless", "matplotlib", "pillow"])
38
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall", "tensorflow==2.12.0"])
39
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--force-reinstall", "deepface"])
40
 
41
  # Now import the required modules
42
  import gradio as gr
 
2
  import os
3
  import subprocess
4
  import sys
5
+ import importlib
6
+ import pkg_resources
7
+
8
+ def install_package(package, version=None):
9
+ package_spec = f"{package}=={version}" if version else package
10
+ print(f"Installing {package_spec}...")
11
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", package_spec])
12
+
13
+ def ensure_package(package, version=None):
14
+ try:
15
+ if version:
16
+ pkg_resources.require(f"{package}=={version}")
17
+ else:
18
+ importlib.import_module(package)
19
+ print(f"{package} is already installed with the correct version.")
20
+ except (ImportError, pkg_resources.VersionConflict):
21
+ install_package(package, version)
22
 
23
  # Check if running in a standard environment (not Colab/Jupyter)
 
24
  if not os.path.exists("/.dockerenv") and not os.path.exists("/kaggle"):
25
+ print("Setting up environment...")
26
+
27
+ # First, uninstall problematic packages to start clean
28
+ for pkg in ["tensorflow", "numpy", "deepface", "ml-dtypes"]:
 
 
 
 
 
 
 
 
29
  try:
30
+ subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "-y", pkg])
31
+ except:
32
+ pass
33
+
34
+ # Install packages in the correct order with compatible versions
35
+ ensure_package("numpy", "1.22.4") # Lower version than before to avoid ml_dtypes issues
36
+ ensure_package("protobuf", "3.20.3") # Important for TensorFlow compatibility
37
+ ensure_package("tensorflow", "2.10.1") # Earlier version with fewer dependencies
38
+
39
+ # Install other dependencies
40
+ for pkg in ["gradio", "opencv-python-headless", "matplotlib", "pillow", "pandas"]:
41
+ ensure_package(pkg)
42
+
43
+ # Install deepface last after all dependencies are set up
44
+ ensure_package("deepface")
 
 
 
45
 
46
  # Now import the required modules
47
  import gradio as gr