unijoh commited on
Commit
9038461
·
verified ·
1 Parent(s): 538b8fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -73
app.py CHANGED
@@ -4,76 +4,101 @@ from transformers import pipeline
4
  import torch
5
  import ffmpeg # Make sure it's ffmpeg-python
6
 
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
- model="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h", device=0)
15
- else:
16
- p = pipeline("automatic-speech-recognition",
17
- model="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h")
18
-
19
- def extract_audio_from_m3u8(url):
20
- try:
21
- output_file = "output_audio.aac"
22
- ffmpeg.input(url).output(output_file).run(overwrite_output=True)
23
- return output_file
24
- except Exception as e:
25
- return f"An error occurred: {e}"
26
-
27
- def transcribe_function(audio, state, uploaded_audio, m3u8_url):
28
- if m3u8_url:
29
- audio = extract_audio_from_m3u8(m3u8_url)
30
-
31
- if uploaded_audio is not None:
32
- audio = uploaded_audio
33
-
34
- if not audio:
35
- return {state_var: state, transcription_var: state} # Return a meaningful message
36
-
37
- try:
38
- time.sleep(3)
39
- text = p(audio, chunk_length_s= 50)["text"]
40
- state += text + "\n"
41
- return {state_var: state, transcription_var: state}
42
- except Exception as e:
43
- return {transcription_var: "An error occurred during transcription.", state_var: state} # Handle other exceptions
44
-
45
- # ... [most of your code remains unchanged]
46
-
47
- def reset_output(transcription, state):
48
- """Function to reset the state to an empty string."""
49
- return "", ""
50
-
51
- with gr.Blocks() as demo:
52
- state_var = gr.State("")
53
-
54
- with gr.Row():
55
- with gr.Column():
56
- microphone = gr.Audio(source="microphone", type="filepath", label="Microphone")
57
- uploaded_audio = gr.Audio(label="Upload Audio File", type="filepath", source="upload")
58
- m3u8_url = gr.Textbox(label="m3u8 URL | E.g.: from kvf.fo or logting.fo")
59
-
60
- with gr.Column():
61
- transcription_var = gr.Textbox(type="text", label="Transcription", readonly=True)
62
-
63
- with gr.Row():
64
- transcribe_button = gr.Button("Transcribe")
65
- reset_button = gr.Button("Reset output")
66
-
67
- transcribe_button.click(
68
- transcribe_function,
69
- [microphone, state_var, uploaded_audio, m3u8_url],
70
- [transcription_var, state_var]
71
- )
72
-
73
- reset_button.click(
74
- reset_output,
75
- [transcription_var, state_var],
76
- [transcription_var, state_var]
77
- )
78
-
79
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import torch
5
  import ffmpeg # Make sure it's ffmpeg-python
6
 
7
+ def main():
8
+ # Check if GPU is available
9
+ use_gpu = torch.cuda.is_available()
10
+
11
+ # Configure the pipeline to use the GPU if available
12
+ if use_gpu:
13
+ p = pipeline(
14
+ "automatic-speech-recognition",
15
+ model="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h",
16
+ device=0
17
+ )
18
+ else:
19
+ p = pipeline(
20
+ "automatic-speech-recognition",
21
+ model="carlosdanielhernandezmena/wav2vec2-large-xlsr-53-faroese-100h"
22
+ )
23
+
24
+ def extract_audio_from_m3u8(url):
25
+ try:
26
+ output_file = "output_audio.aac"
27
+ ffmpeg.input(url).output(output_file).run(overwrite_output=True)
28
+ return output_file
29
+ except Exception as e:
30
+ return f"An error occurred: {e}"
31
+
32
+ def transcribe_function(audio, state, uploaded_audio, m3u8_url):
33
+ if m3u8_url:
34
+ audio = extract_audio_from_m3u8(m3u8_url)
35
+
36
+ if uploaded_audio is not None:
37
+ audio = uploaded_audio
38
+
39
+ if not audio:
40
+ # Return a meaningful message; no audio found
41
+ return {state_var: state, transcription_var: state}
42
+
43
+ try:
44
+ time.sleep(3)
45
+ text = p(audio, chunk_length_s=50)["text"]
46
+ state += text + "\n"
47
+ return {state_var: state, transcription_var: state}
48
+ except Exception as e:
49
+ return {
50
+ transcription_var: "An error occurred during transcription.",
51
+ state_var: state
52
+ }
53
+
54
+ def reset_output(transcription, state):
55
+ """Function to reset the state to an empty string."""
56
+ return "", ""
57
+
58
+ with gr.Blocks() as demo:
59
+ state_var = gr.State("")
60
+
61
+ with gr.Row():
62
+ with gr.Column():
63
+ microphone = gr.Audio(
64
+ source="microphone",
65
+ type="filepath",
66
+ label="Microphone"
67
+ )
68
+ uploaded_audio = gr.Audio(
69
+ label="Upload Audio File",
70
+ type="filepath",
71
+ source="upload"
72
+ )
73
+ m3u8_url = gr.Textbox(
74
+ label="m3u8 URL | E.g.: from kvf.fo or logting.fo"
75
+ )
76
+
77
+ with gr.Column():
78
+ transcription_var = gr.Textbox(
79
+ type="text",
80
+ label="Transcription",
81
+ readonly=True
82
+ )
83
+
84
+ with gr.Row():
85
+ transcribe_button = gr.Button("Transcribe")
86
+ reset_button = gr.Button("Reset output")
87
+
88
+ transcribe_button.click(
89
+ transcribe_function,
90
+ [microphone, state_var, uploaded_audio, m3u8_url],
91
+ [transcription_var, state_var]
92
+ )
93
+
94
+ reset_button.click(
95
+ reset_output,
96
+ [transcription_var, state_var],
97
+ [transcription_var, state_var]
98
+ )
99
+
100
+ # Launch with the latest Gradio features
101
+ demo.launch()
102
+
103
+ if __name__ == "__main__":
104
+ main()