AMfeta99 commited on
Commit
ef24465
1 Parent(s): 369329c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -1,26 +1,40 @@
1
- import requests
2
  import gradio as gr
3
  from transformers import pipeline
 
4
 
5
- # make the image classification pipeline
6
- def classify_image(test_sample):
7
- img_class= pipeline(
8
- "image-classification", model="AMfeta99/vit-base-oxford-brain-tumor"
9
- )
10
- results = img_class(our_dataset['test']['image'][0])
11
- max_score_result = max(results, key=lambda x: x['score'])
12
-
13
- predictions = max_score_result['label']
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- return predictions
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. Check out [the original algorithm](https://huggingface.co/AMfeta99/vit-base-oxford-brain-tumor) that this demo is based off of."
23
- article="<p style='text-align: center'>Image Classification | Demo Model</p>"
24
- demo = gr.Interface(fn=classify_image, inputs=image , outputs=label, description=description,article=article,title=title)
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)