deven367 commited on
Commit
e27cc7e
1 Parent(s): 0088d37
Files changed (1) hide show
  1. annotator/utils.py +62 -0
annotator/utils.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ import datetime
3
+ import pandas as pd
4
+ import numpy as np
5
+ import subprocess
6
+ from fastcore.foundation import working_directory
7
+ from pathlib import Path
8
+
9
+
10
+ def start_app():
11
+ subprocess.run(["streamlit", "run", "app.py"])
12
+
13
+
14
+ def get_audio(url: str):
15
+ audio_path = Path("./audio")
16
+ with working_directory(audio_path):
17
+ # subprocess.run(['youtube-dl', '-F', 'bestaudio[ext=m4a]', url])
18
+ subprocess.run(["youtube-dl", "-x", "--audio-format", "mp3", url])
19
+
20
+
21
+ def annotate(audio_src, model_size="tiny"):
22
+ model = whisper.load_model(model_size, device="cpu")
23
+ result = model.transcribe(audio_src)
24
+ return result
25
+
26
+
27
+ def get_time(seconds):
28
+ return "{:0>8}".format(str(datetime.timedelta(seconds=seconds)))
29
+
30
+
31
+ def df_from_result(result):
32
+ df = pd.json_normalize(result["segments"])
33
+ df["start"] = df["start"].apply(get_time)
34
+ df["end"] = df["end"].apply(get_time)
35
+ return df
36
+
37
+
38
+ def find_word_timestamp(df, *words):
39
+ for word in words:
40
+ vals = df["text"].str.find(word).values
41
+ arr = np.where(vals > 1)
42
+ times = df.iloc[arr]["start"].values
43
+ for t in times:
44
+ t = t.split(".")[:-1]
45
+ print(f"{word} is said on {t} timestamp")
46
+
47
+
48
+ def generate_srt(df):
49
+ s = ""
50
+ for i, (start, end, text) in enumerate(df[["start", "end", "text"]].values):
51
+ start = start.replace(".", ",")
52
+ end = end.replace(".", ",")
53
+ s += f"{i}\n"
54
+ s += f"{start} --> {end}\n"
55
+ s += f"{text.strip()}\n\n"
56
+ return s
57
+
58
+
59
+ def write_srt(s, name):
60
+ with open(f"{name}.srt", "w") as f:
61
+ f.write(s)
62
+ f.close()