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