Update app.py
Browse files
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 |
-
|
10 |
-
|
11 |
-
|
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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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
|