DawnC commited on
Commit
8df4d67
โ€ข
1 Parent(s): 2940859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +289 -115
app.py CHANGED
@@ -132,6 +132,18 @@ def get_akc_breeds_link(breed):
132
  return f"{base_url}{breed_url}/"
133
 
134
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  async def predict_single_dog(image):
137
  image_tensor = preprocess_image(image)
@@ -142,8 +154,13 @@ async def predict_single_dog(image):
142
  topk_probs, topk_indices = torch.topk(probabilities, k=3)
143
  top1_prob = topk_probs[0][0].item()
144
  topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
145
- topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
146
- return top1_prob, topk_breeds, topk_probs_percent
 
 
 
 
 
147
 
148
 
149
  async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
@@ -198,66 +215,210 @@ def calculate_iou(box1, box2):
198
 
199
 
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  async def process_single_dog(image):
202
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
 
 
203
  if top1_prob < 0.15:
204
  initial_state = {
205
  "explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
206
- "buttons": [],
207
- "show_back": False,
208
  "image": None,
209
  "is_multi_dog": False
210
  }
211
- return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
212
 
213
  breed = topk_breeds[0]
214
- description = get_dog_description(breed)
215
-
216
  if top1_prob >= 0.45:
 
217
  formatted_description = format_description(description, breed)
218
  initial_state = {
219
  "explanation": formatted_description,
220
- "buttons": [],
221
- "show_back": False,
222
  "image": image,
223
  "is_multi_dog": False
224
  }
225
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
 
 
226
  else:
227
- explanation = (
228
- f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
229
- f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
230
- f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
231
- f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
232
- "Click on a button to view more information about the breed."
233
- )
234
- buttons = [
235
- gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
236
- gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
237
- gr.update(visible=True, value=f"More about {topk_breeds[2]}")
238
- ]
239
  initial_state = {
240
- "explanation": explanation,
241
- "buttons": buttons,
242
- "show_back": True,
243
  "image": image,
244
  "is_multi_dog": False
245
  }
246
- return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
247
-
248
 
249
  async def predict(image):
250
  if image is None:
251
- return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
252
 
253
  try:
254
  if isinstance(image, np.ndarray):
255
  image = Image.fromarray(image)
256
 
257
  dogs = await detect_multiple_dogs(image)
258
-
259
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
260
- buttons = []
261
  annotated_image = image.copy()
262
  draw = ImageDraw.Draw(annotated_image)
263
  font = ImageFont.load_default()
@@ -265,89 +426,73 @@ async def predict(image):
265
  dogs_info = ""
266
 
267
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
268
- buttons_html = ""
269
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
270
  color = color_list[i % len(color_list)]
271
  draw.rectangle(box, outline=color, width=3)
272
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
273
 
 
274
  combined_confidence = detection_confidence * top1_prob
 
275
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
276
  dogs_info += f'<h2>Dog {i+1}</h2>'
277
-
278
- if top1_prob >= 0.45:
 
 
 
279
  breed = topk_breeds[0]
280
  description = get_dog_description(breed)
281
  dogs_info += format_description_html(description, breed)
282
-
283
- elif combined_confidence >= 0.15:
284
- dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
285
- for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
286
- prob = float(prob.replace('%', ''))
287
- dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
288
- dogs_info += "</ul>"
289
-
290
- for breed in topk_breeds[:3]:
291
- button_id = f"Dog {i+1}: More about {breed}"
292
- buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
293
- buttons.append(button_id)
294
-
295
  else:
296
- dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
297
-
298
- dogs_info += '</div>'
 
 
 
 
 
 
299
 
300
-
301
- buttons_html = ""
302
-
303
  html_output = f"""
304
  <style>
305
- .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
306
- .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
307
- .breed-buttons {{ margin-top: 10px; }}
308
- .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  </style>
310
  {dogs_info}
311
- """
312
-
313
- if buttons:
314
- html_output += """
315
- <script>
316
- function handle_button_click(button_id) {
317
- const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
318
- if (radio) {
319
- radio.click();
320
- } else {
321
- console.error("Radio button not found:", button_id);
322
- }
323
- }
324
- </script>
325
- """
326
- initial_state = {
327
- "dogs_info": dogs_info,
328
- "buttons": buttons,
329
- "show_back": True,
330
- "image": annotated_image,
331
- "is_multi_dog": len(dogs) > 1,
332
- "html_output": html_output
333
- }
334
- return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
335
- else:
336
- initial_state = {
337
- "dogs_info": dogs_info,
338
- "buttons": [],
339
- "show_back": False,
340
- "image": annotated_image,
341
- "is_multi_dog": len(dogs) > 1,
342
- "html_output": html_output
343
- }
344
- return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
345
 
 
 
 
 
 
 
 
 
346
 
347
  except Exception as e:
348
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
349
  print(error_msg)
350
- return error_msg, None, gr.update(visible=False, choices=[]), None
351
 
352
 
353
  def show_details_html(choice, previous_output, initial_state):
@@ -402,47 +547,76 @@ def go_back(state):
402
  )
403
 
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  with gr.Blocks() as iface:
406
  gr.HTML("<h1 style='text-align: center;'>๐Ÿถ Dog Breed Classifier ๐Ÿ”</h1>")
407
- gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
408
 
409
  with gr.Row():
410
  input_image = gr.Image(label="Upload a dog image", type="pil")
411
  output_image = gr.Image(label="Annotated Image")
412
 
413
- output = gr.HTML(label="Prediction Results")
414
-
415
- breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
416
-
417
- back_button = gr.Button("Back", visible=False)
418
-
419
  initial_state = gr.State()
420
 
421
  input_image.change(
422
  predict,
423
  inputs=input_image,
424
- outputs=[output, output_image, breed_buttons, initial_state]
425
- )
426
-
427
- breed_buttons.change(
428
- show_details_html,
429
- inputs=[breed_buttons, output, initial_state],
430
- outputs=[output, back_button, initial_state]
431
- )
432
-
433
- back_button.click(
434
- go_back,
435
- inputs=[initial_state],
436
- outputs=[output, output_image, breed_buttons, back_button, initial_state]
437
  )
438
 
439
  gr.Examples(
440
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
441
  inputs=input_image
442
  )
443
-
444
  gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
445
 
446
-
447
  if __name__ == "__main__":
448
  iface.launch()
 
132
  return f"{base_url}{breed_url}/"
133
 
134
 
135
+ # async def predict_single_dog(image):
136
+ # image_tensor = preprocess_image(image)
137
+ # with torch.no_grad():
138
+ # output = model(image_tensor)
139
+ # logits = output[0] if isinstance(output, tuple) else output
140
+ # probabilities = F.softmax(logits, dim=1)
141
+ # topk_probs, topk_indices = torch.topk(probabilities, k=3)
142
+ # top1_prob = topk_probs[0][0].item()
143
+ # topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
144
+ # topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
145
+ # return top1_prob, topk_breeds, topk_probs_percent
146
+
147
 
148
  async def predict_single_dog(image):
149
  image_tensor = preprocess_image(image)
 
154
  topk_probs, topk_indices = torch.topk(probabilities, k=3)
155
  top1_prob = topk_probs[0][0].item()
156
  topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
157
+
158
+ # Calculate relative probabilities for display
159
+ raw_probs = [prob.item() for prob in topk_probs[0]]
160
+ sum_probs = sum(raw_probs)
161
+ relative_probs = [f"{(prob/sum_probs * 100):.2f}%" for prob in raw_probs]
162
+
163
+ return top1_prob, topk_breeds, relative_probs
164
 
165
 
166
  async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
 
215
 
216
 
217
 
218
+ # async def process_single_dog(image):
219
+ # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
220
+ # if top1_prob < 0.15:
221
+ # initial_state = {
222
+ # "explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
223
+ # "buttons": [],
224
+ # "show_back": False,
225
+ # "image": None,
226
+ # "is_multi_dog": False
227
+ # }
228
+ # return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
229
+
230
+ # breed = topk_breeds[0]
231
+ # description = get_dog_description(breed)
232
+
233
+ # if top1_prob >= 0.45:
234
+ # formatted_description = format_description(description, breed)
235
+ # initial_state = {
236
+ # "explanation": formatted_description,
237
+ # "buttons": [],
238
+ # "show_back": False,
239
+ # "image": image,
240
+ # "is_multi_dog": False
241
+ # }
242
+ # return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
243
+ # else:
244
+ # explanation = (
245
+ # f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
246
+ # f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
247
+ # f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
248
+ # f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
249
+ # "Click on a button to view more information about the breed."
250
+ # )
251
+ # buttons = [
252
+ # gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
253
+ # gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
254
+ # gr.update(visible=True, value=f"More about {topk_breeds[2]}")
255
+ # ]
256
+ # initial_state = {
257
+ # "explanation": explanation,
258
+ # "buttons": buttons,
259
+ # "show_back": True,
260
+ # "image": image,
261
+ # "is_multi_dog": False
262
+ # }
263
+ # return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
264
+
265
+
266
+ # async def predict(image):
267
+ # if image is None:
268
+ # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
269
+
270
+ # try:
271
+ # if isinstance(image, np.ndarray):
272
+ # image = Image.fromarray(image)
273
+
274
+ # dogs = await detect_multiple_dogs(image)
275
+
276
+ # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
277
+ # buttons = []
278
+ # annotated_image = image.copy()
279
+ # draw = ImageDraw.Draw(annotated_image)
280
+ # font = ImageFont.load_default()
281
+
282
+ # dogs_info = ""
283
+
284
+ # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
285
+ # buttons_html = ""
286
+ # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
287
+ # color = color_list[i % len(color_list)]
288
+ # draw.rectangle(box, outline=color, width=3)
289
+ # draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
290
+
291
+ # combined_confidence = detection_confidence * top1_prob
292
+ # dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
293
+ # dogs_info += f'<h2>Dog {i+1}</h2>'
294
+
295
+ # if top1_prob >= 0.45:
296
+ # breed = topk_breeds[0]
297
+ # description = get_dog_description(breed)
298
+ # dogs_info += format_description_html(description, breed)
299
+
300
+ # elif combined_confidence >= 0.15:
301
+ # dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
302
+ # for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
303
+ # prob = float(prob.replace('%', ''))
304
+ # dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
305
+ # dogs_info += "</ul>"
306
+
307
+ # for breed in topk_breeds[:3]:
308
+ # button_id = f"Dog {i+1}: More about {breed}"
309
+ # buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
310
+ # buttons.append(button_id)
311
+
312
+ # else:
313
+ # dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
314
+
315
+ # dogs_info += '</div>'
316
+
317
+
318
+ # buttons_html = ""
319
+
320
+ # html_output = f"""
321
+ # <style>
322
+ # .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
323
+ # .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
324
+ # .breed-buttons {{ margin-top: 10px; }}
325
+ # .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
326
+ # </style>
327
+ # {dogs_info}
328
+ # """
329
+
330
+ # if buttons:
331
+ # html_output += """
332
+ # <script>
333
+ # function handle_button_click(button_id) {
334
+ # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
335
+ # if (radio) {
336
+ # radio.click();
337
+ # } else {
338
+ # console.error("Radio button not found:", button_id);
339
+ # }
340
+ # }
341
+ # </script>
342
+ # """
343
+ # initial_state = {
344
+ # "dogs_info": dogs_info,
345
+ # "buttons": buttons,
346
+ # "show_back": True,
347
+ # "image": annotated_image,
348
+ # "is_multi_dog": len(dogs) > 1,
349
+ # "html_output": html_output
350
+ # }
351
+ # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
352
+ # else:
353
+ # initial_state = {
354
+ # "dogs_info": dogs_info,
355
+ # "buttons": [],
356
+ # "show_back": False,
357
+ # "image": annotated_image,
358
+ # "is_multi_dog": len(dogs) > 1,
359
+ # "html_output": html_output
360
+ # }
361
+ # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
362
+
363
+
364
+ # except Exception as e:
365
+ # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
366
+ # print(error_msg)
367
+ # return error_msg, None, gr.update(visible=False, choices=[]), None
368
+
369
+
370
+
371
  async def process_single_dog(image):
372
+ top1_prob, topk_breeds, relative_probs = await predict_single_dog(image)
373
+
374
+ # Case 1: Low confidence - unclear image or breed not in dataset
375
  if top1_prob < 0.15:
376
  initial_state = {
377
  "explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
 
 
378
  "image": None,
379
  "is_multi_dog": False
380
  }
381
+ return initial_state["explanation"], None, initial_state
382
 
383
  breed = topk_breeds[0]
384
+
385
+ # Case 2: High confidence - single breed result
386
  if top1_prob >= 0.45:
387
+ description = get_dog_description(breed)
388
  formatted_description = format_description(description, breed)
389
  initial_state = {
390
  "explanation": formatted_description,
 
 
391
  "image": image,
392
  "is_multi_dog": False
393
  }
394
+ return formatted_description, image, initial_state
395
+
396
+ # Case 3: Medium confidence - show top 3 breeds with relative probabilities
397
  else:
398
+ breeds_info = ""
399
+ for i, (breed, prob) in enumerate(zip(topk_breeds, relative_probs)):
400
+ description = get_dog_description(breed)
401
+ formatted_description = format_description(description, breed)
402
+ breeds_info += f"\n\nBreed {i+1}: **{breed}** (Confidence: {prob})\n{formatted_description}"
403
+
 
 
 
 
 
 
404
  initial_state = {
405
+ "explanation": breeds_info,
 
 
406
  "image": image,
407
  "is_multi_dog": False
408
  }
409
+ return breeds_info, image, initial_state
410
+
411
 
412
  async def predict(image):
413
  if image is None:
414
+ return "Please upload an image to start.", None, None
415
 
416
  try:
417
  if isinstance(image, np.ndarray):
418
  image = Image.fromarray(image)
419
 
420
  dogs = await detect_multiple_dogs(image)
 
421
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
 
422
  annotated_image = image.copy()
423
  draw = ImageDraw.Draw(annotated_image)
424
  font = ImageFont.load_default()
 
426
  dogs_info = ""
427
 
428
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
 
 
429
  color = color_list[i % len(color_list)]
430
  draw.rectangle(box, outline=color, width=3)
431
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
432
 
433
+ top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
434
  combined_confidence = detection_confidence * top1_prob
435
+
436
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
437
  dogs_info += f'<h2>Dog {i+1}</h2>'
438
+
439
+ if combined_confidence < 0.15:
440
+ dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
441
+
442
+ elif top1_prob >= 0.45:
443
  breed = topk_breeds[0]
444
  description = get_dog_description(breed)
445
  dogs_info += format_description_html(description, breed)
446
+
 
 
 
 
 
 
 
 
 
 
 
 
447
  else:
448
+ dogs_info += "<h3>Top 3 possible breeds:</h3>"
449
+ for breed, prob in zip(topk_breeds, relative_probs):
450
+ description = get_dog_description(breed)
451
+ dogs_info += f"<div class='breed-section'>"
452
+ dogs_info += f"<h4>{breed} (Confidence: {prob})</h4>"
453
+ dogs_info += format_description_html(description, breed)
454
+ dogs_info += "</div>"
455
+
456
+ dogs_info += '</div>'
457
 
 
 
 
458
  html_output = f"""
459
  <style>
460
+ .dog-info {{
461
+ border: 1px solid #ddd;
462
+ margin-bottom: 20px;
463
+ padding: 15px;
464
+ border-radius: 5px;
465
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
466
+ }}
467
+ .dog-info h2 {{
468
+ background-color: #f0f0f0;
469
+ padding: 10px;
470
+ margin: -15px -15px 15px -15px;
471
+ border-radius: 5px 5px 0 0;
472
+ }}
473
+ .breed-section {{
474
+ margin-bottom: 20px;
475
+ padding: 10px;
476
+ background-color: #f8f8f8;
477
+ border-radius: 5px;
478
+ }}
479
  </style>
480
  {dogs_info}
481
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482
 
483
+ initial_state = {
484
+ "dogs_info": dogs_info,
485
+ "image": annotated_image,
486
+ "is_multi_dog": len(dogs) > 1,
487
+ "html_output": html_output
488
+ }
489
+
490
+ return html_output, annotated_image, initial_state
491
 
492
  except Exception as e:
493
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
494
  print(error_msg)
495
+ return error_msg, None, None
496
 
497
 
498
  def show_details_html(choice, previous_output, initial_state):
 
547
  )
548
 
549
 
550
+ # with gr.Blocks() as iface:
551
+ # gr.HTML("<h1 style='text-align: center;'>๐Ÿถ Dog Breed Classifier ๐Ÿ”</h1>")
552
+ # gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
553
+
554
+ # with gr.Row():
555
+ # input_image = gr.Image(label="Upload a dog image", type="pil")
556
+ # output_image = gr.Image(label="Annotated Image")
557
+
558
+ # output = gr.HTML(label="Prediction Results")
559
+
560
+ # breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
561
+
562
+ # back_button = gr.Button("Back", visible=False)
563
+
564
+ # initial_state = gr.State()
565
+
566
+ # input_image.change(
567
+ # predict,
568
+ # inputs=input_image,
569
+ # outputs=[output, output_image, breed_buttons, initial_state]
570
+ # )
571
+
572
+ # breed_buttons.change(
573
+ # show_details_html,
574
+ # inputs=[breed_buttons, output, initial_state],
575
+ # outputs=[output, back_button, initial_state]
576
+ # )
577
+
578
+ # back_button.click(
579
+ # go_back,
580
+ # inputs=[initial_state],
581
+ # outputs=[output, output_image, breed_buttons, back_button, initial_state]
582
+ # )
583
+
584
+ # gr.Examples(
585
+ # examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
586
+ # inputs=input_image
587
+ # )
588
+
589
+ # gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
590
+
591
+
592
+ # if __name__ == "__main__":
593
+ # iface.launch()
594
+
595
+
596
+
597
  with gr.Blocks() as iface:
598
  gr.HTML("<h1 style='text-align: center;'>๐Ÿถ Dog Breed Classifier ๐Ÿ”</h1>")
599
+ gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed and provide detailed information!</p>")
600
 
601
  with gr.Row():
602
  input_image = gr.Image(label="Upload a dog image", type="pil")
603
  output_image = gr.Image(label="Annotated Image")
604
 
605
+ output = gr.HTML(label="Prediction Results")
 
 
 
 
 
606
  initial_state = gr.State()
607
 
608
  input_image.change(
609
  predict,
610
  inputs=input_image,
611
+ outputs=[output, output_image, initial_state]
 
 
 
 
 
 
 
 
 
 
 
 
612
  )
613
 
614
  gr.Examples(
615
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
616
  inputs=input_image
617
  )
618
+
619
  gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
620
 
 
621
  if __name__ == "__main__":
622
  iface.launch()