Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,46 @@ import torch
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
BATCH_SIZE = 8
|
| 7 |
FILE_LIMIT_MB = 1000
|
| 8 |
|
| 9 |
device = 0 if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
pipe = pipeline(
|
| 12 |
-
task="automatic-speech-recognition",
|
| 13 |
-
model=MODEL_NAME,
|
| 14 |
-
chunk_length_s=30,
|
| 15 |
-
device=device,
|
| 16 |
-
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def transcribe(inputs, task):
|
| 20 |
if inputs is None:
|
| 21 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 22 |
|
|
|
|
| 23 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 24 |
-
return
|
| 25 |
|
| 26 |
|
| 27 |
demo = gr.Blocks()
|
| 28 |
mic_transcribe = gr.Interface(
|
| 29 |
fn=transcribe,
|
| 30 |
-
inputs=gr.Audio(sources="microphone", type="filepath"),
|
| 31 |
outputs="text",
|
| 32 |
)
|
| 33 |
|
| 34 |
file_transcribe = gr.Interface(
|
| 35 |
fn=transcribe,
|
| 36 |
-
inputs=gr.Audio(sources="upload", type="filepath"),
|
| 37 |
outputs="text",
|
| 38 |
)
|
| 39 |
|
| 40 |
|
| 41 |
with demo:
|
| 42 |
gr.TabbedInterface([file_transcribe, mic_transcribe], ["Audio file", "Microphone"])
|
| 43 |
-
|
| 44 |
demo.launch(debug=True)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
MODEL_NAME_V1 = "rngzhi/cs3264-project"
|
| 6 |
+
MODEL_NAME_V2 = "rngzhi/cs3264-project-v2"
|
| 7 |
BATCH_SIZE = 8
|
| 8 |
FILE_LIMIT_MB = 1000
|
| 9 |
|
| 10 |
device = 0 if torch.cuda.is_available() else "cpu"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def load_model(model_version):
|
| 14 |
+
model_name = MODEL_NAME_V1 if model_version == 'Model-v1' else MODEL_NAME_V2
|
| 15 |
+
return pipeline(
|
| 16 |
+
task="automatic-speech-recognition",
|
| 17 |
+
model=model_name,
|
| 18 |
+
chunk_length_s=30,
|
| 19 |
+
device=device,
|
| 20 |
+
)
|
| 21 |
|
| 22 |
+
def transcribe(model_version, inputs, task):
|
| 23 |
if inputs is None:
|
| 24 |
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
| 25 |
|
| 26 |
+
pipe = load_model(model_version)
|
| 27 |
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
| 28 |
+
return text
|
| 29 |
|
| 30 |
|
| 31 |
demo = gr.Blocks()
|
| 32 |
mic_transcribe = gr.Interface(
|
| 33 |
fn=transcribe,
|
| 34 |
+
inputs=[gr.Dropdown(choices=['Model-v1', 'Model-v2'], label="Choose Model Version"), gr.Audio(sources="microphone", type="filepath")],
|
| 35 |
outputs="text",
|
| 36 |
)
|
| 37 |
|
| 38 |
file_transcribe = gr.Interface(
|
| 39 |
fn=transcribe,
|
| 40 |
+
inputs=[gr.Dropdown(choices=['Model-v1', 'Model-v2'], label="Choose Model Version"), gr.Audio(sources="upload", type="filepath")],
|
| 41 |
outputs="text",
|
| 42 |
)
|
| 43 |
|
| 44 |
|
| 45 |
with demo:
|
| 46 |
gr.TabbedInterface([file_transcribe, mic_transcribe], ["Audio file", "Microphone"])
|
|
|
|
| 47 |
demo.launch(debug=True)
|