AMfeta99 commited on
Commit
1ceb719
1 Parent(s): 08aeab4

add examples samples

Browse files
Files changed (1) hide show
  1. app.py +35 -8
app.py CHANGED
@@ -1,29 +1,49 @@
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)}"
@@ -34,7 +54,14 @@ 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)
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
+ import os
5
+
6
+ def load_images_from_current_directory():
7
+ images = []
8
+ current_directory = os.getcwd()
9
+ for filename in os.listdir(current_directory):
10
+ if filename.endswith(".jpg") or filename.endswith(".png"):
11
+ img_path = os.path.join(current_directory, filename)
12
+ img = Image.open(img_path)
13
+ if img is not None:
14
+ images.append(img)
15
+ return images
16
+
17
+ # Example: Load images from the current directory
18
+ example_images = load_images_from_current_directory()
19
 
20
  # Define the image classification function
21
  def classify_image(image):
22
  try:
23
  # Convert the Gradio image input (which is a NumPy array) to a PIL image
24
  image = Image.fromarray(image)
25
+
26
  # Create the image classification pipeline
27
  img_class = pipeline(
28
  "image-classification", model="AMfeta99/vit-base-oxford-brain-tumor"
29
  )
30
+
31
  # Perform image classification
32
  results = img_class(image)
33
+
34
  # Find the result with the highest score
35
  max_score_result = max(results, key=lambda x: x['score'])
36
+
37
  # Extract the predicted label
38
  predictions = max_score_result['label']
39
 
40
+ if predictions==1:
41
+ text_pred='Tumor'
42
+ else:
43
+ text_pred='Normal'
44
+
45
+ return text_pred
46
+
47
  except Exception as e:
48
  # Handle any errors that occur during classification
49
  return f"Error: {str(e)}"
 
54
  title = "Brain Tumor X-ray Classification"
55
  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."
56
  article = "<p style='text-align: center'>Image Classification | Demo Model</p>"
57
+
58
+
59
+ # Prepare examples with loaded images
60
+ examples = []
61
+ for img in example_images:
62
+ examples.append([np.array(img), os.path.basename(os.path.splitext(img.filename)[0])])
63
+
64
+ demo = gr.Interface(fn=classify_image, inputs=image, outputs=label, description=description, article=article, title=title, examples=examples)
65
 
66
  # Launch the Gradio interface
67
+ demo.launch()