Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -418,7 +418,7 @@ async def process_single_dog(image):
|
|
418 |
|
419 |
async def predict(image):
|
420 |
if image is None:
|
421 |
-
return "Please upload an image to start.", None,
|
422 |
|
423 |
try:
|
424 |
if isinstance(image, np.ndarray):
|
@@ -448,7 +448,7 @@ async def predict(image):
|
|
448 |
dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
449 |
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
450 |
explanations.append(dog_explanation)
|
451 |
-
buttons.extend([
|
452 |
else:
|
453 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
454 |
|
@@ -463,7 +463,7 @@ async def predict(image):
|
|
463 |
"is_multi_dog": len(dogs) > 1,
|
464 |
"dogs_info": explanations
|
465 |
}
|
466 |
-
return final_explanation, annotated_image,
|
467 |
else:
|
468 |
initial_state = {
|
469 |
"explanation": final_explanation,
|
@@ -473,30 +473,30 @@ async def predict(image):
|
|
473 |
"is_multi_dog": len(dogs) > 1,
|
474 |
"dogs_info": explanations
|
475 |
}
|
476 |
-
return final_explanation, annotated_image,
|
477 |
|
478 |
except Exception as e:
|
479 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
480 |
logger.error(error_msg)
|
481 |
-
return error_msg, None,
|
|
|
482 |
|
483 |
def show_details(choice, previous_output, initial_state):
|
484 |
if not choice:
|
485 |
return previous_output, gr.update(visible=True), initial_state
|
486 |
|
487 |
try:
|
488 |
-
|
489 |
description = get_dog_description(breed)
|
490 |
formatted_description = format_description(description, breed)
|
491 |
|
492 |
-
# Save current description and original button state
|
493 |
initial_state["current_description"] = formatted_description
|
494 |
initial_state["original_buttons"] = initial_state.get("buttons", [])
|
495 |
|
496 |
return formatted_description, gr.update(visible=True), initial_state
|
497 |
except Exception as e:
|
498 |
error_msg = f"An error occurred while showing details: {e}"
|
499 |
-
|
500 |
return error_msg, gr.update(visible=True), initial_state
|
501 |
|
502 |
def go_back(state):
|
@@ -504,11 +504,12 @@ def go_back(state):
|
|
504 |
return (
|
505 |
state["explanation"],
|
506 |
state["image"],
|
507 |
-
buttons,
|
508 |
-
gr.update(visible=False),
|
509 |
state
|
510 |
)
|
511 |
|
|
|
512 |
with gr.Blocks() as iface:
|
513 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
514 |
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>")
|
@@ -519,9 +520,7 @@ with gr.Blocks() as iface:
|
|
519 |
|
520 |
output = gr.Markdown(label="Prediction Results")
|
521 |
|
522 |
-
|
523 |
-
with button_group:
|
524 |
-
buttons = []
|
525 |
|
526 |
back_button = gr.Button("Back", visible=False)
|
527 |
|
@@ -530,25 +529,19 @@ with gr.Blocks() as iface:
|
|
530 |
input_image.change(
|
531 |
predict,
|
532 |
inputs=input_image,
|
533 |
-
outputs=[output, output_image,
|
534 |
)
|
535 |
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
show_details,
|
542 |
-
inputs=[button, output, initial_state],
|
543 |
-
outputs=[output, back_button, initial_state]
|
544 |
-
)
|
545 |
-
buttons.append(button)
|
546 |
-
return button_group
|
547 |
|
548 |
back_button.click(
|
549 |
go_back,
|
550 |
inputs=[initial_state],
|
551 |
-
outputs=[output, output_image,
|
552 |
)
|
553 |
|
554 |
gr.Examples(
|
|
|
418 |
|
419 |
async def predict(image):
|
420 |
if image is None:
|
421 |
+
return "Please upload an image to start.", None, gr.update(visible=False), None
|
422 |
|
423 |
try:
|
424 |
if isinstance(image, np.ndarray):
|
|
|
448 |
dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
449 |
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
450 |
explanations.append(dog_explanation)
|
451 |
+
buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
|
452 |
else:
|
453 |
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
454 |
|
|
|
463 |
"is_multi_dog": len(dogs) > 1,
|
464 |
"dogs_info": explanations
|
465 |
}
|
466 |
+
return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
467 |
else:
|
468 |
initial_state = {
|
469 |
"explanation": final_explanation,
|
|
|
473 |
"is_multi_dog": len(dogs) > 1,
|
474 |
"dogs_info": explanations
|
475 |
}
|
476 |
+
return final_explanation, annotated_image, gr.update(visible=False), initial_state
|
477 |
|
478 |
except Exception as e:
|
479 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
480 |
logger.error(error_msg)
|
481 |
+
return error_msg, None, gr.update(visible=False), None
|
482 |
+
|
483 |
|
484 |
def show_details(choice, previous_output, initial_state):
|
485 |
if not choice:
|
486 |
return previous_output, gr.update(visible=True), initial_state
|
487 |
|
488 |
try:
|
489 |
+
breed = choice.split("More about ")[-1]
|
490 |
description = get_dog_description(breed)
|
491 |
formatted_description = format_description(description, breed)
|
492 |
|
|
|
493 |
initial_state["current_description"] = formatted_description
|
494 |
initial_state["original_buttons"] = initial_state.get("buttons", [])
|
495 |
|
496 |
return formatted_description, gr.update(visible=True), initial_state
|
497 |
except Exception as e:
|
498 |
error_msg = f"An error occurred while showing details: {e}"
|
499 |
+
logger.error(error_msg)
|
500 |
return error_msg, gr.update(visible=True), initial_state
|
501 |
|
502 |
def go_back(state):
|
|
|
504 |
return (
|
505 |
state["explanation"],
|
506 |
state["image"],
|
507 |
+
gr.update(visible=True, choices=buttons),
|
508 |
+
gr.update(visible=False),
|
509 |
state
|
510 |
)
|
511 |
|
512 |
+
# ไฟฎๆน Gradio ็้ข็ตๆง
|
513 |
with gr.Blocks() as iface:
|
514 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
515 |
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>")
|
|
|
520 |
|
521 |
output = gr.Markdown(label="Prediction Results")
|
522 |
|
523 |
+
breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
|
|
|
|
|
524 |
|
525 |
back_button = gr.Button("Back", visible=False)
|
526 |
|
|
|
529 |
input_image.change(
|
530 |
predict,
|
531 |
inputs=input_image,
|
532 |
+
outputs=[output, output_image, breed_buttons, initial_state]
|
533 |
)
|
534 |
|
535 |
+
breed_buttons.change(
|
536 |
+
show_details,
|
537 |
+
inputs=[breed_buttons, output, initial_state],
|
538 |
+
outputs=[output, back_button, initial_state]
|
539 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
540 |
|
541 |
back_button.click(
|
542 |
go_back,
|
543 |
inputs=[initial_state],
|
544 |
+
outputs=[output, output_image, breed_buttons, back_button, initial_state]
|
545 |
)
|
546 |
|
547 |
gr.Examples(
|