Spaces:
Runtime error
Runtime error
Initial commit
Browse files- .gitignore +1 -0
- app.py +46 -0
- requirements.txt +10 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Define model repository
|
| 7 |
+
model_name = "Aya-Ch/brain-tumor-classifier"
|
| 8 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Define brain tumor classes
|
| 14 |
+
tumor_classes = ['meningioma', 'glioma', 'pituitary tumor']
|
| 15 |
+
|
| 16 |
+
def predict(image):
|
| 17 |
+
try:
|
| 18 |
+
# Process the image using the processor
|
| 19 |
+
processed_image = processor(images=image, return_tensors="pt")['pixel_values']
|
| 20 |
+
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
outputs = model(processed_image)
|
| 23 |
+
logits = outputs.logits # Get classification scores
|
| 24 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 25 |
+
|
| 26 |
+
# Convert tensor outputs to Python numbers
|
| 27 |
+
results = {tumor_classes[i]: float(probs[0, i]) for i in range(len(tumor_classes))}
|
| 28 |
+
return results
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return {"Error": f"Failed to process image: {str(e)}"}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Gradio Interface
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=predict,
|
| 37 |
+
inputs=gr.Image(type="pil"), # Accepts image input
|
| 38 |
+
outputs=gr.Label(label="Tumor Classification"),
|
| 39 |
+
title="Brain Tumor Classifier",
|
| 40 |
+
description="Upload an MRI scan to classify the type of brain tumor(meningioma, glioma or pituitary tumor)",
|
| 41 |
+
allow_flagging="never"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Launch the app
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
Pillow
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# This is only needed for local deployment
|
| 10 |
+
gradio
|