File size: 1,804 Bytes
369329c
 
ef24465
369329c
ef24465
 
 
 
 
1ceb719
ef24465
 
520dfc5
ef24465
1ceb719
ef24465
 
1ceb719
ef24465
 
1ceb719
ef24465
 
 
1ceb719
 
 
 
 
 
 
ef24465
 
 
369329c
ef24465
369329c
 
541f337
abec5ff
ef24465
1ceb719
 
 
65af555
1ceb719
65af555
369329c
ef24465
50eefdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
from transformers import pipeline
from PIL import Image

# Define the image classification function
def classify_image(image):
    try:
        # Convert the Gradio image input (which is a NumPy array) to a PIL image
        image = Image.fromarray(image)

        # Create the image classification pipeline
        img_class = pipeline(
            "image-classification", model="AMfeta99/vit-base-oxford-brain-tumor_x-ray"
        )

        # Perform image classification
        results = img_class(image)

        # Find the result with the highest score
        max_score_result = max(results, key=lambda x: x['score'])

        # Extract the predicted label
        predictions = max_score_result['label']
        
        if predictions==1:
          text_pred='Tumor'
        else:
          text_pred='Normal'

        return text_pred

    except Exception as e:
        # Handle any errors that occur during classification
        return f"Error: {str(e)}"

# Define the Gradio interface
image = gr.Image()
label = gr.Label(num_top_classes=1)
title = "Brain Tumor Classification"
description = "Worried about whether your brain scan is normal or not? Upload your brain scan 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 of."
article = "<p style='text-align: center'>Image Classification | Demo Model</p>"


# Prepare examples with loaded images
examples = ["Normal_0.jpg", "Normal_1.jpg", "Normal_2.jpg", "Tumor_0.jpg", "Tumor_1.jpg", "Tumor_2.jpg"]

demo = gr.Interface(fn=classify_image, inputs=image, outputs=label, description=description, article=article, title=title, examples=examples)

# Launch the Gradio interface
demo.launch(share=True)