Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Check if GPU is available
|
| 7 |
use_gpu = torch.cuda.is_available()
|
| 8 |
|
| 9 |
-
|
| 10 |
# Configure the pipeline to use the GPU if available
|
| 11 |
if use_gpu:
|
| 12 |
p = pipeline("automatic-speech-recognition",
|
| 13 |
-
|
| 14 |
else:
|
| 15 |
p = pipeline("automatic-speech-recognition",
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
def transcribe(audio, state="", uploaded_audio=None):
|
| 20 |
if uploaded_audio is not None:
|
| 21 |
audio = uploaded_audio
|
| 22 |
if not audio:
|
| 23 |
return state, state # Return a meaningful message
|
|
|
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
return state, state
|
| 29 |
except Exception as e:
|
| 30 |
return "An error occurred during transcription.", state # Handle other exceptions
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
gr.Interface(
|
| 36 |
fn=transcribe,
|
| 37 |
inputs=[
|
| 38 |
-
gr.inputs.Audio(source="microphone", type="
|
| 39 |
'state',
|
| 40 |
-
gr.inputs.Audio(label="Upload Audio File", type="
|
| 41 |
],
|
| 42 |
outputs=[
|
| 43 |
"textbox",
|
| 44 |
"state"
|
| 45 |
],
|
| 46 |
-
live=True
|
| 47 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
|
|
|
| 3 |
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
# Check if GPU is available
|
| 8 |
use_gpu = torch.cuda.is_available()
|
| 9 |
|
|
|
|
| 10 |
# Configure the pipeline to use the GPU if available
|
| 11 |
if use_gpu:
|
| 12 |
p = pipeline("automatic-speech-recognition",
|
| 13 |
+
model="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h", device=0)
|
| 14 |
else:
|
| 15 |
p = pipeline("automatic-speech-recognition",
|
| 16 |
+
model="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h")
|
| 17 |
+
|
| 18 |
+
chunk_size = 30 # Adjust the chunk size as needed
|
| 19 |
|
| 20 |
def transcribe(audio, state="", uploaded_audio=None):
|
| 21 |
if uploaded_audio is not None:
|
| 22 |
audio = uploaded_audio
|
| 23 |
if not audio:
|
| 24 |
return state, state # Return a meaningful message
|
| 25 |
+
|
| 26 |
try:
|
| 27 |
+
state += "Transcribing...\n"
|
| 28 |
+
chunks = [audio[i:i + chunk_size] for i in range(0, len(audio), chunk_size)]
|
| 29 |
+
for chunk in chunks:
|
| 30 |
+
text = p(chunk)["text"]
|
| 31 |
+
state += text + "\n"
|
| 32 |
+
time.sleep(1) # Simulate processing time for each chunk
|
| 33 |
return state, state
|
| 34 |
except Exception as e:
|
| 35 |
return "An error occurred during transcription.", state # Handle other exceptions
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
gr.Interface(
|
| 38 |
fn=transcribe,
|
| 39 |
inputs=[
|
| 40 |
+
gr.inputs.Audio(source="microphone", type="numpy"),
|
| 41 |
'state',
|
| 42 |
+
gr.inputs.Audio(label="Upload Audio File", type="numpy", source="upload")
|
| 43 |
],
|
| 44 |
outputs=[
|
| 45 |
"textbox",
|
| 46 |
"state"
|
| 47 |
],
|
| 48 |
+
live=True
|
| 49 |
+
).launch()
|