Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- app.py +64 -0
- default-favicon.ico +0 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastrtc import (
|
2 |
+
Stream,
|
3 |
+
AdditionalOutputs,
|
4 |
+
audio_to_float32,
|
5 |
+
ReplyOnPause,
|
6 |
+
get_twilio_turn_credentials,
|
7 |
+
)
|
8 |
+
from functools import lru_cache
|
9 |
+
import gradio as gr
|
10 |
+
from typing import Generator, Literal
|
11 |
+
from numpy.typing import NDArray
|
12 |
+
import numpy as np
|
13 |
+
from moonshine_onnx import MoonshineOnnxModel, load_tokenizer
|
14 |
+
|
15 |
+
|
16 |
+
@lru_cache(maxsize=None)
|
17 |
+
def load_moonshine(
|
18 |
+
model_name: Literal["moonshine/base", "moonshine/tiny"],
|
19 |
+
) -> MoonshineOnnxModel:
|
20 |
+
return MoonshineOnnxModel(model_name=model_name)
|
21 |
+
|
22 |
+
|
23 |
+
tokenizer = load_tokenizer()
|
24 |
+
|
25 |
+
|
26 |
+
def stt(
|
27 |
+
audio: tuple[int, NDArray[np.int16 | np.float32]],
|
28 |
+
model_name: Literal["moonshine/base", "moonshine/tiny"],
|
29 |
+
) -> Generator[AdditionalOutputs, None, None]:
|
30 |
+
moonshine = load_moonshine(model_name)
|
31 |
+
sr, audio_np = audio # type: ignore
|
32 |
+
if audio_np.dtype == np.int16:
|
33 |
+
audio_np = audio_to_float32(audio)
|
34 |
+
if audio_np.ndim == 1:
|
35 |
+
audio_np = audio_np.reshape(1, -1)
|
36 |
+
tokens = moonshine.generate(audio_np)
|
37 |
+
yield AdditionalOutputs(tokenizer.decode_batch(tokens)[0])
|
38 |
+
|
39 |
+
|
40 |
+
stream = Stream(
|
41 |
+
ReplyOnPause(stt, input_sample_rate=16000),
|
42 |
+
modality="audio",
|
43 |
+
mode="send",
|
44 |
+
ui_args={
|
45 |
+
"title": "Live Captions by Moonshine",
|
46 |
+
"icon": "default-favicon.ico",
|
47 |
+
"icon_button_color": "#5c5c5c",
|
48 |
+
"pulse_color": "#a7c6fc",
|
49 |
+
"icon_radius": 0,
|
50 |
+
},
|
51 |
+
rtc_configuration=get_twilio_turn_credentials(),
|
52 |
+
additional_inputs=[
|
53 |
+
gr.Radio(
|
54 |
+
choices=["moonshine/base", "moonshine/tiny"],
|
55 |
+
value="moonshine/base",
|
56 |
+
label="Model",
|
57 |
+
)
|
58 |
+
],
|
59 |
+
additional_outputs=[gr.Textbox(label="Captions")],
|
60 |
+
additional_outputs_handler=lambda prev, current: (prev + "\n" + current).strip(),
|
61 |
+
)
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
stream.ui.launch()
|
default-favicon.ico
ADDED
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastrtc[vad]
|
2 |
+
useful-moonshine-onnx@git+https://[email protected]/usefulsensors/moonshine.git#subdirectory=moonshine-onnx
|
3 |
+
twilio
|