Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,72 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
-
from
|
|
|
4 |
import numpy as np
|
5 |
-
import
|
6 |
-
|
7 |
-
# Set up logging for debugging
|
8 |
-
logging.basicConfig(level=logging.DEBUG)
|
9 |
|
10 |
-
#
|
11 |
MODEL_PATH = "https://huggingface.co/yolac/BacterialMorphologyClassification/resolve/main/model.keras"
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def preprocess_image(image):
|
21 |
-
|
22 |
-
image = image
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# Create a Gradio interface
|
53 |
-
gr.Interface(
|
54 |
-
fn=predict,
|
55 |
-
inputs=gr.Image(type="pil", label="Upload an image"),
|
56 |
-
outputs=["text", "number"],
|
57 |
examples=[
|
58 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%20290.jpg"],
|
59 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%20565.jpg"],
|
60 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%208.jpg"]
|
61 |
-
]
|
62 |
-
)
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
5 |
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import os
|
|
|
|
|
8 |
|
9 |
+
# Cache model during setup
|
10 |
MODEL_PATH = "https://huggingface.co/yolac/BacterialMorphologyClassification/resolve/main/model.keras"
|
11 |
+
|
12 |
+
# Check if the model exists locally, otherwise download
|
13 |
+
if not os.path.exists(MODEL_PATH):
|
14 |
+
from huggingface_hub import hf_hub_download
|
15 |
+
print("Downloading model...")
|
16 |
+
hf_hub_download(
|
17 |
+
repo_id="yolac/BacterialMorphologyClassification",
|
18 |
+
filename="model.keras",
|
19 |
+
local_dir="./",
|
20 |
+
local_dir_use_symlinks=False
|
21 |
+
)
|
22 |
+
|
23 |
+
# Load the model
|
24 |
+
print("Loading model...")
|
25 |
+
model = load_model(MODEL_PATH)
|
26 |
+
|
27 |
+
# Define class labels
|
28 |
+
class_labels = ["Cocci", "Bacilli", "Spirilla"]
|
29 |
+
|
30 |
+
# Preprocessing function
|
31 |
def preprocess_image(image):
|
32 |
+
image = image.resize((224, 224)) # Resize to model input size
|
33 |
+
image = img_to_array(image)
|
34 |
+
image = np.expand_dims(image, axis=0)
|
35 |
+
image = image / 255.0 # Normalize to [0, 1]
|
36 |
+
return image
|
37 |
+
|
38 |
+
# Prediction function
|
39 |
+
def classify_bacteria(image):
|
40 |
+
# Preprocess the input image
|
41 |
+
processed_image = preprocess_image(image)
|
42 |
+
# Get model predictions
|
43 |
+
predictions = model.predict(processed_image)
|
44 |
+
predicted_class = np.argmax(predictions, axis=-1)[0]
|
45 |
+
confidence = predictions[0][predicted_class]
|
46 |
+
# Return the class and confidence
|
47 |
+
return f"{class_labels[predicted_class]} (Confidence: {confidence:.2f})"
|
48 |
+
|
49 |
+
# Define Gradio interface
|
50 |
+
title = "Bacterial Morphology Classifier"
|
51 |
+
description = (
|
52 |
+
"Upload an image of bacteria, and the model will classify it into one of three types: "
|
53 |
+
"**Cocci**, **Bacilli**, or **Spirilla**."
|
54 |
+
)
|
55 |
+
|
56 |
+
# Gradio UI
|
57 |
+
interface = gr.Interface(
|
58 |
+
fn=classify_bacteria,
|
59 |
+
inputs=gr.Image(type="pil"),
|
60 |
+
outputs="text",
|
61 |
+
title=title,
|
62 |
+
description=description,
|
|
|
|
|
|
|
|
|
|
|
63 |
examples=[
|
64 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%20290.jpg"],
|
65 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%20565.jpg"],
|
66 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%208.jpg"]
|
67 |
+
],
|
68 |
+
)
|
69 |
+
|
70 |
+
# Launch the app
|
71 |
+
if __name__ == "__main__":
|
72 |
+
interface.launch()
|