Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,40 @@
|
|
1 |
-
import requests
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
|
|
4 |
|
5 |
-
#
|
6 |
-
def classify_image(
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
|
|
19 |
image = gr.Image()
|
20 |
label = gr.Label(num_top_classes=1)
|
21 |
title = "Brain Tumor X-ray Classification"
|
22 |
-
description="Worried about whether your brain scan is normal or not? Upload your x-ray and the algorithm will give you an expert opinion.
|
23 |
-
article="<p style='text-align: center'>Image Classification | Demo Model</p>"
|
24 |
-
demo = gr.Interface(fn=classify_image, inputs=image
|
25 |
|
|
|
26 |
demo.launch(share=True)
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
|
5 |
+
# Define the image classification function
|
6 |
+
def classify_image(image):
|
7 |
+
try:
|
8 |
+
# Convert the Gradio image input (which is a NumPy array) to a PIL image
|
9 |
+
image = Image.fromarray(image)
|
10 |
+
|
11 |
+
# Create the image classification pipeline
|
12 |
+
img_class = pipeline(
|
13 |
+
"image-classification", model="AMfeta99/vit-base-oxford-brain-tumor"
|
14 |
+
)
|
15 |
+
|
16 |
+
# Perform image classification
|
17 |
+
results = img_class(image)
|
18 |
+
|
19 |
+
# Find the result with the highest score
|
20 |
+
max_score_result = max(results, key=lambda x: x['score'])
|
21 |
+
|
22 |
+
# Extract the predicted label
|
23 |
+
predictions = max_score_result['label']
|
24 |
+
|
25 |
+
return predictions
|
26 |
|
27 |
+
except Exception as e:
|
28 |
+
# Handle any errors that occur during classification
|
29 |
+
return f"Error: {str(e)}"
|
30 |
|
31 |
+
# Define the Gradio interface
|
32 |
image = gr.Image()
|
33 |
label = gr.Label(num_top_classes=1)
|
34 |
title = "Brain Tumor X-ray Classification"
|
35 |
+
description = "Worried about whether your brain scan is normal or not? Upload your x-ray and the algorithm will give you an expert opinion. Check out [the original algorithm](https://huggingface.co/AMfeta99/vit-base-oxford-brain-tumor) that this demo is based off of."
|
36 |
+
article = "<p style='text-align: center'>Image Classification | Demo Model</p>"
|
37 |
+
demo = gr.Interface(fn=classify_image, inputs=image, outputs=label, description=description, article=article, title=title)
|
38 |
|
39 |
+
# Launch the Gradio interface
|
40 |
demo.launch(share=True)
|