Spaces:
Running
Running
Update app.py
Browse filestest gr.Blocks()
app.py
CHANGED
@@ -7,15 +7,13 @@ import ffmpeg # Make sure it's ffmpeg-python
|
|
7 |
# Check if GPU is available
|
8 |
use_gpu = torch.cuda.is_available()
|
9 |
|
10 |
-
|
11 |
# Configure the pipeline to use the GPU if available
|
12 |
if use_gpu:
|
13 |
p = pipeline("automatic-speech-recognition",
|
14 |
-
|
15 |
else:
|
16 |
p = pipeline("automatic-speech-recognition",
|
17 |
-
|
18 |
-
|
19 |
|
20 |
def extract_audio_from_m3u8(url):
|
21 |
try:
|
@@ -25,8 +23,7 @@ def extract_audio_from_m3u8(url):
|
|
25 |
except Exception as e:
|
26 |
return f"An error occurred: {e}"
|
27 |
|
28 |
-
|
29 |
-
def transcribe(audio, state="", uploaded_audio=None, m3u8_url=""):
|
30 |
if m3u8_url:
|
31 |
audio = extract_audio_from_m3u8(m3u8_url)
|
32 |
|
@@ -34,36 +31,48 @@ def transcribe(audio, state="", uploaded_audio=None, m3u8_url=""):
|
|
34 |
audio = uploaded_audio
|
35 |
|
36 |
if not audio:
|
37 |
-
return state, state # Return a meaningful message
|
38 |
|
39 |
try:
|
40 |
time.sleep(3)
|
41 |
text = p(audio, chunk_length_s= 50)["text"]
|
42 |
state += text + "\n"
|
43 |
-
return state, state
|
44 |
except Exception as e:
|
45 |
-
return "An error occurred during transcription.", state # Handle other exceptions
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
gr.
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
def extract_audio_from_m3u8(url):
|
19 |
try:
|
|
|
23 |
except Exception as e:
|
24 |
return f"An error occurred: {e}"
|
25 |
|
26 |
+
def transcribe_function(audio, state, uploaded_audio, m3u8_url):
|
|
|
27 |
if m3u8_url:
|
28 |
audio = extract_audio_from_m3u8(m3u8_url)
|
29 |
|
|
|
31 |
audio = uploaded_audio
|
32 |
|
33 |
if not audio:
|
34 |
+
return {state_var: state, transcription_var: state} # Return a meaningful message
|
35 |
|
36 |
try:
|
37 |
time.sleep(3)
|
38 |
text = p(audio, chunk_length_s= 50)["text"]
|
39 |
state += text + "\n"
|
40 |
+
return {state_var: state, transcription_var: state}
|
41 |
except Exception as e:
|
42 |
+
return {transcription_var: "An error occurred during transcription.", state_var: state} # Handle other exceptions
|
43 |
+
|
44 |
+
# ... [most of your code remains unchanged]
|
45 |
+
|
46 |
+
def reset_output(transcription, state):
|
47 |
+
"""Function to reset the state to an empty string."""
|
48 |
+
return "", ""
|
49 |
+
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
state_var = gr.State("")
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
with gr.Column():
|
55 |
+
microphone = gr.Audio(source="microphone", type="filepath", label="Microphone")
|
56 |
+
uploaded_audio = gr.Audio(label="Upload Audio File", type="filepath", source="upload")
|
57 |
+
m3u8_url = gr.Textbox(label="m3u8 URL | E.g.: from kvf.fo or logting.fo")
|
58 |
+
|
59 |
+
with gr.Column():
|
60 |
+
transcription_var = gr.Textbox(type="text", label="Transcription", readonly=True)
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
transcribe_button = gr.Button("Transcribe")
|
64 |
+
reset_button = gr.Button("Reset output")
|
65 |
+
|
66 |
+
transcribe_button.click(
|
67 |
+
transcribe_function,
|
68 |
+
[microphone, state_var, uploaded_audio, m3u8_url],
|
69 |
+
[transcription_var, state_var]
|
70 |
+
)
|
71 |
+
|
72 |
+
reset_button.click(
|
73 |
+
reset_output,
|
74 |
+
[transcription_var, state_var],
|
75 |
+
[transcription_var, state_var]
|
76 |
+
)
|
77 |
+
|
78 |
+
demo.launch(theme="dark")
|