Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,18 @@ 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
|
| 10 |
MODEL_PATH = "https://huggingface.co/yolac/BacterialMorphologyClassification/resolve/main/model.keras"
|
| 11 |
|
| 12 |
-
#
|
| 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
|
|
@@ -37,23 +36,19 @@ def preprocess_image(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 |
-
#
|
| 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"),
|
|
@@ -61,12 +56,11 @@ interface = gr.Interface(
|
|
| 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()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import os
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
+
# Cache model locally
|
| 11 |
MODEL_PATH = "https://huggingface.co/yolac/BacterialMorphologyClassification/resolve/main/model.keras"
|
| 12 |
|
| 13 |
+
# Download the model if not present
|
| 14 |
if not os.path.exists(MODEL_PATH):
|
|
|
|
| 15 |
print("Downloading model...")
|
| 16 |
hf_hub_download(
|
| 17 |
repo_id="yolac/BacterialMorphologyClassification",
|
| 18 |
filename="model.keras",
|
| 19 |
local_dir="./",
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
# Load the model
|
|
|
|
| 36 |
|
| 37 |
# Prediction function
|
| 38 |
def classify_bacteria(image):
|
|
|
|
| 39 |
processed_image = preprocess_image(image)
|
|
|
|
| 40 |
predictions = model.predict(processed_image)
|
| 41 |
predicted_class = np.argmax(predictions, axis=-1)[0]
|
| 42 |
confidence = predictions[0][predicted_class]
|
|
|
|
| 43 |
return f"{class_labels[predicted_class]} (Confidence: {confidence:.2f})"
|
| 44 |
|
| 45 |
+
# Gradio interface
|
| 46 |
title = "Bacterial Morphology Classifier"
|
| 47 |
description = (
|
| 48 |
"Upload an image of bacteria, and the model will classify it into one of three types: "
|
| 49 |
"**Cocci**, **Bacilli**, or **Spirilla**."
|
| 50 |
)
|
| 51 |
|
|
|
|
| 52 |
interface = gr.Interface(
|
| 53 |
fn=classify_bacteria,
|
| 54 |
inputs=gr.Image(type="pil"),
|
|
|
|
| 56 |
title=title,
|
| 57 |
description=description,
|
| 58 |
examples=[
|
| 59 |
+
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%20290.jpg"],
|
| 60 |
+
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%20565.jpg"],
|
| 61 |
["https://huggingface.co/datasets/yolac/BacterialMorphologyClassification/resolve/main/img%208.jpg"]
|
| 62 |
],
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
interface.launch()
|