Spaces:
Running
on
T4
Running
on
T4
File size: 6,894 Bytes
5a766fd 2d522b6 a7361bc 5cae5d7 5a766fd 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 5a766fd 5cae5d7 366edf8 1727d3b 5a766fd 366edf8 1727d3b 2d522b6 366edf8 2d522b6 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 366edf8 1727d3b 5a766fd 5cae5d7 5a766fd 5cae5d7 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
from __future__ import annotations
import os
import gradio as gr
import numpy as np
import torch
import torchaudio
from simuleval_transcoder import *
from pydub import AudioSegment
import time
from time import sleep
from seamless_communication.cli.streaming.agents.tt_waitk_unity_s2t_m4t import (
TestTimeWaitKUnityS2TM4T,
)
language_code_to_name = {
"cmn": "Mandarin Chinese",
"deu": "German",
"eng": "English",
"fra": "French",
"spa": "Spanish",
}
S2ST_TARGET_LANGUAGE_NAMES = language_code_to_name.values()
LANGUAGE_NAME_TO_CODE = {v: k for k, v in language_code_to_name.items()}
DEFAULT_TARGET_LANGUAGE = "English"
# TODO: Update this so it takes in target langs from input, refactor sample rate
transcoder = SimulevalTranscoder(
sample_rate=48_000,
debug=False,
buffer_limit=1,
)
def start_recording():
logger.debug(f"start_recording: starting transcoder")
transcoder.start()
def translate_audio_segment(audio):
logger.debug(f"translate_audio_segment: incoming audio")
sample_rate, data = audio
transcoder.process_incoming_bytes(data.tobytes(), 'eng', sample_rate)
speech_and_text_output = transcoder.get_buffered_output()
if speech_and_text_output is None:
logger.debug("No output from transcoder.get_buffered_output()")
return None, None
logger.debug(f"We DID get output from the transcoder! {speech_and_text_output}")
text = None
speech = None
if speech_and_text_output.speech_samples:
speech = (speech_and_text_output.speech_samples, speech_and_text_output.speech_sample_rate)
if speech_and_text_output.text:
text = speech_and_text_output.text
if speech_and_text_output.final:
text += "\n"
return speech, text
def streaming_input_callback(
audio_file, translated_audio_bytes_state, translated_text_state
):
translated_wav_segment, translated_text = translate_audio_segment(audio_file)
logger.debug(f'translated_audio_bytes_state {translated_audio_bytes_state}')
logger.debug(f'translated_wav_segment {translated_wav_segment}')
# TODO: accumulate each segment to provide a continuous audio segment
if translated_wav_segment is not None:
sample_rate, audio_bytes = translated_wav_segment
audio_np_array = np.frombuffer(audio_bytes, dtype=np.float32, count=3)
# combine translated wav
if type(translated_audio_bytes_state) is not tuple:
translated_audio_bytes_state = (sample_rate, audio_np_array)
# translated_audio_bytes_state = np.array([])
else:
translated_audio_bytes_state = (translated_audio_bytes_state[0], np.append(translated_audio_bytes_state[1], translated_wav_segment[1]))
if translated_text is not None:
translated_text_state += " | " + str(translated_text)
# most_recent_input_audio_segment = (most_recent_input_audio_segment[0], np.append(most_recent_input_audio_segment[1], audio_file[1]))
# Not necessary but for readability.
most_recent_input_audio_segment = audio_file
translated_wav_segment = translated_wav_segment
output_translation_combined = translated_audio_bytes_state
stream_output_text = translated_text_state
return [
most_recent_input_audio_segment,
translated_wav_segment,
output_translation_combined,
stream_output_text,
translated_audio_bytes_state,
translated_text_state,
]
def clear():
logger.debug(f"Clearing State")
return [bytes(), ""]
def blocks():
with gr.Blocks() as demo:
with gr.Row():
# Hook this up once supported
target_language = gr.Dropdown(
label="Target language",
choices=S2ST_TARGET_LANGUAGE_NAMES,
value=DEFAULT_TARGET_LANGUAGE,
)
translated_audio_bytes_state = gr.State(None)
translated_text_state = gr.State("")
input_audio = gr.Audio(
label="Input Audio",
# source="microphone", # gradio==3.41.0
sources=["microphone"], # new gradio seems to call this less often...
streaming=True,
)
# input_audio = gr.Audio(
# label="Input Audio",
# type="filepath",
# source="microphone",
# streaming=True,
# )
most_recent_input_audio_segment = gr.Audio(
label="Recent Input Audio Segment segments",
# format="bytes",
streaming=True
)
# Force translate
stream_as_bytes_btn = gr.Button("Force translate most recent recording segment (ask for model output)")
output_translation_segment = gr.Audio(
label="Translated audio segment",
autoplay=False,
streaming=True,
type="numpy",
)
output_translation_combined = gr.Audio(
label="Translated audio combined",
autoplay=False,
streaming=True,
type="numpy",
)
# Could add output text segment
stream_output_text = gr.Textbox(label="Translated text")
stream_as_bytes_btn.click(
streaming_input_callback,
[input_audio, translated_audio_bytes_state, translated_text_state],
[
most_recent_input_audio_segment,
output_translation_segment,
output_translation_combined,
stream_output_text,
translated_audio_bytes_state,
translated_text_state,
],
)
# input_audio.change(
# streaming_input_callback,
# [input_audio, translated_audio_bytes_state, translated_text_state],
# [
# most_recent_input_audio_segment,
# output_translation_segment,
# output_translation_combined,
# stream_output_text,
# translated_audio_bytes_state,
# translated_text_state,
# ],
# )
input_audio.stream(
streaming_input_callback,
[input_audio, translated_audio_bytes_state, translated_text_state],
[
most_recent_input_audio_segment,
output_translation_segment,
output_translation_combined,
stream_output_text,
translated_audio_bytes_state,
translated_text_state,
],
)
input_audio.start_recording(
start_recording,
)
input_audio.clear(
clear, None, [translated_audio_bytes_state, translated_text_state]
)
input_audio.start_recording(
clear, None, [translated_audio_bytes_state, translated_text_state]
)
demo.queue().launch()
blocks()
|