mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-18 10:44:33 +08:00
first commit
This commit is contained in:
parent
8e635c082a
commit
10be5e7a2c
1
demo/streaming_stt/requirements.txt
Normal file
1
demo/streaming_stt/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
deepspeech
|
@ -50,7 +50,7 @@
|
||||
let avg_duration = Array.isArray(avg_durations) ? avg_durations[0] : null;
|
||||
let expected_duration: number | null = null;
|
||||
|
||||
const setValues = (index: number, value: unknown) => {
|
||||
const setValues = async (index: number, value: unknown) => {
|
||||
has_changed = true;
|
||||
input_values[index] = value;
|
||||
if (live && state !== "PENDING") {
|
||||
@ -85,7 +85,7 @@
|
||||
clearInterval(timer);
|
||||
};
|
||||
|
||||
const submit = () => {
|
||||
const submit = async () => {
|
||||
if (state === "PENDING") {
|
||||
return;
|
||||
}
|
||||
@ -103,53 +103,55 @@
|
||||
has_changed = false;
|
||||
let submission_count_at_click = submission_count;
|
||||
startTimer();
|
||||
fn("predict", { data: input_values }, queue, queueCallback)
|
||||
.then((output) => {
|
||||
if (
|
||||
state !== "PENDING" ||
|
||||
submission_count_at_click !== submission_count
|
||||
) {
|
||||
return;
|
||||
}
|
||||
stopTimer();
|
||||
output_values = output["data"];
|
||||
for (let [i, value] of output_values.entries()) {
|
||||
if (output_components[i].name === "state") {
|
||||
for (let [j, input_component] of input_components.entries()) {
|
||||
if (input_component.name === "state") {
|
||||
input_values[j] = value;
|
||||
}
|
||||
}
|
||||
let output: any;
|
||||
try {
|
||||
output = await fn(
|
||||
"predict",
|
||||
{ data: input_values },
|
||||
queue,
|
||||
queueCallback
|
||||
);
|
||||
} catch (e) {
|
||||
if (
|
||||
state !== "PENDING" ||
|
||||
submission_count_at_click !== submission_count
|
||||
) {
|
||||
return;
|
||||
}
|
||||
stopTimer();
|
||||
console.error(e);
|
||||
state = "ERROR";
|
||||
output_values = deepCopy(default_outputs);
|
||||
}
|
||||
if (state !== "PENDING" || submission_count_at_click !== submission_count) {
|
||||
return;
|
||||
}
|
||||
stopTimer();
|
||||
output_values = output["data"];
|
||||
for (let [i, value] of output_values.entries()) {
|
||||
if (output_components[i].name === "state") {
|
||||
for (let [j, input_component] of input_components.entries()) {
|
||||
if (input_component.name === "state") {
|
||||
input_values[j] = value;
|
||||
}
|
||||
}
|
||||
if ("durations" in output) {
|
||||
last_duration = output["durations"][0];
|
||||
}
|
||||
if ("avg_durations" in output) {
|
||||
avg_duration = output["avg_durations"][0];
|
||||
if (queue && initial_queue_index) {
|
||||
expected_duration = avg_duration * (initial_queue_index + 1);
|
||||
} else {
|
||||
expected_duration = avg_duration;
|
||||
}
|
||||
}
|
||||
state = "COMPLETE";
|
||||
if (live && has_changed) {
|
||||
submit();
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
if (
|
||||
state !== "PENDING" ||
|
||||
submission_count_at_click !== submission_count
|
||||
) {
|
||||
return;
|
||||
}
|
||||
stopTimer();
|
||||
console.error(e);
|
||||
state = "ERROR";
|
||||
output_values = deepCopy(default_outputs);
|
||||
});
|
||||
}
|
||||
}
|
||||
if ("durations" in output) {
|
||||
last_duration = output["durations"][0];
|
||||
}
|
||||
if ("avg_durations" in output) {
|
||||
avg_duration = output["avg_durations"][0];
|
||||
if (queue && initial_queue_index) {
|
||||
expected_duration = avg_duration * (initial_queue_index + 1);
|
||||
} else {
|
||||
expected_duration = avg_duration;
|
||||
}
|
||||
}
|
||||
state = "COMPLETE";
|
||||
if (live && has_changed) {
|
||||
await submit();
|
||||
}
|
||||
};
|
||||
const clear = () => {
|
||||
input_values = deepCopy(default_inputs);
|
||||
|
@ -9,6 +9,7 @@
|
||||
import { _ } from "svelte-i18n";
|
||||
|
||||
export let value: null | Value;
|
||||
export let live: boolean;
|
||||
export let setValue: (val: typeof value) => typeof value;
|
||||
export let theme: string;
|
||||
export let name: string;
|
||||
@ -24,33 +25,48 @@
|
||||
let player;
|
||||
let inited = false;
|
||||
let crop_values = [0, 100];
|
||||
let converting_blob = false;
|
||||
|
||||
function blob_to_data_url(blob: Blob): Promise<string> {
|
||||
return new Promise((fulfill, reject) => {
|
||||
let reader = new FileReader();
|
||||
reader.onerror = reject;
|
||||
reader.onload = (e) => fulfill(reader.result as string);
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
async function generate_data(): Promise<{
|
||||
data: string;
|
||||
name: string;
|
||||
is_example: boolean;
|
||||
}> {
|
||||
function blob_to_data_url(blob: Blob): Promise<string> {
|
||||
return new Promise((fulfill, reject) => {
|
||||
let reader = new FileReader();
|
||||
reader.onerror = reject;
|
||||
reader.onload = (e) => fulfill(reader.result as string);
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
audio_blob = new Blob(audio_chunks, { type: "audio/wav" });
|
||||
return {
|
||||
data: await blob_to_data_url(audio_blob),
|
||||
name,
|
||||
is_example
|
||||
};
|
||||
}
|
||||
|
||||
async function prepare_audio() {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
recorder = new MediaRecorder(stream);
|
||||
|
||||
recorder.addEventListener("dataavailable", (event) => {
|
||||
recorder.addEventListener("dataavailable", async (event) => {
|
||||
audio_chunks.push(event.data);
|
||||
if (live && !converting_blob) {
|
||||
converting_blob = true;
|
||||
await setValue(await generate_data());
|
||||
converting_blob = false;
|
||||
}
|
||||
});
|
||||
|
||||
recorder.addEventListener("stop", async () => {
|
||||
recording = false;
|
||||
audio_blob = new Blob(audio_chunks, { type: "audio/wav" });
|
||||
|
||||
setValue({
|
||||
data: await blob_to_data_url(audio_blob),
|
||||
name,
|
||||
is_example
|
||||
});
|
||||
if (!live) {
|
||||
setValue(await generate_data());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -117,7 +133,7 @@
|
||||
</script>
|
||||
|
||||
<div class="input-audio">
|
||||
{#if value === null}
|
||||
{#if value === null || (source === "microphone" && live)}
|
||||
{#if source === "microphone"}
|
||||
{#if recording}
|
||||
<button
|
||||
|
Loading…
Reference in New Issue
Block a user