Papizee commited on
Commit
01bc08c
·
verified ·
1 Parent(s): d2e2b92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -1,16 +1,19 @@
1
  import gradio as gr
2
- from tensorflow.keras.models import load_model
3
- from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
  from PIL import Image
6
- import io
 
 
7
 
8
- # Load the model
9
- print("Loading model...")
10
- model = load_model("model.keras")
11
- print("✅ Model loaded successfully!")
 
 
 
 
12
 
13
- # Your exact class names
14
  class_names = [
15
  "Oral Homogenous Leukoplakia",
16
  "Oral Non-Homogenous Leukoplakia",
@@ -18,40 +21,41 @@ class_names = [
18
  ]
19
 
20
  def predict_image(img):
 
 
 
21
  try:
22
- # Preprocess the image
23
  img = img.resize((224, 224))
24
  img_array = image.img_to_array(img)
25
  img_array = np.expand_dims(img_array, axis=0)
26
  img_array = img_array / 255.0
27
 
28
- # Make prediction
29
  predictions = model.predict(img_array, verbose=0)
30
  predicted_class = int(np.argmax(predictions[0]))
31
  confidence = float(np.max(predictions[0]) * 100)
32
 
33
- # Prepare results
34
  result = class_names[predicted_class]
35
- confidences = {class_names[i]: float(predictions[0][i] * 100) for i in range(3)}
36
 
37
  return result, confidences
38
 
39
  except Exception as e:
40
- return f"Error: {str(e)}", {}
41
 
42
- # Create Gradio Interface
43
  interface = gr.Interface(
44
  fn=predict_image,
45
  inputs=gr.Image(type="pil", label="Upload Oral Image"),
46
  outputs=[
47
  gr.Label(label="Predicted Condition"),
48
- gr.Label(label="Confidence Scores")
49
  ],
50
- title="OralScan AI - Oral Lesion Classifier",
51
- description="Upload an image of oral lesion to get prediction using MobileNetV2",
52
- examples=None,
53
  allow_flagging="never"
54
  )
55
 
56
  if __name__ == "__main__":
57
- interface.launch()
 
1
  import gradio as gr
 
 
2
  import numpy as np
3
  from PIL import Image
4
+ from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.preprocessing import image
6
+ import os
7
 
8
+ # Load model
9
+ print("Loading MobileNetV2 model...")
10
+ try:
11
+ model = load_model("model.keras")
12
+ print("✅ Model loaded successfully!")
13
+ except Exception as e:
14
+ print(f"❌ Model loading failed: {e}")
15
+ model = None
16
 
 
17
  class_names = [
18
  "Oral Homogenous Leukoplakia",
19
  "Oral Non-Homogenous Leukoplakia",
 
21
  ]
22
 
23
  def predict_image(img):
24
+ if model is None:
25
+ return "Model failed to load. Please check logs.", {}
26
+
27
  try:
28
+ # Preprocess
29
  img = img.resize((224, 224))
30
  img_array = image.img_to_array(img)
31
  img_array = np.expand_dims(img_array, axis=0)
32
  img_array = img_array / 255.0
33
 
34
+ # Predict
35
  predictions = model.predict(img_array, verbose=0)
36
  predicted_class = int(np.argmax(predictions[0]))
37
  confidence = float(np.max(predictions[0]) * 100)
38
 
 
39
  result = class_names[predicted_class]
40
+ confidences = {class_names[i]: round(float(predictions[0][i] * 100), 2) for i in range(3)}
41
 
42
  return result, confidences
43
 
44
  except Exception as e:
45
+ return f"Error during prediction: {str(e)}", {}
46
 
47
+ # Gradio Interface
48
  interface = gr.Interface(
49
  fn=predict_image,
50
  inputs=gr.Image(type="pil", label="Upload Oral Image"),
51
  outputs=[
52
  gr.Label(label="Predicted Condition"),
53
+ gr.JSON(label="Confidence Scores")
54
  ],
55
+ title="🦷 OralScan AI - Oral Lesion Classifier",
56
+ description="MobileNetV2 model for detecting oral white lesions",
 
57
  allow_flagging="never"
58
  )
59
 
60
  if __name__ == "__main__":
61
+ interface.launch(server_name="0.0.0.0", server_port=7860)