Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Function to install a package using pip
|
| 4 |
def install_package(package):
|
|
@@ -7,42 +13,44 @@ def install_package(package):
|
|
| 7 |
except subprocess.CalledProcessError as e:
|
| 8 |
print(f"Error installing {package}: {e}")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import numpy as np
|
| 18 |
-
from PIL import Image
|
| 19 |
-
import io
|
| 20 |
-
|
| 21 |
-
# Initialize Flask application
|
| 22 |
-
app = Flask(__name__)
|
| 23 |
|
| 24 |
# Load the pre-trained TensorFlow model
|
| 25 |
-
# Load the pre-trained TensorFlow model (assuming it's in the same directory as app.py)
|
| 26 |
model = tf.keras.models.load_model("imageclassifier.h5")
|
| 27 |
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Define the function to predict the teeth health
|
| 31 |
def predict_teeth_health(image):
|
| 32 |
-
# Convert the PIL image object to a
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Load the image from the file-like object
|
| 37 |
-
image = tf.keras.preprocessing.image.load_img(image_bytes, target_size=(256, 256))
|
| 38 |
-
image = tf.keras.preprocessing.image.img_to_array(image)
|
| 39 |
-
image = np.expand_dims(image, axis=0)
|
| 40 |
|
| 41 |
# Make a prediction
|
| 42 |
-
prediction = model.predict(image)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
probability_good = prediction[0]
|
| 46 |
|
| 47 |
# Define the prediction result
|
| 48 |
result = {
|
|
|
|
| 1 |
import subprocess
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
from flask import Flask, request, jsonify
|
| 8 |
|
| 9 |
# Function to install a package using pip
|
| 10 |
def install_package(package):
|
|
|
|
| 13 |
except subprocess.CalledProcessError as e:
|
| 14 |
print(f"Error installing {package}: {e}")
|
| 15 |
|
| 16 |
+
# List of libraries to install
|
| 17 |
+
libraries = [
|
| 18 |
+
'gradio',
|
| 19 |
+
'tensorflow',
|
| 20 |
+
'numpy',
|
| 21 |
+
'Pillow',
|
| 22 |
+
'opencv-python-headless',
|
| 23 |
+
'Flask',
|
| 24 |
+
'joblib'
|
| 25 |
+
]
|
| 26 |
|
| 27 |
+
# Install each library using pip
|
| 28 |
+
for library in libraries:
|
| 29 |
+
install_package(library)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Load the pre-trained TensorFlow model
|
|
|
|
| 32 |
model = tf.keras.models.load_model("imageclassifier.h5")
|
| 33 |
|
| 34 |
+
# Save the model as .pkl file
|
| 35 |
+
joblib.dump(model, "imageclassifier.pkl")
|
| 36 |
|
| 37 |
+
# Initialize Flask application
|
| 38 |
+
app = Flask(__name__)
|
| 39 |
+
|
| 40 |
+
# Load the model from .pkl file
|
| 41 |
+
model = joblib.load("imageclassifier.pkl")
|
| 42 |
|
| 43 |
# Define the function to predict the teeth health
|
| 44 |
def predict_teeth_health(image):
|
| 45 |
+
# Convert the PIL image object to a numpy array
|
| 46 |
+
image = np.array(image)
|
| 47 |
+
# Perform any necessary preprocessing (resizing, normalization, etc.) here if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Make a prediction
|
| 50 |
+
prediction = model.predict(image.reshape(1, -1))
|
| 51 |
|
| 52 |
+
# Assuming binary classification, adjust as per your model's output
|
| 53 |
+
probability_good = prediction[0] # Assuming it's a binary classification
|
| 54 |
|
| 55 |
# Define the prediction result
|
| 56 |
result = {
|