File size: 2,612 Bytes
4df5ada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "openai/whisper-large-v3-turbo"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    torch_dtype=torch_dtype,
    device=device,
)

# dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
# sample = dataset[0]["audio"]
# result = pipe(sample)
# transcript = result["text"]

import os
import gradio as gr

def launch(input):
    out = pipe(input)
    result = pipe(out[0])
    transcript = result["text"]

    # context_str = out[0]['generated_text']
    # translate_str = translate(context_str, 'en', 'sq')
    return translate_str

iface = gr.Interface(launch,
                     inputs=gr.Audio(label="Audio", source="microphone", type="filepath", elem_id='audio'),
                     outputs="text")

iface.launch(share=True)
# iface.launch(share=True, 
#              server_port=int(os.environ['PORT1']))

iface.close()


# def click_js():
#     return """function audioRecord() {
#     var xPathRes = document.evaluate ('//*[@id="audio"]//button', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); 
#     xPathRes.singleNodeValue.click();}"""


# def action(btn):
#     """Changes button text on click"""
#     if btn == 'Speak': return 'Stop'
#     else: return 'Speak'


# def check_btn(btn):
#     """Checks for correct button text before invoking transcribe()"""
#     if btn != 'Speak': raise Exception('Recording...')


# def transcribe():
#     return 'Success'


# with gr.Blocks() as demo:
#     msg = gr.Textbox()
#     audio_box = gr.Audio(label="Audio", source="microphone", type="filepath", elem_id='audio')

#     with gr.Row():
#         audio_btn = gr.Button('Speak')
#         clear = gr.Button("Clear")

#     audio_btn.click(fn=action, inputs=audio_btn, outputs=audio_btn).\
#               then(fn=lambda: None, _js=click_js()).\
#               then(fn=check_btn, inputs=audio_btn).\
#               success(fn=transcribe, outputs=msg)

#     clear.click(lambda: None, None, msg, queue=False)

# demo.queue().launch(debug=True)

# print(result["text"])