yolac commited on
Commit
b11be35
·
verified ·
1 Parent(s): 413e97c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -45
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 tensorflow.keras.preprocessing.image import img_to_array
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
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)) # Resize to model input size
32
- image = img_to_array(image)
33
- image = np.expand_dims(image, axis=0)
34
- image = image / 255.0 # Normalize to [0, 1]
35
- return image
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"),
55
  outputs="text",
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()
 
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()