File size: 7,851 Bytes
23be7ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa156da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd793c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2148c23
 
3f58796
 
2148c23
3f58796
 
 
 
 
 
23be7ff
3f58796
dd793c3
 
 
 
 
3f58796
 
e8f10e6
3f58796
 
 
 
dd793c3
3f58796
79a271f
 
3f58796
79a271f
3f58796
22a93ba
3f58796
22a93ba
3f58796
 
 
 
dd793c3
 
 
 
 
 
 
3f58796
 
 
79a271f
 
72de1df
 
79a271f
dd793c3
72de1df
 
dd793c3
2148c23
3f58796
dd793c3
faa126d
a7f218a
 
dd793c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# import gradio as gr
# import numpy as np
# import torch
# from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor

# model_id = 'openai/whisper-large-v3'
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
# torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
# processor = AutoProcessor.from_pretrained(model_id)

# pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)

# def transcribe_function(new_chunk, state):
#     try:
#         sr, y = new_chunk[0], new_chunk[1]
#     except TypeError:
#         print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
#         return state, "", None

#     y = y.astype(np.float32) / np.max(np.abs(y))

#     if state is not None:
#         state = np.concatenate([state, y])
#     else:
#         state = y

#     result = pipe_asr({"array": state, "sampling_rate": sr}, return_timestamps=False)

#     full_text = result.get("text", "")
    
#     return state, full_text

# with gr.Blocks() as demo:
#     gr.Markdown("# Voice to Text Transcription")
    
#     state = gr.State(None)

#     with gr.Row():
#         with gr.Column():
#             audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', label="Microphone Input")
#         with gr.Column():
#             output_text = gr.Textbox(label="Transcription")

#     audio_input.stream(transcribe_function, inputs=[audio_input, state], outputs=[state, output_text], api_name="SAMLOne_real_time")

# demo.launch(show_error=True)

# import gradio as gr
# import numpy as np
# import torch
# from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor

# model_id = 'openai/whisper-large-v3'
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
# torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
# processor = AutoProcessor.from_pretrained(model_id)

# pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=False)

# def transcribe_function(new_chunk, state):
#     try:
#         sr, y = new_chunk
#     except TypeError:
#         print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
#         return state, "", None

#     y = y.astype(np.float32) / np.max(np.abs(y))

#     if state is not None:
#         state = np.concatenate([state, y])
#     else:
#         state = y

#     result = pipe_asr({"array": state, "sampling_rate": sr}, return_timestamps=False)

#     full_text = result.get("text", "")
    
#     return state, full_text

# with gr.Blocks() as demo:
#     gr.Markdown("# Voice to Text Transcription")
    
#     state = gr.State(None)

#     with gr.Row():
#         with gr.Column():
#             audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', label="Microphone Input")
#         with gr.Column():
#             output_text = gr.Textbox(label="Transcription")

#     audio_input.stream(transcribe_function, inputs=[audio_input, state], outputs=[state, output_text], api_name="SAMLOne_real_time")

# demo.launch(show_error=True)
# import gradio as gr
# import numpy as np
# import torch
# from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor

# model_id = 'openai/whisper-large-v3'
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
# torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
# processor = AutoProcessor.from_pretrained(model_id)

# pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=False)

# def transcribe_function(new_chunk, state):
#     try:
#         sr, y = new_chunk
#     except TypeError:
#         print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
#         return state, "", None

#     y = y.astype(np.float32) / np.max(np.abs(y))

#     if state is not None:
#         state = np.concatenate([state, y])
#     else:
#         state = y

#     result = pipe_asr({"array": state, "sampling_rate": sr}, return_timestamps=False)

#     full_text = result.get("text", "")
    
#     return state, full_text

# with gr.Blocks() as demo:
#     gr.Markdown("# Voice to Text Transcription")
    
#     state = gr.State(None)

#     with gr.Row():
#         with gr.Column():
#             audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', label="Microphone Input")
#         with gr.Column():
#             output_text = gr.Textbox(label="Transcription")

#     audio_input.stream(transcribe_function, inputs=[audio_input, state], outputs=[state, output_text], api_name="SAMLOne_real_time")

# demo.launch(show_error=True)


import gradio as gr
import numpy as np
import torch
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor

model_id = 'openai/whisper-large-v3'
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
processor = AutoProcessor.from_pretrained(model_id)

pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=False)

def ensure_mono(y):
    if len(y.shape) > 1 and y.shape[1] > 1:
        y = np.mean(y, axis=1)
    return y

def transcribe_function(new_chunk, state):
    try:
        sr, y = new_chunk
    except TypeError:
        print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
        return state, "", None

    y = ensure_mono(y)
    y = y.astype(np.float32) / np.max(np.abs(y))

    if state is not None:
        state = np.concatenate([state, y])
    else:
        state = y

    result = pipe_asr({"array": state, "sampling_rate": sr}, return_timestamps=False)

    full_text = result.get("text", "")
    
    return state, full_text

def upload_transcribe(file):
    sr, y = file
    y = ensure_mono(y)
    y = y.astype(np.float32) / np.max(np.abs(y))
    result = pipe_asr({"array": y, "sampling_rate": sr}, return_timestamps=False)
    return result.get("text", "")

with gr.Blocks() as demo:
    gr.Markdown("# Voice to Text Transcription")
    
    state = gr.State(None)

    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', label="Microphone Input")
            audio_upload = gr.Audio(sources="upload", type='numpy', label="Upload Audio File")
        with gr.Column():
            output_text = gr.Textbox(label="Transcription")
            upload_text = gr.Textbox(label="Uploaded Audio Transcription")

    audio_input.stream(transcribe_function, inputs=[audio_input, state], outputs=[state, output_text], api_name="SAMLOne_real_time")
    audio_upload.change(upload_transcribe, inputs=audio_upload, outputs=upload_text)

demo.launch(show_error=True)