Gotenks1893 commited on
Commit
168a18b
·
1 Parent(s): 8034db9

Upload 8 files

Browse files
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import youtubetranscript
3
+ import youtubevideo
4
+ import transcriptFromWhisper
5
+ import sourcetoTarget
6
+ import textToSpeech
7
+ from pytube import YouTube
8
+
9
+ link = st.text_input("Youtube Link")
10
+ st.selectbox("Target Language", ["German"])
11
+ btnTranslate = st.button("Translate video")
12
+
13
+
14
+ if btnTranslate:
15
+ video = YouTube(link)
16
+ videoId = video.video_id
17
+ wasSuccessful = youtubetranscript.getTranscript(videoId)
18
+ if(wasSuccessful):
19
+ fname = video.video_id + ".mp4"
20
+ youtubevideo.Download(video, fname)
21
+
22
+ st.video(f"videos\{videoId}.mp4")
23
+ cols = st.columns(3)
24
+ cols[0].header(f'Original')
25
+ cols[1].header(f'Whisper')
26
+ cols[2].header(f'German')
27
+ file_path = f"transcripts/{videoId}.txt"
28
+ with open(file_path, 'r', encoding='utf8') as f:
29
+ cols[0].write(f.read())
30
+
31
+ transcriptFromWhisper.getWhisperTranscript(videoId)
32
+ file_path = f"whisper/{videoId}.txt"
33
+ with open(file_path, 'r') as f:
34
+ text = f.read()
35
+ text = text.replace("$","dollar")
36
+ print(text)
37
+ cols[1].write(text)
38
+
39
+ sourcetoTarget.englishToGerman(videoId)
40
+ file_path = f"translatedTranscripts/{videoId}.txt"
41
+ with open(file_path, 'r', encoding='utf8') as f:
42
+ cols[2].write(f.read())
43
+
44
+ textToSpeech.ttsSingleFile(videoId)
45
+ st.audio(f"tts\{videoId}.wav")
requirements.txt ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.0.0
2
+ aiohttp==3.9.1
3
+ aiosignal==1.3.1
4
+ altair==5.2.0
5
+ annotated-types==0.6.0
6
+ anyascii==0.3.2
7
+ attrs==23.1.0
8
+ audioread==3.0.1
9
+ Babel==2.13.1
10
+ bangla==0.0.2
11
+ blinker==1.7.0
12
+ blis==0.7.11
13
+ bnnumerizer==0.0.2
14
+ bnunicodenormalizer==0.1.6
15
+ cachetools==5.3.2
16
+ catalogue==2.0.10
17
+ certifi==2023.11.17
18
+ cffi==1.16.0
19
+ charset-normalizer==3.3.2
20
+ click==8.1.7
21
+ cloudpathlib==0.16.0
22
+ colorama==0.4.6
23
+ confection==0.1.4
24
+ contourpy==1.2.0
25
+ coqpit==0.0.17
26
+ cycler==0.12.1
27
+ cymem==2.0.8
28
+ Cython==3.0.6
29
+ dateparser==1.1.8
30
+ decorator==5.1.1
31
+ docopt==0.6.2
32
+ einops==0.7.0
33
+ encodec==0.1.1
34
+ filelock==3.13.1
35
+ Flask==3.0.0
36
+ fonttools==4.45.1
37
+ frozenlist==1.4.0
38
+ fsspec==2023.10.0
39
+ g2pkk==0.1.2
40
+ gitdb==4.0.11
41
+ GitPython==3.1.40
42
+ google-auth==2.24.0
43
+ google-auth-oauthlib==1.1.0
44
+ grpcio==1.59.3
45
+ gruut==2.2.3
46
+ gruut-ipa==0.13.0
47
+ gruut-lang-de==2.0.0
48
+ gruut-lang-en==2.0.0
49
+ gruut-lang-es==2.0.0
50
+ gruut-lang-fr==2.0.2
51
+ hangul-romanize==0.1.0
52
+ huggingface-hub==0.19.4
53
+ idna==3.6
54
+ importlib-metadata==6.8.0
55
+ inflect==7.0.0
56
+ itsdangerous==2.1.2
57
+ jamo==0.4.1
58
+ jieba==0.42.1
59
+ Jinja2==3.1.2
60
+ joblib==1.3.2
61
+ jsonlines==1.2.0
62
+ jsonschema==4.20.0
63
+ jsonschema-specifications==2023.11.2
64
+ kiwisolver==1.4.5
65
+ langcodes==3.3.0
66
+ lazy_loader==0.3
67
+ librosa==0.10.1
68
+ llvmlite==0.41.1
69
+ Markdown==3.5.1
70
+ markdown-it-py==3.0.0
71
+ MarkupSafe==2.1.3
72
+ matplotlib==3.8.2
73
+ mdurl==0.1.2
74
+ more-itertools==10.1.0
75
+ mpmath==1.3.0
76
+ msgpack==1.0.7
77
+ multidict==6.0.4
78
+ murmurhash==1.0.10
79
+ networkx==2.8.8
80
+ nltk==3.8.1
81
+ num2words==0.5.13
82
+ numba==0.58.1
83
+ numpy==1.26.2
84
+ oauthlib==3.2.2
85
+ openai-whisper @ git+https://github.com/openai/whisper.git@e58f28804528831904c3b6f2c0e473f346223433
86
+ packaging==23.2
87
+ pandas==1.5.3
88
+ Pillow==10.1.0
89
+ platformdirs==4.0.0
90
+ pooch==1.8.0
91
+ preshed==3.0.9
92
+ protobuf==4.23.4
93
+ psutil==5.9.6
94
+ pyarrow==14.0.1
95
+ pyasn1==0.5.1
96
+ pyasn1-modules==0.3.0
97
+ pycparser==2.21
98
+ pydantic==2.5.2
99
+ pydantic_core==2.14.5
100
+ pydeck==0.8.1b0
101
+ Pygments==2.17.2
102
+ pynndescent==0.5.11
103
+ pyparsing==3.1.1
104
+ pypinyin==0.49.0
105
+ pysbd==0.3.4
106
+ python-crfsuite==0.9.9
107
+ python-dateutil==2.8.2
108
+ pytube==15.0.0
109
+ pytz==2023.3.post1
110
+ PyYAML==6.0.1
111
+ referencing==0.31.1
112
+ regex==2023.10.3
113
+ requests==2.31.0
114
+ requests-oauthlib==1.3.1
115
+ rich==13.7.0
116
+ rpds-py==0.13.2
117
+ rsa==4.9
118
+ safetensors==0.4.1
119
+ scikit-learn==1.3.2
120
+ scipy==1.11.4
121
+ six==1.16.0
122
+ smart-open==6.4.0
123
+ smmap==5.0.1
124
+ soundfile==0.12.1
125
+ soxr==0.3.7
126
+ spacy==3.7.2
127
+ spacy-legacy==3.0.12
128
+ spacy-loggers==1.0.5
129
+ srsly==2.4.8
130
+ streamlit==1.29.0
131
+ SudachiDict-core==20230927
132
+ SudachiPy==0.6.7
133
+ sympy==1.12
134
+ tenacity==8.2.3
135
+ tensorboard==2.15.1
136
+ tensorboard-data-server==0.7.2
137
+ thinc==8.2.1
138
+ threadpoolctl==3.2.0
139
+ tiktoken==0.5.1
140
+ tokenizers==0.15.0
141
+ toml==0.10.2
142
+ toolz==0.12.0
143
+ torch==2.1.1
144
+ torchaudio==2.1.1
145
+ tornado==6.4
146
+ tqdm==4.66.1
147
+ trainer==0.0.32
148
+ transformers==4.35.2
149
+ TTS==0.21.2
150
+ typer==0.9.0
151
+ typing_extensions==4.8.0
152
+ tzdata==2023.3
153
+ tzlocal==5.2
154
+ umap-learn==0.5.5
155
+ Unidecode==1.3.7
156
+ urllib3==2.1.0
157
+ validators==0.22.0
158
+ wasabi==1.1.2
159
+ watchdog==3.0.0
160
+ weasel==0.3.4
161
+ Werkzeug==3.0.1
162
+ whisper==1.1.10
163
+ yarl==1.9.3
164
+ youtube-transcript-api==0.6.1
165
+ zipp==3.17.0
sourcetoTarget.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Transformers installation
2
+ # pip install transformers
3
+
4
+ import os
5
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
6
+
7
+
8
+ # define tokenizer and model
9
+ tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
10
+ model = AutoModelForSeq2SeqLM.from_pretrained(
11
+ "facebook/nllb-200-distilled-600M")
12
+
13
+
14
+ # translation function for single sentence
15
+ def translateSentence(sentence):
16
+ inputs = tokenizer(sentence, return_tensors="pt")
17
+ translated_tokens = model.generate(
18
+ **inputs, forced_bos_token_id=tokenizer.lang_code_to_id["deu_Latn"], max_length=400
19
+ )
20
+ translatedSentence = tokenizer.batch_decode(
21
+ translated_tokens, skip_special_tokens=True)[0]
22
+ return translatedSentence
23
+
24
+ def translateAllTranscripts():
25
+
26
+ # source text directory
27
+ directory = os.fsencode("whisper")
28
+
29
+ # go through source text files
30
+ # translate them to target language (German)
31
+ #  save translated text files to translatedTranscripts file
32
+ for file in os.listdir(directory):
33
+ filename = os.fsdecode(file)
34
+ print(f'reading {filename}')
35
+ if (filename.endswith(".txt")):
36
+ file_path = f"whisper/{filename}"
37
+ with open(file_path, 'r') as f:
38
+ sourceText = f.read() # read source text
39
+ sentences = sourceText.split('.')
40
+ translationArr = []
41
+ for sentence in sentences:
42
+ #  translate each sentence
43
+ translatedSentence = translateSentence(sentence)
44
+ translationArr.append(translatedSentence)
45
+ # join translated sentences
46
+ translation = " ".join(translationArr)
47
+ # save translated transcript
48
+ fname = "translatedTranscripts/" + filename
49
+ with open(fname, "w", encoding="UTF8") as ft:
50
+ ft.write(translation)
51
+ print("written to: " + fname)
52
+
53
+
54
+ def englishToGerman(videoId):
55
+ file_path = f"whisper/{videoId}.txt"
56
+ with open(file_path, 'r') as f:
57
+ sourceText = f.read() # read source text
58
+ sentences = sourceText.split('.')
59
+ translationArr = []
60
+ for sentence in sentences:
61
+ #  translate each sentence
62
+ translatedSentence = translateSentence(sentence)
63
+ translationArr.append(translatedSentence)
64
+ # join translated sentences
65
+ translation = " ".join(translationArr)
66
+ # save translated transcript
67
+ fname = f"translatedTranscripts/{videoId}.txt"
68
+ with open(fname, "w", encoding="UTF8") as ft:
69
+ ft.write(translation)
70
+ print("written to: " + fname)
textToSpeech.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !pip install tts
2
+ # !pip install python-espeak-ng
3
+
4
+ from TTS.api import TTS
5
+ import os
6
+
7
+ # Init TTS with the target model name
8
+ tts = TTS(model_name="tts_models/de/thorsten/tacotron2-DDC", progress_bar=False)
9
+
10
+
11
+ def ttsForAllFiles():
12
+ # source text directory
13
+ directory = os.fsencode("translatedTranscripts")
14
+
15
+ # go through source text files
16
+ for file in os.listdir(directory):
17
+ filename = os.fsdecode(file)
18
+ print(f'reading {filename}')
19
+ if (filename.endswith(".txt")):
20
+ file_path = f"translatedTranscripts/{filename}"
21
+ with open(file_path, 'r', encoding='utf8') as f:
22
+ sourceText = f.read() # read source text
23
+ fname = filename.replace(".txt", "")
24
+ # Run TTS
25
+ tts.tts_to_file(text=sourceText, file_path=f"tts/{fname}.wav")
26
+
27
+
28
+ def ttsSingleFile(videoId):
29
+ file_path = f"translatedTranscripts/{videoId}.txt"
30
+ with open(file_path, 'r', encoding='utf8') as f:
31
+ sourceText = f.read() # read source text
32
+ # Run TTS
33
+ tts.tts_to_file(text=sourceText, file_path=f"tts/{videoId}.wav")
transcriptFromWhisper.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ import os
3
+
4
+ def getAllWhisperTranscripts():
5
+ directory = os.fsencode("videos")
6
+
7
+ for file in os.listdir(directory):
8
+ filename = os.fsdecode(file)
9
+ print(filename)
10
+
11
+ model = whisper.load_model("tiny")
12
+ result = model.transcribe("videos/" + filename, fp16=False)
13
+ fname = "whisper/" + filename
14
+ fname = fname.replace(".mp4", ".txt")
15
+ with open(fname, "w") as f:
16
+ f.write(result["text"])
17
+ print("written to: " + fname)
18
+
19
+ def getWhisperTranscript(videoId):
20
+ model = whisper.load_model("tiny")
21
+ result = model.transcribe(f"videos/{videoId}.mp4", fp16=False)
22
+ fname = f"whisper/{videoId}.txt"
23
+ with open(fname, "w") as f:
24
+ f.write(result["text"])
25
+ print("written to: " + fname)
videosFromPlaylist.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import youtubetranscript
2
+ import youtubevideo
3
+ from pytube import Playlist
4
+
5
+ playlist = Playlist('https://www.youtube.com/playlist?list=PLI1yx5Z0Lrv77D_g1tvF9u3FVqnrNbCRL')
6
+ print('Number of videos in playlist: %s' % len(playlist.video_urls))
7
+ videoCount = 10
8
+
9
+ i = 1
10
+
11
+ for video in playlist.videos:
12
+ print(f'VIDEO {i}:')
13
+ print(video.title + ": " + video.video_id)
14
+ wasSuccessful = youtubetranscript.getTranscript(video.video_id)
15
+ if(wasSuccessful):
16
+ fname = video.video_id + ".mp4"
17
+ youtubevideo.Download(video, fname)
18
+ i = i + 1
19
+ if(i > videoCount):
20
+ break
21
+
22
+
youtubetranscript.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from youtube_transcript_api import YouTubeTranscriptApi
2
+
3
+ def getTranscript(videoId):
4
+ try:
5
+ srt = YouTubeTranscriptApi.get_transcript(videoId)
6
+ fname = "transcripts/" + videoId + ".txt"
7
+ with open(fname, "w") as f:
8
+ # iterating through each element of list srt
9
+ for i in srt:
10
+ # writing each element of srt on a new line
11
+ f.write("{}\n".format(i["text"]).replace("\xa0", " ").replace(" — ", " "))
12
+ return True
13
+ except:
14
+ print("A transcript error has occurred")
15
+ return False
16
+
youtubevideo.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytube import YouTube
2
+
3
+ SAVE_DIRECTORY = "./videos"
4
+
5
+ def Download(video, fname):
6
+ video = video.streams.get_highest_resolution()
7
+ try:
8
+ video.download(SAVE_DIRECTORY, filename=fname)
9
+ print("Download is completed successfully")
10
+ except:
11
+ print("A video error has occurred")
12
+
13
+