Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,15 @@
|
|
1 |
import json
|
2 |
-
from PIL import Image
|
3 |
import numpy as np
|
4 |
-
import
|
5 |
-
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
|
6 |
-
from tensorflow.keras.models import load_model
|
7 |
-
import ipywidgets as widgets
|
8 |
-
from IPython.display import display
|
9 |
|
|
|
10 |
model_path = 'final_teath_classifier.h5'
|
11 |
-
|
12 |
model = tf.keras.models.load_model(model_path)
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
def preprocess_image(image: Image.Image) -> np.ndarray:
|
17 |
# Resize the image to match input size
|
|
|
18 |
image = image.resize((256, 256))
|
19 |
# Convert image to array and preprocess input
|
20 |
img_array = np.array(image) / 255.0
|
@@ -22,13 +17,9 @@ def preprocess_image(image: Image.Image) -> np.ndarray:
|
|
22 |
img_array = np.expand_dims(img_array, axis=0)
|
23 |
return img_array
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
img_array = preprocess_image(img)
|
29 |
-
# Convert image array to string using base64 encoding (for text-based models)
|
30 |
-
#inputs = tokenizer.encode(img_array, return_tensors="tf")
|
31 |
-
# Make prediction
|
32 |
outputs = model(img_array)
|
33 |
predictions = tf.nn.softmax(outputs.logits, axis=-1)
|
34 |
predicted_class = np.argmax(predictions)
|
@@ -36,31 +27,13 @@ def predict_image(image_path):
|
|
36 |
predict_label = "Clean"
|
37 |
else:
|
38 |
predict_label = "Carries"
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
uploader = widgets.FileUpload(accept="image/*", multiple=False)
|
44 |
-
|
45 |
-
# Display the file uploader widget
|
46 |
-
display(uploader)
|
47 |
|
48 |
-
|
49 |
-
def on_upload(change):
|
50 |
-
# Get the uploaded image file
|
51 |
-
image_file = list(uploader.value.values())[0]["content"]
|
52 |
-
# Save the image to a temporary file
|
53 |
-
with open("temp_image.jpg", "wb") as f:
|
54 |
-
f.write(image_file)
|
55 |
-
# Get predictions for the uploaded image
|
56 |
-
predict_label, logits = predict_image("temp_image.jpg")
|
57 |
-
# Create a JSON object with the predictions
|
58 |
-
predictions_json = {
|
59 |
-
"predicted_class": predict_label,
|
60 |
-
"evaluations": [f"{logit*100:.4f}%" for logit in logits]
|
61 |
-
}
|
62 |
-
# Print the JSON object
|
63 |
-
print(json.dumps(predictions_json, indent=4))
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
1 |
import json
|
|
|
2 |
import numpy as np
|
3 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Load the model
|
6 |
model_path = 'final_teath_classifier.h5'
|
|
|
7 |
model = tf.keras.models.load_model(model_path)
|
8 |
|
9 |
+
# Define preprocessing function
|
10 |
+
def preprocess_image(image):
|
|
|
11 |
# Resize the image to match input size
|
12 |
+
image = Image.fromarray(image)
|
13 |
image = image.resize((256, 256))
|
14 |
# Convert image to array and preprocess input
|
15 |
img_array = np.array(image) / 255.0
|
|
|
17 |
img_array = np.expand_dims(img_array, axis=0)
|
18 |
return img_array
|
19 |
|
20 |
+
# Define prediction function
|
21 |
+
def predict_image(image):
|
22 |
+
img_array = preprocess_image(image)
|
|
|
|
|
|
|
|
|
23 |
outputs = model(img_array)
|
24 |
predictions = tf.nn.softmax(outputs.logits, axis=-1)
|
25 |
predicted_class = np.argmax(predictions)
|
|
|
27 |
predict_label = "Clean"
|
28 |
else:
|
29 |
predict_label = "Carries"
|
30 |
+
return {"prediction": predict_label, "confidence": float(np.max(predictions))}
|
31 |
|
32 |
+
# Create the interface
|
33 |
+
input_interface = gr.inputs.Image(shape=(256, 256))
|
34 |
+
output_interface = gr.outputs.Label(num_top_classes=2)
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
iface = gr.Interface(fn=predict_image, inputs=input_interface, outputs=output_interface)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Launch the interface
|
39 |
+
iface.launch()
|