Spaces:
Sleeping
Sleeping
Xudong Xiao
commited on
Commit
·
0f7f63b
1
Parent(s):
61ca873
Add class Task skeleton
Browse filesCo-authored-by: Eason Lu <[email protected]>
Co-authored-by: Ivanfangsc <[email protected]>
Former-commit-id: b9dbcb50abb28983b81a5eb04dfc460d234d84cf
- src/Pigeon.py +1 -1
- src/preprocess/audio_extract.py +13 -0
- src/preprocess/video_download.py +20 -0
- src/task.py +111 -0
src/Pigeon.py
CHANGED
|
@@ -317,7 +317,7 @@ class Pigeon(object):
|
|
| 317 |
logging.info("--------------------Start Preprocessing SRT class--------------------")
|
| 318 |
self.srt.write_srt_file_src(self.srt_path)
|
| 319 |
self.srt.form_whole_sentence()
|
| 320 |
-
self.srt.spell_check_term()
|
| 321 |
self.srt.correct_with_force_term()
|
| 322 |
processed_srt_file_en = str(Path(self.srt_path).with_suffix('')) + '_processed.srt'
|
| 323 |
self.srt.write_srt_file_src(processed_srt_file_en)
|
|
|
|
| 317 |
logging.info("--------------------Start Preprocessing SRT class--------------------")
|
| 318 |
self.srt.write_srt_file_src(self.srt_path)
|
| 319 |
self.srt.form_whole_sentence()
|
| 320 |
+
# self.srt.spell_check_term()
|
| 321 |
self.srt.correct_with_force_term()
|
| 322 |
processed_srt_file_en = str(Path(self.srt_path).with_suffix('')) + '_processed.srt'
|
| 323 |
self.srt.write_srt_file_src(processed_srt_file_en)
|
src/preprocess/audio_extract.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pathlib
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def extract_audio(local_video_path: str, save_dir_path: str = "./downloads/audio") -> str:
|
| 7 |
+
if os.name == 'nt':
|
| 8 |
+
NotImplementedError("Filename extraction on Windows not yet implemented")
|
| 9 |
+
|
| 10 |
+
out_file_name = os.path.basename(local_video_path)
|
| 11 |
+
audio_path_out = save_dir_path.join("/").join(out_file_name)
|
| 12 |
+
subprocess.run(['ffmpeg', '-i', local_video_path, '-f', 'mp3', '-ab', '192000', '-vn', audio_path_out])
|
| 13 |
+
return audio_path_out
|
src/preprocess/video_download.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pytube import YouTube
|
| 2 |
+
import logging
|
| 3 |
+
|
| 4 |
+
def download_youtube_to_local_file(youtube_url: str, local_dir_path: str = "./downloads") -> str:
|
| 5 |
+
yt = YouTube(youtube_url)
|
| 6 |
+
try:
|
| 7 |
+
audio = yt.streams.filter(only_audio=True, file_extension='mp4').order_by('abr').desc().first()
|
| 8 |
+
# video = yt.streams.filter(file_extension='mp4').order_by('resolution').asc().first()
|
| 9 |
+
if audio:
|
| 10 |
+
saved_audio = audio.download(output_path=local_dir_path.join("/audio"))
|
| 11 |
+
logging.info(f"Audio download successful: {saved_audio}")
|
| 12 |
+
return saved_audio
|
| 13 |
+
else:
|
| 14 |
+
logging.error(f"Audio stream not found in {youtube_url}")
|
| 15 |
+
raise f"Audio stream not found in {youtube_url}"
|
| 16 |
+
except Exception as e:
|
| 17 |
+
# print("Connection Error: ", end='')
|
| 18 |
+
print(e)
|
| 19 |
+
raise e
|
| 20 |
+
|
src/task.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.srt_util.srt import SrtScript
|
| 2 |
+
from src.srt_util.srt2ass import srt2ass
|
| 3 |
+
import openai
|
| 4 |
+
import stable_whisper
|
| 5 |
+
import torch
|
| 6 |
+
import whisper
|
| 7 |
+
from pytube import YouTube
|
| 8 |
+
from os import getenv
|
| 9 |
+
from enum import Enum
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from enum import Enum, auto
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
"""
|
| 15 |
+
Youtube link
|
| 16 |
+
- link
|
| 17 |
+
- model
|
| 18 |
+
- output type
|
| 19 |
+
|
| 20 |
+
Video file
|
| 21 |
+
- path
|
| 22 |
+
- model
|
| 23 |
+
- output type
|
| 24 |
+
|
| 25 |
+
Audio file
|
| 26 |
+
- path
|
| 27 |
+
- model
|
| 28 |
+
- output type
|
| 29 |
+
|
| 30 |
+
"""
|
| 31 |
+
"""
|
| 32 |
+
TaskID
|
| 33 |
+
Progress: Enum
|
| 34 |
+
Computing resrouce status
|
| 35 |
+
SRT_Script : SrtScript
|
| 36 |
+
- input module -> initialize (ASR module)
|
| 37 |
+
- Pre-process
|
| 38 |
+
- Translation (%)
|
| 39 |
+
- Post process (time stamp)
|
| 40 |
+
- Output module: SRT_Script --> output(.srt)
|
| 41 |
+
- (Optional) mp4
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
class TaskStatus(Enum):
|
| 45 |
+
INITIALIZING_ASR = (auto(), None)
|
| 46 |
+
PRE_PROCESSING = (auto(), None)
|
| 47 |
+
TRANSLATING = (auto(), 0.0)
|
| 48 |
+
POST_PROCESSING = (auto(), None)
|
| 49 |
+
OUTPUT_MODULE = (auto(), None)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class Task:
|
| 53 |
+
def __init__(self, task_id, audio_path, model, output_type):
|
| 54 |
+
# openai.api_key = getenv("OPENAI_API_KEY")
|
| 55 |
+
self.audio_path = audio_path
|
| 56 |
+
self.model = model
|
| 57 |
+
self.gpu_status = 0
|
| 58 |
+
self.output_type = output_type
|
| 59 |
+
self.task_id = task_id
|
| 60 |
+
self.progress = NotImplemented
|
| 61 |
+
self.SRT_Script = None
|
| 62 |
+
self.local_dump = Path()
|
| 63 |
+
|
| 64 |
+
@staticmethod
|
| 65 |
+
def fromYoutubeLink(youtube_url):
|
| 66 |
+
# convert to audio
|
| 67 |
+
|
| 68 |
+
return Task(...)
|
| 69 |
+
|
| 70 |
+
@staticmethod
|
| 71 |
+
def fromAudioFile():
|
| 72 |
+
#
|
| 73 |
+
return Task(...)
|
| 74 |
+
|
| 75 |
+
@staticmethod
|
| 76 |
+
def fromVideoFile():
|
| 77 |
+
# convert to audio
|
| 78 |
+
return Task(...)
|
| 79 |
+
|
| 80 |
+
# Module 1 ASR: audio --> SRT_script
|
| 81 |
+
def get_srt_class(self, whisper_model='tiny', method="stable"):
|
| 82 |
+
# Instead of using the script_en variable directly, we'll use script_input
|
| 83 |
+
pass
|
| 84 |
+
|
| 85 |
+
# Module 2: SRT preprocess: perform preprocess steps
|
| 86 |
+
def preprocess(self):
|
| 87 |
+
pass
|
| 88 |
+
|
| 89 |
+
def update_translation_progress(self, new_progress):
|
| 90 |
+
if self.progress == TaskStatus.TRANSLATING:
|
| 91 |
+
self.progress = TaskStatus.TRANSLATING.value[0], new_progress
|
| 92 |
+
|
| 93 |
+
# Module 3: perform srt translation
|
| 94 |
+
def translation(self):
|
| 95 |
+
pass
|
| 96 |
+
|
| 97 |
+
# Module 4: perform srt post process steps
|
| 98 |
+
def postprocess(self):
|
| 99 |
+
pass
|
| 100 |
+
|
| 101 |
+
# Module 5: output module
|
| 102 |
+
def output_render(self):
|
| 103 |
+
pass
|
| 104 |
+
|
| 105 |
+
def run_pipeline(self):
|
| 106 |
+
self.get_srt_class()
|
| 107 |
+
self.preprocess()
|
| 108 |
+
self.translation()
|
| 109 |
+
self.postprocess()
|
| 110 |
+
out = self.output_render()
|
| 111 |
+
return out
|