Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
def install_tensorflow():
|
| 7 |
"""
|
| 8 |
-
Installs TensorFlow using pip
|
|
|
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
print("Installing TensorFlow...")
|
|
|
|
| 12 |
result = subprocess.run(
|
| 13 |
[sys.executable, "-m", "pip", "install", "tensorflow"],
|
| 14 |
check=True,
|
|
@@ -17,54 +20,67 @@ def install_tensorflow():
|
|
| 17 |
text=True,
|
| 18 |
)
|
| 19 |
print("TensorFlow installed successfully.")
|
| 20 |
-
print(result.stdout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
except subprocess.CalledProcessError as e:
|
| 23 |
print(f"Error installing TensorFlow: {e}")
|
| 24 |
print(f"Error output (stderr):\n{e.stderr}")
|
| 25 |
-
print("Please check your pip installation
|
| 26 |
print("You may need to install TensorFlow manually. Instructions can be found at:")
|
| 27 |
print("https://www.tensorflow.org/install")
|
| 28 |
-
|
| 29 |
|
| 30 |
except Exception as ex:
|
| 31 |
print(f"An unexpected error occurred during TensorFlow installation: {ex}")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
def check_tensorflow_installation():
|
| 35 |
"""
|
| 36 |
Checks if TensorFlow is installed and can be imported.
|
| 37 |
If not installed, it calls the installation function.
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
print(f"TensorFlow version: {tf.__version__}")
|
| 42 |
print("TensorFlow is installed and ready to use.")
|
| 43 |
return True
|
| 44 |
except ImportError:
|
| 45 |
-
print("TensorFlow is not installed.")
|
| 46 |
-
install_tensorflow()
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
return False
|
| 54 |
|
| 55 |
if not check_tensorflow_installation():
|
| 56 |
print("TensorFlow installation failed. Exiting.")
|
| 57 |
sys.exit(1)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPooling2D, AveragePooling2D, Dropout
|
| 62 |
from tensorflow.keras.models import Model
|
| 63 |
from tensorflow.keras.initializers import GlorotUniform
|
| 64 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 65 |
from tensorflow.keras.optimizers import Adam
|
| 66 |
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
|
| 67 |
-
|
| 68 |
# Check if running in Google Colab
|
| 69 |
try:
|
| 70 |
from google.colab import drive
|
|
@@ -356,3 +372,4 @@ except KeyboardInterrupt:
|
|
| 356 |
except Exception as e:
|
| 357 |
print(f"An error occurred during training: {e}")
|
| 358 |
print("Please check your data and model configuration.")
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
+
import re # Import the regular expression module
|
| 5 |
+
|
| 6 |
|
| 7 |
def install_tensorflow():
|
| 8 |
"""
|
| 9 |
+
Installs TensorFlow using pip, targeting the *current* Python environment.
|
| 10 |
+
Handles potential errors during the installation. Also checks if the installation was successful.
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
print("Installing TensorFlow...")
|
| 14 |
+
# Use sys.executable to ensure we're using the correct pip for the current Python interpreter
|
| 15 |
result = subprocess.run(
|
| 16 |
[sys.executable, "-m", "pip", "install", "tensorflow"],
|
| 17 |
check=True,
|
|
|
|
| 20 |
text=True,
|
| 21 |
)
|
| 22 |
print("TensorFlow installed successfully.")
|
| 23 |
+
print(result.stdout) # Print the output of the installation
|
| 24 |
+
|
| 25 |
+
# Check for successful installation using regex
|
| 26 |
+
if re.search(r"Successfully installed tensorflow", result.stdout):
|
| 27 |
+
print("TensorFlow installation confirmed.")
|
| 28 |
+
return True # Indicate successful installation
|
| 29 |
+
else:
|
| 30 |
+
print(
|
| 31 |
+
"Warning: TensorFlow installation may not have been fully successful. "
|
| 32 |
+
"Please check the output above for any errors."
|
| 33 |
+
)
|
| 34 |
+
return False
|
| 35 |
|
| 36 |
except subprocess.CalledProcessError as e:
|
| 37 |
print(f"Error installing TensorFlow: {e}")
|
| 38 |
print(f"Error output (stderr):\n{e.stderr}")
|
| 39 |
+
print("Please check your pip installation, internet connection, and any system dependencies.")
|
| 40 |
print("You may need to install TensorFlow manually. Instructions can be found at:")
|
| 41 |
print("https://www.tensorflow.org/install")
|
| 42 |
+
return False # Indicate installation failure
|
| 43 |
|
| 44 |
except Exception as ex:
|
| 45 |
print(f"An unexpected error occurred during TensorFlow installation: {ex}")
|
| 46 |
+
return False # Indicate installation failure
|
| 47 |
+
|
| 48 |
def check_tensorflow_installation():
|
| 49 |
"""
|
| 50 |
Checks if TensorFlow is installed and can be imported.
|
| 51 |
If not installed, it calls the installation function.
|
| 52 |
"""
|
| 53 |
try:
|
| 54 |
+
import tensorflow as tf
|
| 55 |
print(f"TensorFlow version: {tf.__version__}")
|
| 56 |
print("TensorFlow is installed and ready to use.")
|
| 57 |
return True
|
| 58 |
except ImportError:
|
| 59 |
+
print("TensorFlow is not installed. Attempting to install...")
|
| 60 |
+
if install_tensorflow(): # Call install_tensorflow and check its return value
|
| 61 |
+
try:
|
| 62 |
+
import tensorflow as tf
|
| 63 |
+
print(f"TensorFlow version: {tf.__version__}")
|
| 64 |
+
print("TensorFlow is now installed and ready to use.")
|
| 65 |
+
return True
|
| 66 |
+
except ImportError:
|
| 67 |
+
print("TensorFlow installation failed. ImportError after installation.")
|
| 68 |
+
return False
|
| 69 |
+
else:
|
| 70 |
+
print("TensorFlow installation failed.")
|
| 71 |
return False
|
| 72 |
|
| 73 |
if not check_tensorflow_installation():
|
| 74 |
print("TensorFlow installation failed. Exiting.")
|
| 75 |
sys.exit(1)
|
| 76 |
+
|
| 77 |
+
import tensorflow as tf
|
|
|
|
| 78 |
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, MaxPooling2D, AveragePooling2D, Dropout
|
| 79 |
from tensorflow.keras.models import Model
|
| 80 |
from tensorflow.keras.initializers import GlorotUniform
|
| 81 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 82 |
from tensorflow.keras.optimizers import Adam
|
| 83 |
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
|
|
|
|
| 84 |
# Check if running in Google Colab
|
| 85 |
try:
|
| 86 |
from google.colab import drive
|
|
|
|
| 372 |
except Exception as e:
|
| 373 |
print(f"An error occurred during training: {e}")
|
| 374 |
print("Please check your data and model configuration.")
|
| 375 |
+
|