abuhanzala commited on
Commit
d45efd4
·
verified ·
1 Parent(s): 2a68285

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ import tensorflow as tf
5
+
6
+ # Load TFLite model
7
+ interpreter = tf.lite.Interpreter(model_path="model.tflite")
8
+ interpreter.allocate_tensors()
9
+
10
+ # Get input and output details
11
+ input_details = interpreter.get_input_details()
12
+ output_details = interpreter.get_output_details()
13
+
14
+ class_names = ['Dyskeratotic', 'Koilocytotic', 'Metaplastic', 'Parabasal', 'Superficial-Intermediat']
15
+ CONFIDENCE_THRESHOLD = 0.7
16
+
17
+ def predict_image(image):
18
+ try:
19
+ image = image.resize((224, 224)).convert("RGB")
20
+ img_array = np.array(image, dtype=np.float32) / 255.0
21
+ img_array = np.expand_dims(img_array, axis=0)
22
+
23
+ interpreter.set_tensor(input_details[0]['index'], img_array)
24
+ interpreter.invoke()
25
+ output = interpreter.get_tensor(output_details[0]['index'])
26
+
27
+ class_idx = int(np.argmax(output))
28
+ confidence = float(np.max(output))
29
+
30
+ if confidence < CONFIDENCE_THRESHOLD:
31
+ return f"⚠️ Low confidence ({confidence:.2f}). The model is unsure. Please try a clearer image."
32
+ else:
33
+ return f"✅ Prediction: {class_names[class_idx]} (Confidence: {confidence:.2f})"
34
+ except Exception as e:
35
+ return f"Error: {str(e)}"
36
+
37
+ gr.Interface(
38
+ fn=predict_image,
39
+ inputs=gr.Image(type="pil"),
40
+ outputs="text",
41
+ title="Cervical Cancer Classification",
42
+ description="Upload an image to detect cervical cancer."
43
+ ).launch()