Spaces:
Runtime error
Runtime error
Update App_Function_Libraries/Local_File_Processing_Lib.py
Browse files
App_Function_Libraries/Local_File_Processing_Lib.py
CHANGED
|
@@ -1,90 +1,90 @@
|
|
| 1 |
-
# Local_File_Processing_Lib.py
|
| 2 |
-
#########################################
|
| 3 |
-
# Local File Processing and File Path Handling Library
|
| 4 |
-
# This library is used to handle processing local filepaths and URLs.
|
| 5 |
-
# It checks for the OS, the availability of the GPU, and the availability of the ffmpeg executable.
|
| 6 |
-
# If the GPU is available, it asks the user if they would like to use it for processing.
|
| 7 |
-
# If ffmpeg is not found, it asks the user if they would like to download it.
|
| 8 |
-
# The script will exit if the user chooses not to download ffmpeg.
|
| 9 |
-
####
|
| 10 |
-
|
| 11 |
-
####################
|
| 12 |
-
# Function List
|
| 13 |
-
#
|
| 14 |
-
# 1. read_paths_from_file(file_path)
|
| 15 |
-
# 2. process_path(path)
|
| 16 |
-
# 3. process_local_file(file_path)
|
| 17 |
-
# 4. read_paths_from_file(file_path: str) -> List[str]
|
| 18 |
-
#
|
| 19 |
-
####################
|
| 20 |
-
|
| 21 |
-
# Import necessary libraries
|
| 22 |
-
# Import Local
|
| 23 |
-
from App_Function_Libraries.Audio_Transcription_Lib import convert_to_wav
|
| 24 |
-
from App_Function_Libraries.Video_DL_Ingestion_Lib import *
|
| 25 |
-
from App_Function_Libraries.Video_DL_Ingestion_Lib import get_youtube
|
| 26 |
-
from App_Function_Libraries.Utils import normalize_title, create_download_directory
|
| 27 |
-
|
| 28 |
-
#######################################################################################################################
|
| 29 |
-
# Function Definitions
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
def read_paths_from_file(file_path):
|
| 33 |
-
""" Reads a file containing URLs or local file paths and returns them as a list. """
|
| 34 |
-
paths = [] # Initialize paths as an empty list
|
| 35 |
-
with open(file_path, 'r') as file:
|
| 36 |
-
paths = file.readlines()
|
| 37 |
-
return [path.strip() for path in paths]
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def process_path(path):
|
| 41 |
-
""" Decides whether the path is a URL or a local file and processes accordingly. """
|
| 42 |
-
if path.startswith('http'):
|
| 43 |
-
logging.debug("file is a URL")
|
| 44 |
-
# For YouTube URLs, modify to download and extract info
|
| 45 |
-
return get_youtube(path)
|
| 46 |
-
elif os.path.exists(path):
|
| 47 |
-
logging.debug("File is a path")
|
| 48 |
-
# For local files, define a function to handle them
|
| 49 |
-
return process_local_file(path)
|
| 50 |
-
else:
|
| 51 |
-
logging.error(f"Path does not exist: {path}")
|
| 52 |
-
return None
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# FIXME - ingest_text is not used, need to confirm.
|
| 56 |
-
def process_local_file(file_path, ingest_text=False):
|
| 57 |
-
logging.info(f"Processing local file: {file_path}")
|
| 58 |
-
file_extension = os.path.splitext(file_path)[1].lower()
|
| 59 |
-
|
| 60 |
-
if os.path.isfile(file_path):
|
| 61 |
-
if file_path.lower().endswith('.txt'):
|
| 62 |
-
if ingest_text:
|
| 63 |
-
# Treat as content to be ingested
|
| 64 |
-
return os.path.dirname(file_path), {'title': os.path.basename(file_path)}, file_path
|
| 65 |
-
else:
|
| 66 |
-
# Treat as potential list of URLs
|
| 67 |
-
with open(file_path, 'r') as file:
|
| 68 |
-
urls = file.read().splitlines()
|
| 69 |
-
return None, None, urls
|
| 70 |
-
elif file_path.lower().endswith(('.mp4', '.avi', '.mov', '.wav', '.mp3', '.m4a')):
|
| 71 |
-
# Handle video and audio files (existing code)
|
| 72 |
-
title = normalize_title(os.path.splitext(os.path.basename(file_path))[0])
|
| 73 |
-
info_dict = {'title': title}
|
| 74 |
-
logging.debug(f"Creating {title} directory...")
|
| 75 |
-
download_path = create_download_directory(title)
|
| 76 |
-
logging.debug(f"Converting '{title}' to an audio file (wav).")
|
| 77 |
-
audio_file = convert_to_wav(file_path)
|
| 78 |
-
logging.debug(f"'{title}' successfully converted to an audio file (wav).")
|
| 79 |
-
return download_path, info_dict, audio_file
|
| 80 |
-
else:
|
| 81 |
-
logging.error(f"File not found: {file_path}")
|
| 82 |
-
return None, None, None
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
#
|
| 89 |
-
#
|
| 90 |
#######################################################################################################################
|
|
|
|
| 1 |
+
# Local_File_Processing_Lib.py
|
| 2 |
+
#########################################
|
| 3 |
+
# Local File Processing and File Path Handling Library
|
| 4 |
+
# This library is used to handle processing local filepaths and URLs.
|
| 5 |
+
# It checks for the OS, the availability of the GPU, and the availability of the ffmpeg executable.
|
| 6 |
+
# If the GPU is available, it asks the user if they would like to use it for processing.
|
| 7 |
+
# If ffmpeg is not found, it asks the user if they would like to download it.
|
| 8 |
+
# The script will exit if the user chooses not to download ffmpeg.
|
| 9 |
+
####
|
| 10 |
+
|
| 11 |
+
####################
|
| 12 |
+
# Function List
|
| 13 |
+
#
|
| 14 |
+
# 1. read_paths_from_file(file_path)
|
| 15 |
+
# 2. process_path(path)
|
| 16 |
+
# 3. process_local_file(file_path)
|
| 17 |
+
# 4. read_paths_from_file(file_path: str) -> List[str]
|
| 18 |
+
#
|
| 19 |
+
####################
|
| 20 |
+
|
| 21 |
+
# Import necessary libraries
|
| 22 |
+
# Import Local
|
| 23 |
+
from App_Function_Libraries.Audio_Transcription_Lib import convert_to_wav
|
| 24 |
+
from App_Function_Libraries.Video_DL_Ingestion_Lib import *
|
| 25 |
+
from App_Function_Libraries.Video_DL_Ingestion_Lib import get_youtube
|
| 26 |
+
from App_Function_Libraries.Utils import normalize_title, create_download_directory
|
| 27 |
+
|
| 28 |
+
#######################################################################################################################
|
| 29 |
+
# Function Definitions
|
| 30 |
+
#
|
| 31 |
+
|
| 32 |
+
def read_paths_from_file(file_path):
|
| 33 |
+
""" Reads a file containing URLs or local file paths and returns them as a list. """
|
| 34 |
+
paths = [] # Initialize paths as an empty list
|
| 35 |
+
with open(file_path, 'r') as file:
|
| 36 |
+
paths = file.readlines()
|
| 37 |
+
return [path.strip() for path in paths]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def process_path(path):
|
| 41 |
+
""" Decides whether the path is a URL or a local file and processes accordingly. """
|
| 42 |
+
if path.startswith('http'):
|
| 43 |
+
logging.debug("file is a URL")
|
| 44 |
+
# For YouTube URLs, modify to download and extract info
|
| 45 |
+
return get_youtube(path)
|
| 46 |
+
elif os.path.exists(path):
|
| 47 |
+
logging.debug("File is a path")
|
| 48 |
+
# For local files, define a function to handle them
|
| 49 |
+
return process_local_file(path)
|
| 50 |
+
else:
|
| 51 |
+
logging.error(f"Path does not exist: {path}")
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# FIXME - ingest_text is not used, need to confirm.
|
| 56 |
+
def process_local_file(file_path, ingest_text=False):
|
| 57 |
+
logging.info(f"Processing local file: {file_path}")
|
| 58 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
| 59 |
+
|
| 60 |
+
if os.path.isfile(file_path):
|
| 61 |
+
if file_path.lower().endswith('.txt'):
|
| 62 |
+
if ingest_text:
|
| 63 |
+
# Treat as content to be ingested
|
| 64 |
+
return os.path.dirname(file_path), {'title': os.path.basename(file_path)}, file_path
|
| 65 |
+
else:
|
| 66 |
+
# Treat as potential list of URLs
|
| 67 |
+
with open(file_path, 'r') as file:
|
| 68 |
+
urls = file.read().splitlines()
|
| 69 |
+
return None, None, urls
|
| 70 |
+
elif file_path.lower().endswith(('.mp4', '.avi', '.mov', '.wav', '.mp3', '.m4a')):
|
| 71 |
+
# Handle video and audio files (existing code)
|
| 72 |
+
title = normalize_title(os.path.splitext(os.path.basename(file_path))[0])
|
| 73 |
+
info_dict = {'title': title}
|
| 74 |
+
logging.debug(f"Creating {title} directory...")
|
| 75 |
+
download_path = create_download_directory(title)
|
| 76 |
+
logging.debug(f"Converting '{title}' to an audio file (wav).")
|
| 77 |
+
audio_file = convert_to_wav(file_path)
|
| 78 |
+
logging.debug(f"'{title}' successfully converted to an audio file (wav).")
|
| 79 |
+
return download_path, info_dict, audio_file
|
| 80 |
+
else:
|
| 81 |
+
logging.error(f"File not found: {file_path}")
|
| 82 |
+
return None, None, None
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
#
|
| 89 |
+
#
|
| 90 |
#######################################################################################################################
|