File size: 1,508 Bytes
5e84ffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448b42a
5e84ffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Web-based piece practice view.

This module provides a web interface using Gradio for visualizing
and interacting with piece practice sessions.
"""

from abc import ABC, abstractmethod
from typing import Any, Callable, Tuple

import gradio as gr
import numpy as np


class WebPracticeView(ABC):
    """Handles the user interface for all practice applications."""

    def __init__(
        self,
        on_generate_melody: Callable[..., Tuple[Any, ...]],
        on_end_practice: Callable[[], Tuple[Any, ...]],
        on_audio_input: Callable[[Tuple[int, np.ndarray]], Tuple[Any, ...]],
    ):
        """Initialize the UI with callback functions.

        Args:
            on_generate_melody: Function to call when start button is clicked
            on_end_practice: Function to call when stop button is clicked
            on_audio_input: Function to process audio input
        """
        self.on_generate_melody = on_generate_melody
        self.on_end_practice = on_end_practice
        self.on_audio_input = on_audio_input

    def launch(self, **kwargs):
        """Launch the Gradio application.

        Args:
            **kwargs: Additional keyword arguments for the launch method.
        """
        app = self._build_interface()
        app.queue()
        app.launch(**kwargs)

    @abstractmethod
    def _build_interface(self) -> gr.Blocks:
        """Create and configure the Gradio interface.

        Returns:
            gr.Blocks: The Gradio interface.
        """
        pass