Skip to content

Commit

Permalink
Add editable parameter to Audio (#6745)
Browse files Browse the repository at this point in the history
* add editable param to audio and add story

* add changeset

* fix test

* fix test again

* tweak

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
hannahblair and gradio-pr-bot committed Dec 11, 2023
1 parent 21ce721 commit 3240d04
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/breezy-cougars-flow.md
@@ -0,0 +1,6 @@
---
"@gradio/audio": minor
"gradio": minor
---

feat:Add `editable` parameter to Audio
5 changes: 4 additions & 1 deletion gradio/components/audio.py
Expand Up @@ -82,6 +82,7 @@ def __init__(
autoplay: bool = False,
show_download_button=True,
show_share_button: bool | None = None,
editable: bool = True,
min_length: int | None = None,
max_length: int | None = None,
waveform_options: WaveformOptions | dict | None = None,
Expand All @@ -97,7 +98,7 @@ def __init__(
container: If True, will place the component in a container - providing some extra padding around the border.
scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
interactive: if True, will allow users to upload and edit a audio file; if False, can only be used to play audio. If not provided, this is inferred based on whether the component is used as an input or output.
interactive: If True, will allow users to upload and edit an audio file. If False, can only be used to play audio. If not provided, this is inferred based on whether the component is used as an input or output.
visible: If False, component will be hidden.
streaming: If set to True when used in a `live` interface as an input, will automatically stream webcam feed. When used set as an output, takes audio chunks yield from the backend and combines them into one streaming audio output.
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
Expand All @@ -107,6 +108,7 @@ def __init__(
autoplay: Whether to automatically play the audio when the component is used as an output. Note: browsers will not autoplay audio files if the user has not interacted with the page yet.
show_download_button: If True, will show a download button in the corner of the component for saving audio. If False, icon does not appear.
show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
editable: If True, allows users to manipulate the audio file (if the component is interactive).
min_length: The minimum length of audio (in seconds) that the user can pass into the prediction function. If None, there is no minimum length.
max_length: The maximum length of audio (in seconds) that the user can pass into the prediction function. If None, there is no maximum length.
waveform_options: A dictionary of options for the waveform display. Options include: waveform_color (str), waveform_progress_color (str), show_controls (bool), skip_length (int). Default is None, which uses the default values for these options.
Expand Down Expand Up @@ -146,6 +148,7 @@ def __init__(
if show_share_button is None
else show_share_button
)
self.editable = editable
if waveform_options is None:
self.waveform_options = WaveformOptions()
self.waveform_options = (
Expand Down
15 changes: 15 additions & 0 deletions js/audio/Audio.stories.svelte
Expand Up @@ -56,6 +56,21 @@
}}
/>

<Story
name="with disabled editing"
args={{
value: {
path: "https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3",
url: "https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3",
orig_name: "sample-0.mp3"
},
interactive: true,
sources: ["microphone", "upload"],
label: "Audio Upload",
editable: false
}}
/>

<Story
name="with hidden recording waveform"
args={{
Expand Down
2 changes: 2 additions & 0 deletions js/audio/Index.svelte
Expand Up @@ -34,6 +34,7 @@
export let autoplay = false;
export let show_download_button = true;
export let show_share_button = false;
export let editable = true;
export let waveform_options: WaveformOptions = {};
export let pending: boolean;
export let streaming: boolean;
Expand Down Expand Up @@ -190,6 +191,7 @@
{pending}
{streaming}
{handle_reset_value}
{editable}
bind:dragging
on:edit={() => gradio.dispatch("edit")}
on:play={() => gradio.dispatch("play")}
Expand Down
2 changes: 2 additions & 0 deletions js/audio/interactive/InteractiveAudio.svelte
Expand Up @@ -31,6 +31,7 @@
export let dragging: boolean;
export let active_source: "microphone" | "upload";
export let handle_reset_value: () => void = () => {};
export let editable = true;
$: dispatch("drag", dragging);
Expand Down Expand Up @@ -259,6 +260,7 @@
{waveform_options}
{trim_region_settings}
{handle_reset_value}
{editable}
interactive
on:stop
on:play
Expand Down
2 changes: 2 additions & 0 deletions js/audio/player/AudioPlayer.svelte
Expand Up @@ -20,6 +20,7 @@
event: "stream" | "change" | "stop_recording"
) => Promise<void> = () => Promise.resolve();
export let interactive = false;
export let editable = true;
export let trim_region_settings = {};
export let waveform_settings: Record<string, any>;
export let waveform_options: WaveformOptions;
Expand Down Expand Up @@ -184,6 +185,7 @@
{handle_reset_value}
{waveform_options}
{trim_region_settings}
{editable}
/>
{/if}
</div>
Expand Down
29 changes: 15 additions & 14 deletions js/audio/shared/WaveformControls.svelte
Expand Up @@ -23,6 +23,7 @@
export let waveform_options: WaveformOptions = {};
export let trim_region_settings: WaveformOptions = {};
export let show_volume_slider = false;
export let editable = true;
export let trimDuration = 0;
Expand Down Expand Up @@ -237,21 +238,21 @@
</div>

<div class="settings-wrapper">
{#if showRedo && mode === ""}
<button
class="action icon"
aria-label="Reset audio"
on:click={() => {
handle_reset_value();
clearRegions();
mode = "";
}}
>
<Undo />
</button>
{/if}
{#if editable && interactive}
{#if showRedo && mode === ""}
<button
class="action icon"
aria-label="Reset audio"
on:click={() => {
handle_reset_value();
clearRegions();
mode = "";
}}
>
<Undo />
</button>
{/if}

{#if interactive}
{#if mode === ""}
<button
class="action icon"
Expand Down
2 changes: 2 additions & 0 deletions test/test_components.py
Expand Up @@ -731,6 +731,7 @@ def test_component_functions(self, gradio_temp_dir):
"show_label": True,
"label": "Upload Your Audio",
"container": True,
"editable": True,
"min_width": 160,
"scale": None,
"elem_id": None,
Expand Down Expand Up @@ -776,6 +777,7 @@ def test_component_functions(self, gradio_temp_dir):
"max_length": None,
"min_length": None,
"container": True,
"editable": True,
"min_width": 160,
"scale": None,
"elem_id": None,
Expand Down

0 comments on commit 3240d04

Please sign in to comment.