File size: 38,478 Bytes
dd5ec97 86680d1 dd5ec97 31f2fc3 dd5ec97 88b9f71 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 |
"""
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ ๐ CISC 121 - OOP Sorting & Searching Visualizer โ
โ โ
โ Queen's University - Introduction to Computing Science I โ
โ โ
โ This application demonstrates Object-Oriented Programming concepts โ
โ through interactive visualization of sorting and searching algorithms. โ
โ โ
โ HOW TO RUN: python app_oop_gradio.py โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ PHASE 5: Gradio UI
This is the final phase - creating a user-friendly web interface that:
1. Allows capturing/uploading gesture images
2. Displays the image list with gesture recognition
3. Lets users run sorting/searching algorithms
4. Visualizes each step of the algorithm
The UI demonstrates COMPOSITION - the GradioApp class composes:
- ImageList (data management)
- SortingAlgorithm / SearchAlgorithm (algorithm execution)
- Visualizer (step-by-step display)
"""
# ==============================================================================
# IMPORTS
# ==============================================================================
import gradio as gr
from PIL import Image
import os
from typing import List, Tuple, Optional
# Import our OOP package
from oop_sorting_teaching import (
# Models
GestureRanking,
GestureImage,
ImageList,
StepType,
Step,
# Sorting
BubbleSort,
MergeSort,
QuickSort,
PivotStrategy,
PartitionScheme,
# Searching
LinearSearch,
BinarySearch,
# Visualization
Visualizer,
VisualizationConfig,
RendererFactory,
)
# Try to import transformers for gesture recognition
try:
from transformers import pipeline
CLASSIFIER_AVAILABLE = True
except ImportError:
CLASSIFIER_AVAILABLE = False
print("โ ๏ธ transformers not installed. Using manual gesture selection.")
# ==============================================================================
# CONFIGURATION
# ==============================================================================
MODEL_NAME = "dima806/hand_gestures_image_detection"
HF_TOKEN = os.environ.get("HF_TOKEN", None)
APP_TITLE = "## ๐ CISC 121 - OOP Sorting & Searching Visualizer"
APP_DESCRIPTION = """
**Learn Object-Oriented Programming through Algorithm Visualization!**
This app demonstrates key OOP concepts:
- ๐ฆ **Classes & Objects**: GestureImage, ImageList, Algorithms
- ๐ญ **Inheritance**: All sorting algorithms inherit from SortingAlgorithm
- ๐ **Polymorphism**: Swap between algorithms seamlessly
- ๐ญ **Factory Pattern**: RendererFactory creates the right visualizer
**How to use:**
1. **Add images** using the buttons below (capture or manual)
2. **View your list** of gesture images
3. **Run an algorithm** to see step-by-step visualization
4. **Navigate steps** to understand how the algorithm works
"""
# ==============================================================================
# GRADIO APP CLASS
# ==============================================================================
class GradioApp:
"""
๐ CONCEPT: Composition
The GradioApp class COMPOSES (contains) other objects:
- ImageList for managing captured images
- Visualizer for displaying algorithm steps
- Classifier for gesture recognition (if available)
This is the Controller in MVC pattern - it coordinates
between user interface (View) and data/logic (Model).
"""
def __init__(self):
"""Initialize the application state."""
self.image_list = ImageList()
self.visualizer = Visualizer(VisualizationConfig(
show_statistics=True,
show_legend=True,
image_size=60
))
self._capture_count = 0
# Initialize classifier if available
self.classifier = None
if CLASSIFIER_AVAILABLE:
try:
self.classifier = pipeline(
"image-classification",
model=MODEL_NAME,
token=HF_TOKEN
)
print(f"โ
Loaded model: {MODEL_NAME}")
except Exception as e:
print(f"โ ๏ธ Could not load model: {e}")
# -------------------------------------------------------------------------
# Image Management Methods
# -------------------------------------------------------------------------
def add_manual_gesture(self, gesture_name: str) -> Tuple[str, str]:
"""
Add a gesture image manually (without camera).
Returns:
Tuple of (image_list_html, status_message)
"""
if not gesture_name:
return self._render_image_list(), "โ ๏ธ Please select a gesture"
self._capture_count += 1
self.image_list.add_new(gesture_name)
return (
self._render_image_list(),
f"โ
Added {GestureRanking.get_emoji(gesture_name)} {gesture_name} (#{self._capture_count})"
)
def add_from_image(self, image: Image.Image) -> Tuple[str, str]:
"""
Add a gesture from an uploaded/captured image.
Uses AI classification if available, otherwise prompts for manual selection.
"""
if image is None:
return self._render_image_list(), "โ ๏ธ No image provided"
if self.classifier:
try:
# Classify the image
results = self.classifier(image)
if results:
top_result = results[0]
gesture_name = top_result['label'].lower()
confidence = top_result['score']
self._capture_count += 1
img = GestureImage.create_from_prediction(
gesture_name=gesture_name,
capture_id=self._capture_count,
image=image,
confidence=confidence
)
self.image_list._save_state() # Save before modifying
self.image_list._images.append(img)
return (
self._render_image_list(),
f"โ
Detected: {img.emoji} {gesture_name} ({confidence:.1%} confidence)"
)
except Exception as e:
return self._render_image_list(), f"โ ๏ธ Classification error: {e}"
return self._render_image_list(), "โ ๏ธ No classifier available. Use manual gesture selection."
def remove_image(self, index: int) -> Tuple[str, str]:
"""Remove an image at the given index."""
if 0 <= index < len(self.image_list):
removed = self.image_list[index]
self.image_list.remove(index)
return self._render_image_list(), f"โ
Removed {removed}"
return self._render_image_list(), "โ ๏ธ Invalid index"
def shuffle_images(self) -> Tuple[str, str]:
"""Shuffle the image list."""
self.image_list.shuffle()
return self._render_image_list(), "๐ Shuffled!"
def clear_images(self) -> Tuple[str, str]:
"""Clear all images."""
count = len(self.image_list)
self.image_list.clear()
self._capture_count = 0
self.visualizer.reset()
return self._render_image_list(), f"๐๏ธ Cleared {count} images"
def undo_action(self) -> Tuple[str, str]:
"""Undo the last action."""
if self.image_list.undo():
return self._render_image_list(), "โฉ๏ธ Undone!"
return self._render_image_list(), "โ ๏ธ Nothing to undo"
def add_sample_data(self) -> Tuple[str, str]:
"""Add sample data for testing."""
gestures = ['fist', 'peace', 'like', 'peace', 'ok', 'fist']
for g in gestures:
self._capture_count += 1
self.image_list.add_new(g)
return self._render_image_list(), f"โ
Added {len(gestures)} sample gestures"
def add_instability_demo(self) -> Tuple[str, str]:
"""
Add data specifically designed to demonstrate Quick Sort instability.
๐ EDUCATIONAL PURPOSE:
This creates a scenario where Quick Sort will reorder equal elements,
demonstrating that it's an UNSTABLE sorting algorithm.
Setup: [โ๏ธโ] [โ๏ธโ] [โ๏ธโ] [โโ]
After Quick Sort: The peace signs may be reordered (e.g., โ,โ,โ)
After Bubble/Merge Sort: Order preserved (โ,โ,โ)
"""
self.clear_images()
# Three peace signs followed by a lower-ranked fist
demo_gestures = ['peace', 'peace', 'peace', 'fist']
for g in demo_gestures:
self._capture_count += 1
self.image_list.add_new(g)
return (
self._render_image_list(),
"๐ Instability Demo: [โ๏ธโ][โ๏ธโ][โ๏ธโ][โโ]\n"
"Try Quick Sort vs Bubble Sort - watch the subscript order!"
)
def add_worst_case_demo(self) -> Tuple[str, str]:
"""
Add already-sorted data to demonstrate worst-case for Quick Sort.
๐ EDUCATIONAL PURPOSE:
When data is already sorted and we use First Pivot strategy,
Quick Sort degrades to O(nยฒ) - its worst case!
"""
self.clear_images()
# Sorted order: fist(1) < peace(2) < like(3) < ok(4) < call(5)
sorted_gestures = ['fist', 'peace', 'like', 'ok', 'call']
for g in sorted_gestures:
self._capture_count += 1
self.image_list.add_new(g)
return (
self._render_image_list(),
"๐ Worst-Case Demo: Already sorted data!\n"
"Quick Sort with First Pivot โ O(nยฒ)\n"
"Try Median-of-3 or Random pivot to see the difference."
)
def add_binary_search_demo(self) -> Tuple[str, str]:
"""
Add sorted data for binary search demonstration.
๐ EDUCATIONAL PURPOSE:
Binary search requires sorted data. This preset shows
how O(log n) is much faster than O(n) linear search.
"""
self.clear_images()
# Create larger sorted dataset for more dramatic comparison
gestures = ['fist', 'fist', 'peace', 'peace', 'like', 'like',
'ok', 'ok', 'call', 'call', 'palm', 'palm']
for g in gestures:
self._capture_count += 1
self.image_list.add_new(g)
return (
self._render_image_list(),
"๐ Search Demo: 12 sorted elements\n"
"Linear Search: up to 12 comparisons\n"
"Binary Search: at most 4 comparisons (logโ12 โ 3.6)"
)
# -------------------------------------------------------------------------
# Algorithm Execution Methods
# -------------------------------------------------------------------------
def run_sort(self, algorithm_name: str, pivot_strategy: str = "first",
partition_scheme: str = "2-way") -> Tuple[str, str, str]:
"""
Run a sorting algorithm on the image list.
Returns:
Tuple of (visualization_html, image_list_html, status_message)
"""
if len(self.image_list) < 2:
return (
self.visualizer.render_current(),
self._render_image_list(),
"โ ๏ธ Need at least 2 images to sort"
)
# Create the algorithm instance
if algorithm_name == "Bubble Sort":
algo = BubbleSort()
elif algorithm_name == "Merge Sort":
algo = MergeSort()
elif algorithm_name == "Quick Sort":
# Map string to enum
pivot_map = {
"first": PivotStrategy.FIRST,
"last": PivotStrategy.LAST,
"median": PivotStrategy.MEDIAN_OF_THREE,
"random": PivotStrategy.RANDOM,
}
partition_map = {
"2-way": PartitionScheme.TWO_WAY,
"3-way": PartitionScheme.THREE_WAY,
}
algo = QuickSort(
pivot_strategy=pivot_map.get(pivot_strategy, PivotStrategy.FIRST),
partition_scheme=partition_map.get(partition_scheme, PartitionScheme.TWO_WAY)
)
else:
return (
self.visualizer.render_current(),
self._render_image_list(),
f"โ ๏ธ Unknown algorithm: {algorithm_name}"
)
# Get data copy and run algorithm
data = list(self.image_list)
sorted_data, steps = algo.run_full(data)
# Load into visualizer
self.visualizer.load_steps(steps, sorted_data, algo.name)
# Update the image list to sorted order
self.image_list._save_state() # Save before modifying
self.image_list._images = list(sorted_data)
return (
self.visualizer.render_current(),
self._render_image_list(),
f"โ
{algo.name}: {len(steps)} steps"
)
def run_search(self, algorithm_name: str, target_index: int) -> Tuple[str, str]:
"""
Run a search algorithm.
Args:
algorithm_name: "Linear Search" or "Binary Search"
target_index: Index of the target element to search for
Returns:
Tuple of (visualization_html, status_message)
"""
if len(self.image_list) < 1:
return self.visualizer.render_current(), "โ ๏ธ Need at least 1 image to search"
if not (0 <= target_index < len(self.image_list)):
return self.visualizer.render_current(), "โ ๏ธ Invalid target index"
data = list(self.image_list)
target = data[target_index]
# For binary search, we need sorted data
if algorithm_name == "Binary Search":
if not self.image_list.is_sorted():
return (
self.visualizer.render_current(),
"โ ๏ธ Binary Search requires sorted data! Run a sort first."
)
algo = BinarySearch(variant="iterative")
else:
algo = LinearSearch()
# Run the search
result_index, steps = algo.run_full(data, target)
# Load into visualizer
self.visualizer.load_steps(steps, data, algo.name)
if result_index is not None:
status = f"โ
{algo.name}: Found {target} at index {result_index}"
else:
status = f"โ {algo.name}: {target} not found"
return self.visualizer.render_current(), status
# -------------------------------------------------------------------------
# Visualization Navigation Methods
# -------------------------------------------------------------------------
def viz_next(self) -> str:
"""Go to next visualization step."""
return self.visualizer.next_step()
def viz_prev(self) -> str:
"""Go to previous visualization step."""
return self.visualizer.prev_step()
def viz_start(self) -> str:
"""Go to first step."""
return self.visualizer.go_to_start()
def viz_end(self) -> str:
"""Go to last step."""
return self.visualizer.go_to_end()
def viz_goto(self, step: int) -> str:
"""Go to a specific step."""
return self.visualizer.go_to_step(int(step) - 1) # Convert to 0-based
# -------------------------------------------------------------------------
# Rendering Methods
# -------------------------------------------------------------------------
def _render_image_list(self) -> str:
"""Render the current image list as HTML."""
if len(self.image_list) == 0:
return """
<div style="
text-align: center;
padding: 40px;
color: #666;
background: #f8f9fa;
border-radius: 12px;
border: 2px dashed #ddd;
">
<div style="font-size: 48px; margin-bottom: 15px;">๐ท</div>
<h3 style="margin: 0 0 10px 0;">No Images Yet</h3>
<p style="margin: 0;">Add gestures using the buttons above!</p>
</div>
"""
# Build image cards
cards = []
for i, img in enumerate(self.image_list):
card = f"""
<div style="
display: inline-flex;
flex-direction: column;
align-items: center;
margin: 6px;
padding: 12px;
border-radius: 10px;
background: white;
border: 2px solid #ddd;
min-width: 70px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
">
<div style="font-size: 32px; margin-bottom: 4px;">{img.emoji}</div>
<div style="font-size: 11px; color: #666;">โ{img.capture_id}โ</div>
<div style="font-size: 10px; color: #999;">rank {img.rank}</div>
<div style="font-size: 9px; color: #aaa; margin-top: 4px;">[{i}]</div>
</div>
"""
cards.append(card)
# Analysis
analysis = self.image_list.get_analysis()
is_sorted = "โ
Sorted" if self.image_list.is_sorted() else "โ Not Sorted"
return f"""
<div style="
background: linear-gradient(135deg, #002D62 0%, #9B2335 100%);
color: white;
padding: 15px;
border-radius: 12px 12px 0 0;
">
<div style="display: flex; justify-content: space-between; align-items: center;">
<strong>Image List ({len(self.image_list)} items)</strong>
<span>{is_sorted}</span>
</div>
</div>
<div style="
background: #f8f9fa;
padding: 15px;
border-radius: 0 0 12px 12px;
border: 1px solid #ddd;
border-top: none;
">
<div style="
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 4px;
">
{''.join(cards)}
</div>
<div style="
margin-top: 15px;
padding-top: 10px;
border-top: 1px solid #ddd;
font-size: 12px;
color: #666;
text-align: center;
">
{analysis}
</div>
</div>
"""
# -------------------------------------------------------------------------
# Create Gradio UI
# -------------------------------------------------------------------------
def create_ui(self) -> gr.Blocks:
"""
Create the Gradio interface.
๐ CONCEPT: Builder Pattern (light version)
We build up the UI component by component, each with its
own responsibility. The final result is a complete interface.
"""
with gr.Blocks() as demo:
# Header
gr.Markdown(APP_TITLE)
gr.Markdown(APP_DESCRIPTION)
with gr.Tabs():
# ============================================================
# TAB 1: Image Management
# ============================================================
with gr.TabItem("๐ท Capture & Manage"):
with gr.Row():
# Left column: Add images
with gr.Column(scale=1):
gr.Markdown("### Add Gestures")
# Manual gesture selection
gesture_dropdown = gr.Dropdown(
choices=GestureRanking.get_all_gestures(),
label="Select Gesture",
info="Choose a gesture to add"
)
add_btn = gr.Button("โ Add Gesture", variant="primary")
gr.Markdown("---")
# Image upload
image_input = gr.Image(
label="Upload Image",
type="pil",
sources=["upload", "webcam"]
)
classify_btn = gr.Button("๐ Classify & Add")
gr.Markdown("---")
# Quick actions
with gr.Row():
sample_btn = gr.Button("๐ Add Samples")
shuffle_btn = gr.Button("๐ Shuffle")
with gr.Row():
undo_btn = gr.Button("โฉ๏ธ Undo")
clear_btn = gr.Button("๐๏ธ Clear", variant="stop")
gr.Markdown("---")
# Educational demos
gr.Markdown("### ๐ Educational Demos")
instability_btn = gr.Button(
"โ ๏ธ Instability Demo",
variant="secondary"
)
worst_case_btn = gr.Button(
"๐ Worst-Case Demo",
variant="secondary"
)
search_demo_btn = gr.Button(
"๐ Search Demo",
variant="secondary"
)
# Right column: Image list display
with gr.Column(scale=2):
gr.Markdown("### Current Image List")
image_list_display = gr.HTML(
value=self._render_image_list()
)
status_msg = gr.Textbox(
label="Status",
interactive=False
)
# Wire up events for Tab 1
add_btn.click(
fn=self.add_manual_gesture,
inputs=[gesture_dropdown],
outputs=[image_list_display, status_msg]
)
classify_btn.click(
fn=self.add_from_image,
inputs=[image_input],
outputs=[image_list_display, status_msg]
)
sample_btn.click(
fn=self.add_sample_data,
outputs=[image_list_display, status_msg]
)
shuffle_btn.click(
fn=self.shuffle_images,
outputs=[image_list_display, status_msg]
)
undo_btn.click(
fn=self.undo_action,
outputs=[image_list_display, status_msg]
)
clear_btn.click(
fn=self.clear_images,
outputs=[image_list_display, status_msg]
)
instability_btn.click(
fn=self.add_instability_demo,
outputs=[image_list_display, status_msg]
)
worst_case_btn.click(
fn=self.add_worst_case_demo,
outputs=[image_list_display, status_msg]
)
search_demo_btn.click(
fn=self.add_binary_search_demo,
outputs=[image_list_display, status_msg]
)
# ============================================================
# TAB 2: Sorting Algorithms
# ============================================================
with gr.TabItem("๐ Sorting"):
with gr.Row():
# Left: Algorithm selection
with gr.Column(scale=1):
gr.Markdown("### Select Algorithm")
sort_algo = gr.Radio(
choices=["Bubble Sort", "Merge Sort", "Quick Sort"],
value="Bubble Sort",
label="Algorithm",
info="Each has different time complexity and stability"
)
# Educational info accordion
with gr.Accordion("๐ Algorithm Info", open=False):
gr.Markdown("""
**Bubble Sort** - O(nยฒ) average, O(n) best
- โ
Stable (preserves order of equal elements)
- Simple but slow for large lists
- Best when: Nearly sorted data
**Merge Sort** - O(n log n) always
- โ
Stable
- Consistent performance
- Uses extra memory for merging
**Quick Sort** - O(n log n) average, O(nยฒ) worst
- โ Unstable (may reorder equal elements)
- Fast in practice, in-place
- Best when: Random data, good pivot
""")
# Quick Sort options (only shown when Quick Sort selected)
with gr.Group() as quicksort_options:
gr.Markdown("**Quick Sort Options**")
pivot_strategy = gr.Radio(
choices=["first", "last", "median", "random"],
value="first",
label="Pivot Strategy",
info="Median/Random avoid worst-case O(nยฒ)"
)
partition_scheme = gr.Radio(
choices=["2-way", "3-way"],
value="2-way",
label="Partition Scheme",
info="3-way handles duplicates better"
)
run_sort_btn = gr.Button("โถ๏ธ Run Sort", variant="primary", size="lg")
gr.Markdown("---")
gr.Markdown("### Current List")
sort_list_display = gr.HTML(value=self._render_image_list())
# Right: Visualization
with gr.Column(scale=2):
gr.Markdown("### Visualization")
sort_viz_display = gr.HTML(
value=self.visualizer.render_current()
)
# Navigation controls
with gr.Row():
viz_start_btn = gr.Button("โฎ๏ธ Start")
viz_prev_btn = gr.Button("โ๏ธ Prev")
step_slider = gr.Slider(
minimum=1,
maximum=100,
step=1,
value=1,
label="Step"
)
viz_next_btn = gr.Button("Next โถ๏ธ")
viz_end_btn = gr.Button("End โญ๏ธ")
sort_status = gr.Textbox(label="Status", interactive=False)
# Wire up sorting events
run_sort_btn.click(
fn=self.run_sort,
inputs=[sort_algo, pivot_strategy, partition_scheme],
outputs=[sort_viz_display, sort_list_display, sort_status]
)
viz_next_btn.click(fn=self.viz_next, outputs=[sort_viz_display])
viz_prev_btn.click(fn=self.viz_prev, outputs=[sort_viz_display])
viz_start_btn.click(fn=self.viz_start, outputs=[sort_viz_display])
viz_end_btn.click(fn=self.viz_end, outputs=[sort_viz_display])
step_slider.change(fn=self.viz_goto, inputs=[step_slider], outputs=[sort_viz_display])
# ============================================================
# TAB 3: Searching Algorithms
# ============================================================
with gr.TabItem("๐ Searching"):
with gr.Row():
# Left: Search controls
with gr.Column(scale=1):
gr.Markdown("### Search Settings")
search_algo = gr.Radio(
choices=["Linear Search", "Binary Search"],
value="Linear Search",
label="Algorithm",
info="Binary Search is O(log n) but requires sorted data"
)
# Educational info accordion
with gr.Accordion("๐ Algorithm Info", open=False):
gr.Markdown("""
**Linear Search** - O(n)
- Works on ANY list (sorted or unsorted)
- Checks each element one by one
- Simple but slow for large lists
**Binary Search** - O(log n)
- โ ๏ธ REQUIRES SORTED DATA!
- Halves the search space each step
- Much faster: 1000 elements โ only 10 comparisons!
**Example (searching 1000 elements):**
- Linear: up to 1000 checks
- Binary: at most 10 checks (logโ1000 โ 10)
""")
target_index = gr.Number(
label="Target Index",
value=0,
precision=0,
info="Which element to search for (by index)"
)
run_search_btn = gr.Button("๐ Run Search", variant="primary", size="lg")
gr.Markdown("---")
gr.Markdown("### Current List")
search_list_display = gr.HTML(value=self._render_image_list())
# Right: Visualization
with gr.Column(scale=2):
gr.Markdown("### Visualization")
search_viz_display = gr.HTML(
value=self.visualizer.render_current()
)
# Navigation controls
with gr.Row():
search_start_btn = gr.Button("โฎ๏ธ Start")
search_prev_btn = gr.Button("โ๏ธ Prev")
search_next_btn = gr.Button("Next โถ๏ธ")
search_end_btn = gr.Button("End โญ๏ธ")
search_status = gr.Textbox(label="Status", interactive=False)
# Wire up search events
run_search_btn.click(
fn=self.run_search,
inputs=[search_algo, target_index],
outputs=[search_viz_display, search_status]
)
search_next_btn.click(fn=self.viz_next, outputs=[search_viz_display])
search_prev_btn.click(fn=self.viz_prev, outputs=[search_viz_display])
search_start_btn.click(fn=self.viz_start, outputs=[search_viz_display])
search_end_btn.click(fn=self.viz_end, outputs=[search_viz_display])
# ============================================================
# TAB 4: Learn OOP
# ============================================================
with gr.TabItem("๐ Learn OOP"):
gr.Markdown("""
# Object-Oriented Programming Concepts
This application demonstrates several key OOP concepts:
## ๐ฆ Classes & Objects
**Classes** are blueprints for creating objects. In this app:
- `GestureImage` - represents a single captured gesture
- `ImageList` - manages a collection of gestures
- `BubbleSort`, `MergeSort`, `QuickSort` - sorting algorithms
- `Visualizer` - handles step-by-step display
## ๐ญ Inheritance
**Inheritance** lets classes share code. All sorting algorithms inherit from `SortingAlgorithm`:
```python
class SortingAlgorithm(ABC): # Abstract Base Class
@abstractmethod
def sort(self, data): ...
class BubbleSort(SortingAlgorithm): # Inherits from SortingAlgorithm
def sort(self, data):
# Bubble sort implementation
```
## ๐ Polymorphism
**Polymorphism** means "same interface, different behavior":
```python
# All these work the same way!
algo = BubbleSort()
algo = MergeSort()
algo = QuickSort()
# Same method call, different algorithms
result, steps = algo.run_full(data)
```
## ๐ญ Factory Pattern
**Factory Pattern** creates objects without exposing creation logic:
```python
# Factory creates the right renderer automatically
renderer = RendererFactory.create("Bubble Sort")
```
## ๐ Algorithm Comparison
| Algorithm | Time (Best) | Time (Worst) | Stable? | In-Place? |
|-----------|-------------|--------------|---------|-----------|
| Bubble Sort | O(n) | O(nยฒ) | โ
Yes | โ
Yes |
| Merge Sort | O(n log n) | O(n log n) | โ
Yes | โ No |
| Quick Sort | O(n log n) | O(nยฒ) | โ No | โ
Yes |
| Linear Search | O(1) | O(n) | - | - |
| Binary Search | O(1) | O(log n) | - | - |
## ๐ Stability
A **stable** sort preserves the relative order of equal elements.
Example with two peace signs โ๏ธโ and โ๏ธโ:
- **Stable**: Always produces [โ๏ธโ, โ๏ธโ] (original order kept)
- **Unstable**: Might produce [โ๏ธโ, โ๏ธโ] (order can change)
Try Quick Sort with duplicate gestures to see instability!
""")
# Footer
gr.Markdown("""
---
*Built for CISC 121 - Queen's University*
""")
return demo
# ==============================================================================
# MAIN ENTRY POINT
# ==============================================================================
def main():
"""Create and launch the Gradio app."""
app = GradioApp()
demo = app.create_ui()
demo.launch(share=False, ssr_mode=False, theme=gr.themes.Glass())
if __name__ == "__main__":
main()
# For HuggingFace Spaces compatibility (expects a 'demo' variable)
app = GradioApp()
demo = app.create_ui()
|