Update app.py
Browse files
app.py
CHANGED
|
@@ -176,10 +176,15 @@ def generate_srt_from_sentences(sentence_timestamp, srt_path="default_subtitle.s
|
|
| 176 |
end_time = convert_time_to_srt_format(sentence['end'])
|
| 177 |
srt_file.write(f"{index + 1}\n{start_time} --> {end_time}\n{sentence['text']}\n\n")
|
| 178 |
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
def whisper_subtitle(uploaded_file,Source_Language,max_words_per_subtitle=8):
|
| 182 |
-
global language_dict,base_path
|
| 183 |
#Load model
|
| 184 |
if torch.cuda.is_available():
|
| 185 |
# If CUDA is available, use GPU with float16 precision
|
|
@@ -191,7 +196,7 @@ def whisper_subtitle(uploaded_file,Source_Language,max_words_per_subtitle=8):
|
|
| 191 |
device = "cpu"
|
| 192 |
compute_type = "int8"
|
| 193 |
faster_whisper_model = WhisperModel("deepdml/faster-whisper-large-v3-turbo-ct2",device=device, compute_type=compute_type)
|
| 194 |
-
audio_path=uploaded_file
|
| 195 |
if Source_Language=="Automatic":
|
| 196 |
segments,d = faster_whisper_model.transcribe(audio_path, word_timestamps=True)
|
| 197 |
lang_code=d.language
|
|
@@ -210,7 +215,7 @@ def whisper_subtitle(uploaded_file,Source_Language,max_words_per_subtitle=8):
|
|
| 210 |
|
| 211 |
#setup srt file names
|
| 212 |
base_name = os.path.basename(uploaded_file).rsplit('.', 1)[0][:30]
|
| 213 |
-
save_name = f"{
|
| 214 |
original_srt_name=clean_file_name(save_name)
|
| 215 |
original_txt_name=original_srt_name.replace(".srt",".txt")
|
| 216 |
word_level_srt_name=original_srt_name.replace(".srt","_word_level.srt")
|
|
@@ -239,8 +244,13 @@ import gradio as gr
|
|
| 239 |
import click
|
| 240 |
|
| 241 |
base_path="."
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
source_lang_list = ['Automatic']
|
| 246 |
available_language=language_dict.keys()
|
|
|
|
| 176 |
end_time = convert_time_to_srt_format(sentence['end'])
|
| 177 |
srt_file.write(f"{index + 1}\n{start_time} --> {end_time}\n{sentence['text']}\n\n")
|
| 178 |
|
| 179 |
+
def get_audio_file(uploaded_file):
|
| 180 |
+
global temp_folder
|
| 181 |
+
file_path = os.path.join(temp_folder, os.path.basename(uploaded_file))
|
| 182 |
+
file_path=clean_file_name(file_path)
|
| 183 |
+
shutil.copy(uploaded_file, file_path)
|
| 184 |
+
return file_path
|
| 185 |
|
| 186 |
def whisper_subtitle(uploaded_file,Source_Language,max_words_per_subtitle=8):
|
| 187 |
+
global language_dict,base_path,subtitle_folder
|
| 188 |
#Load model
|
| 189 |
if torch.cuda.is_available():
|
| 190 |
# If CUDA is available, use GPU with float16 precision
|
|
|
|
| 196 |
device = "cpu"
|
| 197 |
compute_type = "int8"
|
| 198 |
faster_whisper_model = WhisperModel("deepdml/faster-whisper-large-v3-turbo-ct2",device=device, compute_type=compute_type)
|
| 199 |
+
audio_path=get_audio_file(uploaded_file)
|
| 200 |
if Source_Language=="Automatic":
|
| 201 |
segments,d = faster_whisper_model.transcribe(audio_path, word_timestamps=True)
|
| 202 |
lang_code=d.language
|
|
|
|
| 215 |
|
| 216 |
#setup srt file names
|
| 217 |
base_name = os.path.basename(uploaded_file).rsplit('.', 1)[0][:30]
|
| 218 |
+
save_name = f"{subtitle_folder}/{base_name}_{src_lang}.srt"
|
| 219 |
original_srt_name=clean_file_name(save_name)
|
| 220 |
original_txt_name=original_srt_name.replace(".srt",".txt")
|
| 221 |
word_level_srt_name=original_srt_name.replace(".srt","_word_level.srt")
|
|
|
|
| 244 |
import click
|
| 245 |
|
| 246 |
base_path="."
|
| 247 |
+
subtitle_folder=f"{base_path}/generated_subtitle"
|
| 248 |
+
temp_folder = f"{base_path}/subtitle_audio"
|
| 249 |
+
|
| 250 |
+
if not os.path.exists(subtitle_folder):
|
| 251 |
+
os.makedirs(subtitle_folder, exist_ok=True)
|
| 252 |
+
if not os.path.exists(temp_folder):
|
| 253 |
+
os.makedirs(temp_folder, exist_ok=True)
|
| 254 |
|
| 255 |
source_lang_list = ['Automatic']
|
| 256 |
available_language=language_dict.keys()
|