nithinraok commited on
Commit
7c6ede0
1 Parent(s): badd660

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from nemo.collections.asr.models import EncDecRNNTBPEModel
2
+ import gradio as gr
3
+ from pydub import AudioSegment
4
+
5
+ device = "cuda" if torch.cuda.is_available() else "cpu"
6
+ MODEL_NAME="nvidia/parakeet-rnnt-1.1b"
7
+
8
+
9
+ def get_transcripts(audio_path):
10
+
11
+ model = EncDecRNNTBPEModel.from_pretrained(model_name="nvidia/parakeet-rnnt-1.1b").to(device)
12
+ model.eval()
13
+ text = model.transcribe([audio_path])[0][0]
14
+ return text
15
+
16
+ article = (
17
+ "<p style='text-align: center'>"
18
+ "<a href='https://huggingface.co/nvidia/parakeet-rnnt-1.1b' target='_blank'>🎙️ Learn more about Parakeet model</a> | "
19
+ "<a href='https://arxiv.org/abs/2305.05084' target='_blank'>📚 FastConformer paper</a> | "
20
+ "<a href='https://github.com/NVIDIA/NeMo' target='_blank'>🧑‍💻 Repository</a>"
21
+ "</p>"
22
+ )
23
+ examples = [
24
+ ["data/conversation.wav"],
25
+ ["data/id10270_5r0dWxy17C8-00001.wav"],
26
+ ]
27
+
28
+ def _return_yt_html_embed(yt_url):
29
+ video_id = yt_url.split("?v=")[-1]
30
+ HTML_str = (
31
+ f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
32
+ " </center>"
33
+ )
34
+ return HTML_str
35
+
36
+ def download_yt_audio(yt_url, filename):
37
+ info_loader = youtube_dl.YoutubeDL()
38
+
39
+ try:
40
+ info = info_loader.extract_info(yt_url, download=False)
41
+ except youtube_dl.utils.DownloadError as err:
42
+ raise gr.Error(str(err))
43
+
44
+ file_length = info["duration_string"]
45
+ file_h_m_s = file_length.split(":")
46
+ file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
47
+
48
+ if len(file_h_m_s) == 1:
49
+ file_h_m_s.insert(0, 0)
50
+ if len(file_h_m_s) == 2:
51
+ file_h_m_s.insert(0, 0)
52
+ file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
53
+
54
+ if file_length_s > YT_LENGTH_LIMIT_S:
55
+ yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
56
+ file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
57
+ raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
58
+
59
+ ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
60
+
61
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
62
+ try:
63
+ ydl.download([yt_url])
64
+ except youtube_dl.utils.ExtractorError as err:
65
+ raise gr.Error(str(err))
66
+
67
+
68
+ def yt_transcribe(yt_url, task, max_filesize=75.0):
69
+ html_embed_str = _return_yt_html_embed(yt_url)
70
+
71
+ with tempfile.TemporaryDirectory() as tmpdirname:
72
+ filepath = os.path.join(tmpdirname, "video.mp4")
73
+ download_yt_audio(yt_url, filepath)
74
+ audio = AudioSegment.from_file(filepath)
75
+ wav_filepath = os.path.join(tmpdirname, "audio.wav")
76
+ audio.export(wav_filepath, format="wav")
77
+
78
+ text = get_transcripts(wav_filepath)
79
+ return html_embed_str, text
80
+
81
+
82
+ demo = gr.Blocks()
83
+
84
+ mf_transcribe = gr.Interface(
85
+ fn=transcribe,
86
+ inputs=[
87
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True)
88
+ ],
89
+ outputs="text",
90
+ layout="horizontal",
91
+ theme="huggingface",
92
+ title="Parakeet RNNT 1.1B: Transcribe Audio",
93
+ description=(
94
+ "Transcribe microphone or audio inputs with the click of a button! Demo uses the"
95
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and NVIDIA NeMo to transcribe audio files"
96
+ " of arbitrary length."
97
+ ),
98
+ allow_flagging="never",
99
+ )
100
+
101
+ file_transcribe = gr.Interface(
102
+ fn=transcribe,
103
+ inputs=[
104
+ gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
105
+ ],
106
+ outputs="text",
107
+ layout="horizontal",
108
+ theme="huggingface",
109
+ title="Parakeet RNNT 1.1B: Transcribe Audio",
110
+ description=(
111
+ "Transcribe microphone or audio inputs with the click of a button! Demo uses the"
112
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and NVIDIA NeMo to transcribe audio files"
113
+ " of arbitrary length."
114
+ ),
115
+ allow_flagging="never",
116
+ )
117
+
118
+ yt_transcribe = gr.Interface(
119
+ fn=yt_transcribe,
120
+ inputs=[
121
+ gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
122
+ ],
123
+ outputs=["html", "text"],
124
+ layout="horizontal",
125
+ theme="huggingface",
126
+ title="Parakeet RNNT 1.1B: Transcribe Audio",
127
+ description=(
128
+ "Transcribe microphone or audio inputs with the click of a button! Demo uses the"
129
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and NVIDIA NeMo to transcribe audio files"
130
+ " of arbitrary length."
131
+ ),
132
+ allow_flagging="never",
133
+ )
134
+
135
+ with demo:
136
+ gr.TabbedInterface([mf_transcribe, file_transcribe], ["Microphone", "Audio file"])
137
+
138
+ demo.launch(enable_queue=True)