Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,48 +4,42 @@ import spaces
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
from transformers import pipeline
|
| 7 |
-
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
BATCH_SIZE = 8
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
pipe = pipeline(task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device,)
|
| 18 |
-
|
| 19 |
|
| 20 |
@spaces.GPU
|
| 21 |
def transcribe(inputs, task):
|
| 22 |
-
if inputs is None:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
gr.Audio(sources="upload", type="filepath", label="
|
| 36 |
-
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
with demo:
|
| 49 |
-
gr.TabbedInterface([file_transcribe], ["Audio file"])
|
| 50 |
-
|
| 51 |
-
demo.queue().launch(ssr_mode=False)
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
from transformers import pipeline
|
|
|
|
| 7 |
|
| 8 |
+
# Pre-Initialize
|
| 9 |
+
DEVICE = "auto"
|
| 10 |
+
if DEVICE == "auto":
|
| 11 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
print(f"[SYSTEM] | Using {DEVICE} type compute device.")
|
| 13 |
|
| 14 |
+
# Variables
|
| 15 |
BATCH_SIZE = 8
|
| 16 |
|
| 17 |
+
pipe = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3-turbo", chunk_length_s=30, device=device)
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@spaces.GPU
|
| 20 |
def transcribe(inputs, task):
|
| 21 |
+
if inputs is None: raise gr.Error("Invalid input.")
|
| 22 |
+
output = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 23 |
+
return output
|
| 24 |
+
|
| 25 |
+
def cloud():
|
| 26 |
+
print("[CLOUD] | Space maintained.")
|
| 27 |
+
|
| 28 |
+
# Initialize
|
| 29 |
+
with gr.Blocks(css=css) as main:
|
| 30 |
+
with gr.Column():
|
| 31 |
+
gr.Markdown("🪄 Transcribe audio to text.")
|
| 32 |
+
|
| 33 |
+
with gr.Column():
|
| 34 |
+
input = gr.Audio(sources="upload", type="filepath", label="Input"),
|
| 35 |
+
type = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
|
| 36 |
+
submit = gr.Button("▶")
|
| 37 |
+
maintain = gr.Button("☁️")
|
| 38 |
+
|
| 39 |
+
with gr.Column():
|
| 40 |
+
output = gr.Textbox(lines=1, value=DEFAULT_INPUT, label="Output")
|
| 41 |
+
|
| 42 |
+
submit.click(transcribe, inputs=[input, type], outputs=[output], queue=False)
|
| 43 |
+
maintain.click(cloud, inputs=[], outputs=[], queue=False)
|
| 44 |
+
|
| 45 |
+
main.launch(show_api=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|