File size: 5,406 Bytes
23be7ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa156da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2148c23
 
3f58796
 
2148c23
3f58796
 
 
 
 
 
23be7ff
3f58796
 
 
e8f10e6
3f58796
 
 
 
 
79a271f
 
3f58796
79a271f
3f58796
22a93ba
3f58796
22a93ba
3f58796
 
 
 
 
 
 
79a271f
 
72de1df
 
79a271f
72de1df
 
2148c23
3f58796
faa126d
23be7ff
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
# 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)