NerdyAlgorithm commited on
Commit
be41f59
·
verified ·
1 Parent(s): 3b19a73

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
7
+ # Load the model
8
+ print("Loading MobileNetV2 model...")
9
+ try:
10
+ model = load_model("model.keras")
11
+ print("✅ Model loaded successfully!")
12
+ except Exception as e:
13
+ print(f"❌ Model loading failed: {e}")
14
+ model = None
15
+
16
+ class_names = [
17
+ "Oral Homogenous Leukoplakia",
18
+ "Oral Non-Homogenous Leukoplakia",
19
+ "Other Oral White Lesions"
20
+ ]
21
+
22
+ def predict_image(img):
23
+ if model is None:
24
+ return "Error: Model failed to load. Please check if model.keras is uploaded.", {}
25
+
26
+ try:
27
+ # Preprocess image
28
+ img = img.resize((224, 224))
29
+ img_array = image.img_to_array(img)
30
+ img_array = np.expand_dims(img_array, axis=0)
31
+ img_array = img_array / 255.0
32
+
33
+ # Predict
34
+ predictions = model.predict(img_array, verbose=0)
35
+ predicted_class = int(np.argmax(predictions[0]))
36
+ confidence = float(np.max(predictions[0]) * 100)
37
+
38
+ result = class_names[predicted_class]
39
+ confidences = {class_names[i]: round(float(predictions[0][i] * 100), 2) for i in range(3)}
40
+
41
+ return result, confidences
42
+
43
+ except Exception as e:
44
+ return f"Prediction error: {str(e)}", {}
45
+
46
+ # Gradio Interface (updated for newer Gradio)
47
+ demo = gr.Interface(
48
+ fn=predict_image,
49
+ inputs=gr.Image(type="pil", label="Upload Oral Image"),
50
+ outputs=[
51
+ gr.Label(label="Predicted Condition"),
52
+ gr.JSON(label="Confidence Scores")
53
+ ],
54
+ title="🦷 OralScan AI - Oral Lesion Classifier",
55
+ description="Upload an image to detect oral white lesions using MobileNetV2",
56
+ examples=None,
57
+ flagging_mode="never" # Updated parameter name
58
+ )
59
+
60
+ if __name__ == "__main__":
61
+ demo.launch(server_name="0.0.0.0", server_port=7860)