JaMugen commited on
Commit
0d79804
·
1 Parent(s): 28ea672

Update Milestone5API.py

Browse files
Files changed (1) hide show
  1. Milestone5API.py +19 -49
Milestone5API.py CHANGED
@@ -1,9 +1,10 @@
 
 
1
  from pytube import YouTube
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
- import re
4
- import os
5
 
6
- def download_video_transcript(video_url):
7
  try:
8
  video_id = re.search(r"(?<=v=)[\w-]+", video_url)
9
  if video_id:
@@ -25,62 +26,31 @@ def download_video_transcript(video_url):
25
  print('Download completed!')
26
 
27
  transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
28
- transcript = transcript_list.find_generated_transcript(['en']).fetch()
29
 
30
- captions = ""
31
  for i, line in enumerate(transcript):
32
  start_time = line['start']
33
  formatted_time = f"{int(start_time // 60):02d}:{int(start_time % 60):02d}"
34
- captions += f"{formatted_time} {line['text']}\n"
35
 
36
- with open(f'{captions_path}/{modified_title}.txt', 'w', encoding='utf-8') as file:
37
- file.write(captions)
 
38
 
 
 
 
 
 
 
39
  # Delete video file after captions are saved
40
  os.remove(video_file)
41
 
42
- return captions
43
  else:
44
  print("Video ID not found in URL.")
45
- return None
46
  except Exception as e:
47
  print("Error:", e)
48
- return None
49
- from transformers import MarianMTModel, MarianTokenizer
50
- import os
51
-
52
- def translate_text_file(input_text_path, output_text_path, source_lang='en', target_lang='fr', batch_size=8):
53
- model_name = "Helsinki-NLP/opus-mt-en-fr"
54
-
55
- model = MarianMTModel.from_pretrained(model_name)
56
- tokenizer = MarianTokenizer.from_pretrained(model_name)
57
-
58
- def translate_batch(model, tokenizer, sentences):
59
- sentences = [f"{source_lang}: {sentence}" for sentence in sentences]
60
-
61
- input_ids = tokenizer(sentences, return_tensors="pt", padding=True, truncation=True)["input_ids"]
62
- translation_ids = model.generate(input_ids)
63
- translated_texts = tokenizer.batch_decode(translation_ids, skip_special_tokens=True)
64
-
65
- return translated_texts
66
-
67
- def read_text_from_file(file_path):
68
- with open(file_path, 'r', encoding='utf-8') as file:
69
- text = file.readlines()
70
- return text
71
-
72
- def write_text_to_file(file_path, translated_texts):
73
- with open(file_path, 'w', encoding='utf-8') as file:
74
- file.writelines([f"{line}\n" for line in translated_texts])
75
-
76
- input_lines = read_text_from_file(input_text_path)
77
- translated_lines = []
78
-
79
- for i in range(0, len(input_lines), batch_size):
80
- batch = input_lines[i:i + batch_size]
81
- translated_batch = translate_batch(model, tokenizer, batch)
82
- translated_lines.extend(translated_batch)
83
-
84
- write_text_to_file(output_text_path, translated_lines)
85
- return translated_lines
86
-
 
1
+ import os
2
+ import re
3
  from pytube import YouTube
4
  from youtube_transcript_api import YouTubeTranscriptApi
5
+ from transformers import MarianMTModel, MarianTokenizer
 
6
 
7
+ def download_video_transcript(video_url, source_lang='en', target_lang='fr'):
8
  try:
9
  video_id = re.search(r"(?<=v=)[\w-]+", video_url)
10
  if video_id:
 
26
  print('Download completed!')
27
 
28
  transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
29
+ transcript = transcript_list.find_generated_transcript([source_lang]).fetch()
30
 
31
+ original_captions = ""
32
  for i, line in enumerate(transcript):
33
  start_time = line['start']
34
  formatted_time = f"{int(start_time // 60):02d}:{int(start_time % 60):02d}"
35
+ original_captions += f"{formatted_time} {line['text']}\n"
36
 
37
+ original_filename = f'{captions_path}/{modified_title}_original.txt'
38
+ with open(original_filename, 'w', encoding='utf-8') as file:
39
+ file.write(original_captions)
40
 
41
+ # Translation part
42
+ translated_captions = translate_text_file(original_filename,
43
+ f'{captions_path}/{modified_title}_translated.txt',
44
+ source_lang=source_lang,
45
+ target_lang=target_lang)
46
+
47
  # Delete video file after captions are saved
48
  os.remove(video_file)
49
 
50
+ return original_captions, translated_captions, original_filename, f'{captions_path}/{modified_title}_translated.txt'
51
  else:
52
  print("Video ID not found in URL.")
53
+ return None, None, None, None
54
  except Exception as e:
55
  print("Error:", e)
56
+ return None, None, None, None