Download app.py from pavankm96/bt_fatapi: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/spaces/pavankm96/bt_fatapi/resolve/main/app.py
- Command line
-
hf download hf://spaces/pavankm96/bt_fatapi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pavankm96/bt_fatapi/resolve/main/app.py
1.54 kB
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| import cv2 | |
| import tensorflow as tf # Assuming you're using TensorFlow for loading your model | |
| # Load your model | |
| model = tf.keras.models.load_model("Brain_tumor_pred_large.h5") | |
| def img_pred(upload): | |
| # Convert the Gradio input image to OpenCV format | |
| opencvImage = cv2.cvtColor(np.array(upload), cv2.COLOR_RGB2BGR) | |
| img = cv2.resize(opencvImage, (128, 128)) | |
| img = img.reshape(1, 128, 128, 3) | |
| # Predict using the model and get confidence | |
| predictions = model.predict(img)[0] # Get probabilities for each class | |
| predicted_class = np.argmax(predictions) # Index of the predicted class | |
| confidence = predictions[predicted_class] # Confidence of the predicted class | |
| # Determine if tumor is present | |
| if confidence < 0.20: | |
| if confidence <0.10: | |
| result="No Tumor" | |
| confidence=1 | |
| else: | |
| result = "Uncertain" | |
| else: | |
| if predicted_class == 1: # Assuming '1' is 'No Tumor' | |
| result = "No Tumor" | |
| else: | |
| result = "Tumor Detected" | |
| # Return result with confidence | |
| return f"The Model predicts: {result} with a confidence of {confidence:.2%}" | |
| # Define Gradio interface | |
| iface = gr.Interface( | |
| fn=img_pred, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="Brain Tumor Detection", | |
| description="Upload an MRI image to check if there is a tumor and determine the type if detected." | |
| ) | |
| # Launch the app | |
| iface.launch(share=True) | |