Papizee commited on
Commit
272ca95
·
verified ·
1 Parent(s): 10bf0e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
17
+ "Other Oral White Lesions"
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()