Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,11 @@ import os
|
|
| 4 |
import shutil
|
| 5 |
import sys
|
| 6 |
import yt_dlp # для працы з YouTube
|
| 7 |
-
import glob
|
| 8 |
-
import os
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
from sub_tools.media.converter import hls_to_media, media_to_signature, video_to_audio
|
| 11 |
from sub_tools.media.segmenter import segment_audio
|
|
@@ -14,9 +17,6 @@ from sub_tools.system.directory import change_directory
|
|
| 14 |
from sub_tools.system.console import header, success, error
|
| 15 |
from sub_tools.transcribe import transcribe
|
| 16 |
|
| 17 |
-
GEMINI_API_KEY = os.getenv("gembeh")
|
| 18 |
-
print(GEMINI_API_KEY)
|
| 19 |
-
|
| 20 |
# Вызначэнне функцый для дэактывацыі процілеглага поля
|
| 21 |
def update_on_audio_change(audio):
|
| 22 |
if audio is not None:
|
|
@@ -72,14 +72,13 @@ def main_logic(args: Namespace) -> tuple:
|
|
| 72 |
header(f"{step}. Segment Audio")
|
| 73 |
output_str += f"{step}. Segment Audio: Started\n"
|
| 74 |
|
| 75 |
-
print(f"Current working directory before segment_audio: {os.getcwd()}")
|
| 76 |
-
print(f"Audio file path for segmentation: {args.audio_file}")
|
| 77 |
-
if not os.path.exists(args.audio_file):
|
| 78 |
error_msg = f"Error: Audio file not found before segmentation: {args.audio_file}"
|
| 79 |
error(error_msg)
|
| 80 |
return (error_msg, None)
|
| 81 |
-
print(f"Audio file exists before segmentation: {args.audio_file}")
|
| 82 |
-
|
| 83 |
|
| 84 |
segment_audio(args.audio_file, args.audio_segment_prefix, args.audio_segment_format, args.audio_segment_length, args.overwrite)
|
| 85 |
success("Done!")
|
|
@@ -132,7 +131,7 @@ def run_subtools(
|
|
| 132 |
languages,
|
| 133 |
overwrite,
|
| 134 |
retry,
|
| 135 |
-
gemini_api_key,
|
| 136 |
debug,
|
| 137 |
audio_segment_prefix,
|
| 138 |
audio_segment_format,
|
|
@@ -158,7 +157,7 @@ def run_subtools(
|
|
| 158 |
languages=languages,
|
| 159 |
overwrite=overwrite,
|
| 160 |
retry=retry,
|
| 161 |
-
gemini_api_key=
|
| 162 |
debug=debug,
|
| 163 |
audio_segment_prefix=audio_segment_prefix,
|
| 164 |
audio_segment_format=audio_segment_format,
|
|
@@ -176,9 +175,9 @@ def transcribe_youtube(youtube_url: str) -> tuple:
|
|
| 176 |
if not youtube_url:
|
| 177 |
return "Не ўведзена спасылка", None
|
| 178 |
|
| 179 |
-
output_dir = "output_youtube_downloads"
|
| 180 |
-
os.makedirs(output_dir, exist_ok=True)
|
| 181 |
-
outtmpl = os.path.join(output_dir, "temp_youtube_audio.%(ext)s")
|
| 182 |
|
| 183 |
try:
|
| 184 |
ydl_opts = {
|
|
@@ -187,7 +186,7 @@ def transcribe_youtube(youtube_url: str) -> tuple:
|
|
| 187 |
#'quiet': True, # Прыбіраем quiet для адладкі
|
| 188 |
'postprocessors': [{
|
| 189 |
'key': 'FFmpegExtractAudio',
|
| 190 |
-
'preferredcodec': 'mp3',
|
| 191 |
'preferredquality': '0', # лепшая якасць
|
| 192 |
}],
|
| 193 |
}
|
|
@@ -202,11 +201,11 @@ def transcribe_youtube(youtube_url: str) -> tuple:
|
|
| 202 |
|
| 203 |
# Шукаем створаны аўдыёфайл у output_dir
|
| 204 |
audio_files = glob.glob(os.path.join(output_dir, "temp_youtube_audio.*"))
|
| 205 |
-
print(f"Знойдзеныя аўдыёфайлы ў {output_dir}: {audio_files}")
|
| 206 |
if not audio_files:
|
| 207 |
return ("Памылка: файл аўдыё не створаны", None)
|
| 208 |
|
| 209 |
-
audio_file_rel = None
|
| 210 |
for f in audio_files:
|
| 211 |
if f.endswith(".mp3"):
|
| 212 |
audio_file_rel = f
|
|
@@ -214,11 +213,11 @@ def transcribe_youtube(youtube_url: str) -> tuple:
|
|
| 214 |
if audio_file_rel is None:
|
| 215 |
audio_file_rel = audio_files[0]
|
| 216 |
|
| 217 |
-
audio_file_abs = os.path.abspath(audio_file_rel)
|
| 218 |
|
| 219 |
-
print(f"Абраны аўдыёфайл (relative): {audio_file_rel}")
|
| 220 |
-
print(f"Абраны аўдыёфайл (absolute): {audio_file_abs}")
|
| 221 |
-
return ("Спампоўка YouTube аўдыё завершана", audio_file_abs)
|
| 222 |
except Exception as e:
|
| 223 |
err_msg = f"Памылка пры апрацоўцы YouTube (yt_dlp): {e}. Калі ласка, пераканайцеся, што спасылка сапраўдная, і праверце, ці даступныя відэа."
|
| 224 |
print(err_msg)
|
|
@@ -242,7 +241,7 @@ def process_youtube_url(youtube_url):
|
|
| 242 |
|
| 243 |
output_text_file, output_file_file = process_uploaded_file(audio=full_audio_file_path, video=None)
|
| 244 |
|
| 245 |
-
return output_text_file, output_file_file
|
| 246 |
|
| 247 |
|
| 248 |
def process_uploaded_file(audio, video):
|
|
@@ -274,7 +273,7 @@ def process_uploaded_file(audio, video):
|
|
| 274 |
languages="be",
|
| 275 |
overwrite=False,
|
| 276 |
retry=50,
|
| 277 |
-
gemini_api_key=GEMINI_API_KEY,
|
| 278 |
debug=False,
|
| 279 |
audio_segment_prefix="audio_segment",
|
| 280 |
audio_segment_format="mp3",
|
|
@@ -286,8 +285,8 @@ with gr.Blocks() as demo:
|
|
| 286 |
with gr.Tabs():
|
| 287 |
with gr.Tab("Файл"):
|
| 288 |
with gr.Row():
|
| 289 |
-
audio_input = gr.Audio(type="filepath", label="Аўдыёфайл")
|
| 290 |
-
video_input = gr.Video(label="Відэафайл", interactive=True)
|
| 291 |
|
| 292 |
audio_input.change(fn=update_on_audio_change, inputs=audio_input, outputs=video_input)
|
| 293 |
video_input.change(fn=update_on_video_change, inputs=video_input, outputs=audio_input)
|
|
@@ -306,8 +305,7 @@ with gr.Blocks() as demo:
|
|
| 306 |
submit_btn_youtube.click(
|
| 307 |
fn=process_youtube_url,
|
| 308 |
inputs=youtube_url_input,
|
| 309 |
-
outputs=[output_text_youtube, output_file_youtube]
|
| 310 |
)
|
| 311 |
|
| 312 |
-
|
| 313 |
-
demo.launch(allowed_paths=["output_youtube_downloads"])
|
|
|
|
| 4 |
import shutil
|
| 5 |
import sys
|
| 6 |
import yt_dlp # для працы з YouTube
|
| 7 |
+
import glob # Import glob for file searching
|
| 8 |
+
import os # Import os for path manipulation
|
| 9 |
+
|
| 10 |
+
# Атрыманне GEMINI API Key з пераменных асяроддзя
|
| 11 |
+
GEMINI_API_KEY = os.getenv("gembeh")
|
| 12 |
|
| 13 |
from sub_tools.media.converter import hls_to_media, media_to_signature, video_to_audio
|
| 14 |
from sub_tools.media.segmenter import segment_audio
|
|
|
|
| 17 |
from sub_tools.system.console import header, success, error
|
| 18 |
from sub_tools.transcribe import transcribe
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
# Вызначэнне функцый для дэактывацыі процілеглага поля
|
| 21 |
def update_on_audio_change(audio):
|
| 22 |
if audio is not None:
|
|
|
|
| 72 |
header(f"{step}. Segment Audio")
|
| 73 |
output_str += f"{step}. Segment Audio: Started\n"
|
| 74 |
|
| 75 |
+
print(f"Current working directory before segment_audio: {os.getcwd()}") # Debug CWD
|
| 76 |
+
print(f"Audio file path for segmentation: {args.audio_file}") # Debug audio file path
|
| 77 |
+
if not os.path.exists(args.audio_file): # Critical file existence check
|
| 78 |
error_msg = f"Error: Audio file not found before segmentation: {args.audio_file}"
|
| 79 |
error(error_msg)
|
| 80 |
return (error_msg, None)
|
| 81 |
+
print(f"Audio file exists before segmentation: {args.audio_file}") # Debug file existence
|
|
|
|
| 82 |
|
| 83 |
segment_audio(args.audio_file, args.audio_segment_prefix, args.audio_segment_format, args.audio_segment_length, args.overwrite)
|
| 84 |
success("Done!")
|
|
|
|
| 131 |
languages,
|
| 132 |
overwrite,
|
| 133 |
retry,
|
| 134 |
+
gemini_api_key,
|
| 135 |
debug,
|
| 136 |
audio_segment_prefix,
|
| 137 |
audio_segment_format,
|
|
|
|
| 157 |
languages=languages,
|
| 158 |
overwrite=overwrite,
|
| 159 |
retry=retry,
|
| 160 |
+
gemini_api_key=gemini_api_key,
|
| 161 |
debug=debug,
|
| 162 |
audio_segment_prefix=audio_segment_prefix,
|
| 163 |
audio_segment_format=audio_segment_format,
|
|
|
|
| 175 |
if not youtube_url:
|
| 176 |
return "Не ўведзена спасылка", None
|
| 177 |
|
| 178 |
+
output_dir = "output_youtube_downloads" # Define a specific output directory
|
| 179 |
+
os.makedirs(output_dir, exist_ok=True) # Create the directory if it doesn't exist
|
| 180 |
+
outtmpl = os.path.join(output_dir, "temp_youtube_audio.%(ext)s") # Path within the output dir
|
| 181 |
|
| 182 |
try:
|
| 183 |
ydl_opts = {
|
|
|
|
| 186 |
#'quiet': True, # Прыбіраем quiet для адладкі
|
| 187 |
'postprocessors': [{
|
| 188 |
'key': 'FFmpegExtractAudio',
|
| 189 |
+
'preferredcodec': 'mp3', # Download as MP3
|
| 190 |
'preferredquality': '0', # лепшая якасць
|
| 191 |
}],
|
| 192 |
}
|
|
|
|
| 201 |
|
| 202 |
# Шукаем створаны аўдыёфайл у output_dir
|
| 203 |
audio_files = glob.glob(os.path.join(output_dir, "temp_youtube_audio.*"))
|
| 204 |
+
print(f"Знойдзеныя аўдыёфайлы ў {output_dir}: {audio_files}") # Дадаем вывад для адладкі
|
| 205 |
if not audio_files:
|
| 206 |
return ("Памылка: файл аўдыё не створаны", None)
|
| 207 |
|
| 208 |
+
audio_file_rel = None # Relative path
|
| 209 |
for f in audio_files:
|
| 210 |
if f.endswith(".mp3"):
|
| 211 |
audio_file_rel = f
|
|
|
|
| 213 |
if audio_file_rel is None:
|
| 214 |
audio_file_rel = audio_files[0]
|
| 215 |
|
| 216 |
+
audio_file_abs = os.path.abspath(audio_file_rel) # Get absolute path here!
|
| 217 |
|
| 218 |
+
print(f"Абраны аўдыёфайл (relative): {audio_file_rel}") # Дадаем вывад для адладкі
|
| 219 |
+
print(f"Абраны аўдыёфайл (absolute): {audio_file_abs}") # Дадаем вывад для адладкі
|
| 220 |
+
return ("Спампоўка YouTube аўдыё завершана", audio_file_abs) # Return absolute path!
|
| 221 |
except Exception as e:
|
| 222 |
err_msg = f"Памылка пры апрацоўцы YouTube (yt_dlp): {e}. Калі ласка, пераканайцеся, што спасылка сапраўдная, і праверце, ці даступныя відэа."
|
| 223 |
print(err_msg)
|
|
|
|
| 241 |
|
| 242 |
output_text_file, output_file_file = process_uploaded_file(audio=full_audio_file_path, video=None)
|
| 243 |
|
| 244 |
+
return output_text_file, output_file_file # Correct return order: text, file_path
|
| 245 |
|
| 246 |
|
| 247 |
def process_uploaded_file(audio, video):
|
|
|
|
| 273 |
languages="be",
|
| 274 |
overwrite=False,
|
| 275 |
retry=50,
|
| 276 |
+
gemini_api_key=GEMINI_API_KEY,
|
| 277 |
debug=False,
|
| 278 |
audio_segment_prefix="audio_segment",
|
| 279 |
audio_segment_format="mp3",
|
|
|
|
| 285 |
with gr.Tabs():
|
| 286 |
with gr.Tab("Файл"):
|
| 287 |
with gr.Row():
|
| 288 |
+
audio_input = gr.Audio(type="filepath", label="Аўдыёфайл") # Keep type="filepath"
|
| 289 |
+
video_input = gr.Video(label="Відэафайл", interactive=True) # Reset to interactive True initially
|
| 290 |
|
| 291 |
audio_input.change(fn=update_on_audio_change, inputs=audio_input, outputs=video_input)
|
| 292 |
video_input.change(fn=update_on_video_change, inputs=video_input, outputs=audio_input)
|
|
|
|
| 305 |
submit_btn_youtube.click(
|
| 306 |
fn=process_youtube_url,
|
| 307 |
inputs=youtube_url_input,
|
| 308 |
+
outputs=[output_text_youtube, output_file_youtube] # Correct output order
|
| 309 |
)
|
| 310 |
|
| 311 |
+
demo.launch(allowed_paths=["output_youtube_downloads"])
|
|
|