awacke1 commited on
Commit
59d8195
1 Parent(s): 1e396cd

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.txt +13 -0
  2. app.py +119 -0
  3. packages.txt +1 -0
  4. requirements.txt +5 -0
  5. streaming.py +66 -0
README.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: 📹NLP Video Transcript SL📝
3
+ emoji: 📹📝
4
+ colorFrom: red
5
+ colorTo: white
6
+ sdk: streamlit
7
+ sdk_version: 1.2.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import deque
2
+ import streamlit as st
3
+ import torch
4
+ from streamlit_player import st_player
5
+ from transformers import AutoModelForCTC, Wav2Vec2Processor
6
+ from streaming import ffmpeg_stream
7
+
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+ player_options = {
10
+ "events": ["onProgress"],
11
+ "progress_interval": 200,
12
+ "volume": 1.0,
13
+ "playing": True,
14
+ "loop": False,
15
+ "controls": False,
16
+ "muted": False,
17
+ "config": {"youtube": {"playerVars": {"start": 1}}},
18
+ }
19
+
20
+ # disable rapid fading in and out on `st.code` updates
21
+ st.markdown("<style>.element-container{opacity:1 !important}</style>", unsafe_allow_html=True)
22
+
23
+ @st.cache(hash_funcs={torch.nn.parameter.Parameter: lambda _: None})
24
+ def load_model(model_path="facebook/wav2vec2-large-robust-ft-swbd-300h"):
25
+ processor = Wav2Vec2Processor.from_pretrained(model_path)
26
+ model = AutoModelForCTC.from_pretrained(model_path).to(device)
27
+ return processor, model
28
+
29
+ processor, model = load_model()
30
+
31
+ def stream_text(url, chunk_duration_ms, pad_duration_ms):
32
+ sampling_rate = processor.feature_extractor.sampling_rate
33
+
34
+ # calculate the length of logits to cut from the sides of the output to account for input padding
35
+ output_pad_len = model._get_feat_extract_output_lengths(int(sampling_rate * pad_duration_ms / 1000))
36
+
37
+ # define the audio chunk generator
38
+ stream = ffmpeg_stream(url, sampling_rate, chunk_duration_ms=chunk_duration_ms, pad_duration_ms=pad_duration_ms)
39
+
40
+ leftover_text = ""
41
+ for i, chunk in enumerate(stream):
42
+ input_values = processor(chunk, sampling_rate=sampling_rate, return_tensors="pt").input_values
43
+
44
+ with torch.no_grad():
45
+ logits = model(input_values.to(device)).logits[0]
46
+ if i > 0:
47
+ logits = logits[output_pad_len : len(logits) - output_pad_len]
48
+ else: # don't count padding at the start of the clip
49
+ logits = logits[: len(logits) - output_pad_len]
50
+
51
+ predicted_ids = torch.argmax(logits, dim=-1).cpu().tolist()
52
+ if processor.decode(predicted_ids).strip():
53
+ leftover_ids = processor.tokenizer.encode(leftover_text)
54
+ # concat the last word (or its part) from the last frame with the current text
55
+ text = processor.decode(leftover_ids + predicted_ids)
56
+ # don't return the last word in case it's just partially recognized
57
+ text, leftover_text = text.rsplit(" ", 1)
58
+ yield text
59
+ else:
60
+ yield leftover_text
61
+ leftover_text = ""
62
+ yield leftover_text
63
+
64
+ def main():
65
+ state = st.session_state
66
+ st.header("Video ASR Streamlit from Youtube Link")
67
+
68
+ with st.form(key="inputs_form"):
69
+
70
+ # Our worlds best teachers on subjects of AI, Cognitive, Neuroscience for our Behavioral and Medical Health
71
+ ytJoschaBach="https://youtu.be/cC1HszE5Hcw?list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&t=8984"
72
+ ytSamHarris="https://www.youtube.com/watch?v=4dC_nRYIDZU&list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&index=2"
73
+ ytJohnAbramson="https://www.youtube.com/watch?v=arrokG3wCdE&list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&index=3"
74
+ ytElonMusk="https://www.youtube.com/watch?v=DxREm3s1scA&list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&index=4"
75
+ ytJeffreyShainline="https://www.youtube.com/watch?v=EwueqdgIvq4&list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&index=5"
76
+ ytJeffHawkins="https://www.youtube.com/watch?v=Z1KwkpTUbkg&list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&index=6"
77
+ ytSamHarris="https://youtu.be/Ui38ZzTymDY?list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L"
78
+ ytSamHarris="https://youtu.be/4dC_nRYIDZU?list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&t=7809"
79
+ ytSamHarris="https://youtu.be/4dC_nRYIDZU?list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&t=7809"
80
+ ytSamHarris="https://youtu.be/4dC_nRYIDZU?list=PLHgX2IExbFouJoqEr8JMF5MbZSbyC91-L&t=7809"
81
+ ytTimelapseAI="https://www.youtube.com/watch?v=63yr9dlI0cU&list=PLHgX2IExbFovQybyfltywXnqZi5YvaSS-"
82
+ state.youtube_url = st.text_input("YouTube URL", ytTimelapseAI)
83
+
84
+
85
+ state.chunk_duration_ms = st.slider("Audio chunk duration (ms)", 2000, 10000, 3000, 100)
86
+ state.pad_duration_ms = st.slider("Padding duration (ms)", 100, 5000, 1000, 100)
87
+ submit_button = st.form_submit_button(label="Submit")
88
+
89
+ if submit_button or "asr_stream" not in state:
90
+ # a hack to update the video player on value changes
91
+ state.youtube_url = (
92
+ state.youtube_url.split("&hash=")[0]
93
+ + f"&hash={state.chunk_duration_ms}-{state.pad_duration_ms}"
94
+ )
95
+ state.asr_stream = stream_text(
96
+ state.youtube_url, state.chunk_duration_ms, state.pad_duration_ms
97
+ )
98
+ state.chunks_taken = 0
99
+
100
+
101
+ state.lines = deque([], maxlen=100) # limit to the last n lines of subs
102
+
103
+
104
+ player = st_player(state.youtube_url, **player_options, key="youtube_player")
105
+
106
+ if "asr_stream" in state and player.data and player.data["played"] < 1.0:
107
+ # check how many seconds were played, and if more than processed - write the next text chunk
108
+ processed_seconds = state.chunks_taken * (state.chunk_duration_ms / 1000)
109
+ if processed_seconds < player.data["playedSeconds"]:
110
+ text = next(state.asr_stream)
111
+ state.lines.append(text)
112
+ state.chunks_taken += 1
113
+ if "lines" in state:
114
+ # print the lines of subs
115
+ st.code("\n".join(state.lines))
116
+
117
+
118
+ if __name__ == "__main__":
119
+ main()
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ --find-links https://download.pytorch.org/whl/cpu/torch_stable.html
2
+ torch==1.10.0+cpu
3
+ transformers
4
+ streamlit-player
5
+ yt-dlp
streaming.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+
3
+ import numpy as np
4
+
5
+
6
+ def ffmpeg_stream(youtube_url, sampling_rate=16_000, chunk_duration_ms=5000, pad_duration_ms=200):
7
+ """
8
+ Helper function to read an audio file through ffmpeg.
9
+ """
10
+ chunk_len = int(sampling_rate * chunk_duration_ms / 1000)
11
+ pad_len = int(sampling_rate * pad_duration_ms / 1000)
12
+ read_chunk_len = chunk_len + pad_len * 2
13
+
14
+ ar = f"{sampling_rate}"
15
+ ac = "1"
16
+ format_for_conversion = "f32le"
17
+ dtype = np.float32
18
+ size_of_sample = 4
19
+
20
+ ffmpeg_command = [
21
+ "ffmpeg",
22
+ "-i",
23
+ "pipe:",
24
+ "-ac",
25
+ ac,
26
+ "-ar",
27
+ ar,
28
+ "-f",
29
+ format_for_conversion,
30
+ "-hide_banner",
31
+ "-loglevel",
32
+ "quiet",
33
+ "pipe:1",
34
+ ]
35
+
36
+ ytdl_command = ["yt-dlp", "-f", "bestaudio", youtube_url, "--quiet", "-o", "-"]
37
+
38
+ try:
39
+ ffmpeg_process = subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=-1)
40
+ ytdl_process = subprocess.Popen(ytdl_command, stdout=ffmpeg_process.stdin)
41
+ except FileNotFoundError:
42
+ raise ValueError("ffmpeg was not found but is required to stream audio files from filename")
43
+
44
+ acc = b""
45
+ leftover = np.zeros((0,), dtype=np.float32)
46
+ while ytdl_process.poll() is None:
47
+ buflen = read_chunk_len * size_of_sample
48
+
49
+ raw = ffmpeg_process.stdout.read(buflen)
50
+ if raw == b"":
51
+ break
52
+
53
+ if len(acc) + len(raw) > buflen:
54
+ acc = raw
55
+ else:
56
+ acc += raw
57
+
58
+ audio = np.frombuffer(acc, dtype=dtype)
59
+ audio = np.concatenate([leftover, audio])
60
+ if len(audio) < pad_len * 2:
61
+ # TODO: handle end of stream better than this
62
+ break
63
+ yield audio
64
+
65
+ leftover = audio[-pad_len * 2 :]
66
+ read_chunk_len = chunk_len