phiaaa commited on
Commit
af3260e
·
verified ·
1 Parent(s): 1bebb68

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 tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # Load the TFLite model
7
+ interpreter = tf.lite.Interpreter(model_path="stool_model.tflite")
8
+ interpreter.allocate_tensors()
9
+
10
+ input_details = interpreter.get_input_details()
11
+ output_details = interpreter.get_output_details()
12
+
13
+ # Define label names (adjust if your model uses different ones)
14
+ labels = ["bloody", "hard stool", "normal", "parasite", "watery"]
15
+
16
+ def preprocess_image(img: Image.Image):
17
+ img = img.convert("RGB").resize((128, 128))
18
+ arr = np.asarray(img).astype(np.float32) / 255.0
19
+ arr = np.expand_dims(arr, axis=0)
20
+ return arr
21
+
22
+ def classify_image(image):
23
+ try:
24
+ input_data = preprocess_image(image)
25
+ interpreter.set_tensor(input_details[0]['index'], input_data)
26
+ interpreter.invoke()
27
+ output_data = interpreter.get_tensor(output_details[0]['index'])[0]
28
+ results = {labels[i]: float(output_data[i]) for i in range(len(labels))}
29
+ sorted_results = dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
30
+ return sorted_results
31
+ except Exception as e:
32
+ return {"error": str(e)}
33
+
34
+ demo = gr.Interface(
35
+ fn=classify_image,
36
+ inputs=gr.Image(type="pil", label="Upload stool image"),
37
+ outputs=gr.Label(num_top_classes=3, label="Predictions"),
38
+ title="Stool Diagnosis Model",
39
+ description="Upload a stool image for classification."
40
+ )
41
+
42
+ if __name__ == "__main__":
43
+ demo.launch(server_name="0.0.0.0", server_port=7860)