Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
import tensorflow as tf
|
3 |
from tensorflow.keras.models import load_model
|
4 |
-
from
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
-
import os
|
8 |
-
from huggingface_hub import hf_hub_download
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
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
|
23 |
print("Loading model...")
|
24 |
model = load_model(MODEL_PATH)
|
25 |
|
26 |
-
# Define class labels
|
27 |
-
class_labels = ["Cocci", "Bacilli", "Spirilla"]
|
28 |
-
|
29 |
# Preprocessing function
|
30 |
def preprocess_image(image):
|
31 |
-
image = image.resize((224, 224)) #
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
return image
|
36 |
|
37 |
# Prediction function
|
38 |
-
def
|
39 |
-
|
40 |
-
predictions = model.predict(
|
41 |
-
|
42 |
-
|
43 |
-
return f"
|
44 |
-
|
45 |
-
# Gradio
|
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=
|
54 |
inputs=gr.Image(type="pil"),
|
55 |
outputs="text",
|
56 |
-
title=
|
57 |
-
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()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from tensorflow.keras.models import load_model
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
|
|
|
|
6 |
|
7 |
+
# Define constants
|
8 |
+
MODEL_REPO = "yolac/BacterialMorphologyClassification"
|
9 |
+
MODEL_FILENAME = "model.keras"
|
10 |
+
MODEL_PATH = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Load the model
|
13 |
print("Loading model...")
|
14 |
model = load_model(MODEL_PATH)
|
15 |
|
|
|
|
|
|
|
16 |
# Preprocessing function
|
17 |
def preprocess_image(image):
|
18 |
+
image = image.resize((224, 224)) # Adjust size as per your model input
|
19 |
+
image_array = np.array(image) / 255.0 # Normalize to [0, 1]
|
20 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
21 |
+
return image_array
|
|
|
22 |
|
23 |
# Prediction function
|
24 |
+
def predict(image):
|
25 |
+
image_array = preprocess_image(image)
|
26 |
+
predictions = model.predict(image_array)
|
27 |
+
class_names = ["Cocci", "Bacilli", "Spirilla"]
|
28 |
+
predicted_class = class_names[np.argmax(predictions)]
|
29 |
+
return f"Predicted Class: {predicted_class}"
|
30 |
+
|
31 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
interface = gr.Interface(
|
33 |
+
fn=predict,
|
34 |
inputs=gr.Image(type="pil"),
|
35 |
outputs="text",
|
36 |
+
title="Bacterial Morphology Classification",
|
37 |
+
description="Upload an image of bacteria to classify as Cocci, Bacilli, or Spirilla."
|
|
|
|
|
|
|
|
|
|
|
38 |
)
|
39 |
|
40 |
+
# Launch the app
|
41 |
if __name__ == "__main__":
|
42 |
interface.launch()
|