Spaces:
Running
on
Zero
Running
on
Zero
Staticaliza
commited on
Commit
•
3469434
1
Parent(s):
3919065
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Imports
|
2 |
+
import gradio as gr
|
3 |
+
import spaces
|
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 |
+
DEFAULT_TASK = "transcribe"
|
16 |
+
BATCH_SIZE = 8
|
17 |
+
|
18 |
+
repo = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3-turbo", chunk_length_s=30, device=DEVICE)
|
19 |
+
|
20 |
+
css = '''
|
21 |
+
.gradio-container{max-width: 560px !important}
|
22 |
+
h1{text-align:center}
|
23 |
+
footer {
|
24 |
+
visibility: hidden
|
25 |
+
}
|
26 |
+
'''
|
27 |
+
|
28 |
+
@spaces.GPU(duration=10)
|
29 |
+
def transcribe(input=None, task=DEFAULT_TASK):
|
30 |
+
print(input)
|
31 |
+
if input is None: raise gr.Error("Invalid input.")
|
32 |
+
output = repo(input, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
33 |
+
return output
|
34 |
+
|
35 |
+
def cloud():
|
36 |
+
print("[CLOUD] | Space maintained.")
|
37 |
+
|
38 |
+
# Initialize
|
39 |
+
with gr.Blocks(css=css) as main:
|
40 |
+
with gr.Column():
|
41 |
+
gr.Markdown("🪄 Transcribe audio to text.")
|
42 |
+
|
43 |
+
with gr.Column():
|
44 |
+
input = gr.Audio(sources="upload", type="filepath", label="Input")
|
45 |
+
task = gr.Radio(["transcribe", "translate"], label="Task", value=DEFAULT_TASK)
|
46 |
+
submit = gr.Button("▶")
|
47 |
+
maintain = gr.Button("☁️")
|
48 |
+
|
49 |
+
with gr.Column():
|
50 |
+
output = gr.Textbox(lines=1, value="", label="Output")
|
51 |
+
|
52 |
+
submit.click(transcribe, inputs=[input, task], outputs=[output], queue=False)
|
53 |
+
maintain.click(cloud, inputs=[], outputs=[], queue=False)
|
54 |
+
|
55 |
+
main.launch(show_api=True)
|