Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to gradio 5 #100

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions gryannote/audio/backend/gryannote_audio/audio_labeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from gradio.exceptions import Error
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
from pydub import AudioSegment
from pyannote.core import Annotation as PyannoteAnnotation

from .core import AnnotadedAudioData
Expand Down Expand Up @@ -298,6 +299,15 @@ def __init__(

def example_inputs(self) -> Any:
return "https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav"

def process_example(
self, value: Tuple[int, np.ndarray] | str | Path | bytes | None
) -> str:
if value is None:
return ""
elif isinstance(value, (str, Path)):
return Path(value).name
return "(audio)"

def preprocess(
self, payload: AnnotadedAudioData | None
Expand Down Expand Up @@ -452,15 +462,6 @@ def stream_output(
binary_data = binary_data[44:]
return binary_data, output_file

def process_example(
self, value: Tuple[int, np.ndarray] | str | Path | bytes | None
) -> str:
if value is None:
return ""
elif isinstance(value, (str, Path)):
return Path(value).name
return "(audio)"

def check_streamable(self):
if (
self.sources is not None
Expand All @@ -470,6 +471,27 @@ def check_streamable(self):
raise ValueError(
"AudioLabeling streaming only available if source includes 'microphone'."
)

async def combine_stream(
self,
stream: list[bytes],
desired_output_format: str | None = None,
only_file=False, # noqa: ARG002
) -> FileData:
output_file = FileData(
path=processing_utils.save_bytes_to_cache(
b"".join(stream), "audio.mp3", cache_dir=self.GRADIO_CACHE
),
is_stream=False,
orig_name="audio-stream.mp3",
)
if desired_output_format and desired_output_format != "mp3":
new_path = Path(output_file.path).with_suffix(f".{desired_output_format}")
AudioSegment.from_file(output_file.path).export(
new_path, format=desired_output_format
)
output_file.path = str(new_path)
return output_file


def Player(
Expand Down
2 changes: 2 additions & 0 deletions gryannote/audio/frontend/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@
on:clear={() => gradio.dispatch("clear")}
on:error={handle_error}
i18n={gradio.i18n}
upload={gradio.client.upload}
stream_handler={gradio.client.stream}
{waveform_settings}
{waveform_options}
{timeline_options}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { getContext, onDestroy, createEventDispatcher } from "svelte";
import { Upload, ModifyUpload } from "@gradio/upload";
import {
upload,
prepare_files, FileData,
type upload_files
type upload_files,
type Client
} from "@gradio/client";
import { BlockLabel } from "@gradio/atoms";
import { Music } from "@gradio/icons";
Expand Down Expand Up @@ -41,6 +41,9 @@
export let hover_options: HoverOptions = {};
export let dragging: boolean;
export let active_source: "microphone" | "upload";
export let max_file_size: number | null = null;
export let upload: Client["upload"];
export let stream_handler: Client["stream"];

// Needed for wasm support
const upload_fn = getContext<typeof upload_files>("upload_files");
Expand Down Expand Up @@ -112,7 +115,7 @@
let _audio_blob = new File(blobs, "audio.wav");
const val = await prepare_files([_audio_blob], event === "stream");
let fileData = (
(await upload(val, root, undefined, upload_fn))?.filter(
(await upload(val, root, undefined, max_file_size || undefined))?.filter(
Boolean
) as FileData[]
)[0];
Expand Down Expand Up @@ -272,6 +275,9 @@
bind:dragging
on:error={({ detail }) => dispatch("error", detail)}
{root}
{max_file_size}
{upload}
{stream_handler}
>
<slot />
</Upload>
Expand Down
Loading