AIOmarRehan commited on
Commit
2739ec2
·
verified ·
1 Parent(s): cc7d279

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -4,12 +4,15 @@ from PIL import Image
4
  from model import predict
5
  from datasets import load_dataset
6
 
7
- # Load dataset (NO streaming allows len() and indexing)
8
- dataset = load_dataset("AIOmarRehan/AnimalsDataset", split="train")
 
 
 
 
9
 
10
  def classify_image(img: Image.Image):
11
 
12
- # Handle empty input safely
13
  if img is None:
14
  return "No image uploaded", 0, {}
15
 
@@ -21,14 +24,16 @@ def classify_image(img: Image.Image):
21
  {k: round(v, 3) for k, v in probs.items()}
22
  )
23
 
24
- # Pick a random example
25
  def random_example():
26
 
27
- idx = random.randint(0, len(dataset) - 1)
28
- item = dataset[idx]
29
 
30
  img = item["image"].convert("RGB")
31
  label = item["label"]
 
 
32
  label_str = dataset.features["label"].int2str(label)
33
 
34
  return img, label_str
@@ -43,18 +48,15 @@ with demo:
43
  input_img = gr.Image(type="pil", label="Upload an image")
44
  rand_img = gr.Button("Random Dataset Image")
45
 
46
- with gr.Row():
47
- pred_btn = gr.Button("Predict")
48
 
49
  output_label = gr.Label(label="Predicted Class")
50
  output_conf = gr.Number(label="Confidence")
51
  output_probs = gr.JSON(label="All Probabilities")
52
 
53
- # Display random dataset sample
54
  rand_display = gr.Image(type="pil", label="Random Dataset Sample")
55
  rand_label = gr.Textbox(label="Sample Label")
56
 
57
- # Actions
58
  pred_btn.click(
59
  classify_image,
60
  inputs=input_img,
 
4
  from model import predict
5
  from datasets import load_dataset
6
 
7
+ # Streaming dataset (SAFE for large datasets)
8
+ dataset = load_dataset(
9
+ "AIOmarRehan/AnimalsDataset",
10
+ split="train",
11
+ streaming=True
12
+ )
13
 
14
  def classify_image(img: Image.Image):
15
 
 
16
  if img is None:
17
  return "No image uploaded", 0, {}
18
 
 
24
  {k: round(v, 3) for k, v in probs.items()}
25
  )
26
 
27
+ # Random example for streaming dataset
28
  def random_example():
29
 
30
+ # Shuffle streaming buffer then take first item
31
+ item = next(iter(dataset.shuffle(buffer_size=100)))
32
 
33
  img = item["image"].convert("RGB")
34
  label = item["label"]
35
+
36
+ # streaming dataset keeps features
37
  label_str = dataset.features["label"].int2str(label)
38
 
39
  return img, label_str
 
48
  input_img = gr.Image(type="pil", label="Upload an image")
49
  rand_img = gr.Button("Random Dataset Image")
50
 
51
+ pred_btn = gr.Button("Predict")
 
52
 
53
  output_label = gr.Label(label="Predicted Class")
54
  output_conf = gr.Number(label="Confidence")
55
  output_probs = gr.JSON(label="All Probabilities")
56
 
 
57
  rand_display = gr.Image(type="pil", label="Random Dataset Sample")
58
  rand_label = gr.Textbox(label="Sample Label")
59
 
 
60
  pred_btn.click(
61
  classify_image,
62
  inputs=input_img,