Spaces:
Runtime error
Runtime error
Download app.py from bmedAI/Biomedical-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/bmedAI/Biomedical-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/bmedAI/Biomedical-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bmedAI/Biomedical-AI/resolve/main/app.py
1.02 kB
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| # Load trained model | |
| model = tf.keras.models.load_model( | |
| "pneumonia_3class_best_model.keras" | |
| ) | |
| CLASS_NAMES = [ | |
| "Class 1", | |
| "Class 2", | |
| "Class 3" | |
| ] | |
| def predict(image): | |
| if image is None: | |
| return "Invalid File Format" | |
| image = image.convert("RGB") | |
| image = image.resize((224, 224)) | |
| img_array = np.array(image) | |
| img_array = np.expand_dims(img_array, axis=0) | |
| predictions = model.predict(img_array, verbose=0)[0] | |
| predicted_index = np.argmax(predictions) | |
| predicted_class = CLASS_NAMES[predicted_index] | |
| confidence = predictions[predicted_index] * 100 | |
| return f"{predicted_class} ({confidence:.2f}% confidence)" | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="Upload Chest X-ray"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="Pneumonia Detection", | |
| description="Upload a chest X-ray image for 3-class classification." | |
| ) | |
| demo.launch() | |