Download src/utils.py from arpy8/autobot: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/arpy8/autobot/resolve/main/src/utils.py
- Command line
-
hf download hf://spaces/arpy8/autobot/src/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/arpy8/autobot/resolve/main/src/utils.py
3.1 kB
| import os | |
| import sys | |
| import json | |
| import random | |
| import logging | |
| import zipfile | |
| import requests | |
| from termcolor import colored | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| def clean_dir(path: str) -> None: | |
| """ | |
| Removes every file in a directory. | |
| Args: | |
| path (str): Path to directory. | |
| Returns: | |
| None | |
| """ | |
| try: | |
| if not os.path.exists(path): | |
| os.mkdir(path) | |
| logger.info(f"Created directory: {path}") | |
| for file in os.listdir(path): | |
| file_path = os.path.join(path, file) | |
| os.remove(file_path) | |
| logger.info(f"Removed file: {file_path}") | |
| logger.info(colored(f"Cleaned {path} directory", "green")) | |
| except Exception as e: | |
| logger.error(f"Error occurred while cleaning directory {path}: {str(e)}") | |
| def fetch_songs(zip_url: str) -> None: | |
| """ | |
| Downloads songs into songs/ directory to use with geneated videos. | |
| Args: | |
| zip_url (str): The URL to the zip file containing the songs. | |
| Returns: | |
| None | |
| """ | |
| try: | |
| logger.info(colored(f" => Fetching songs...", "magenta")) | |
| files_dir = "../Songs" | |
| if not os.path.exists(files_dir): | |
| os.mkdir(files_dir) | |
| logger.info(colored(f"Created directory: {files_dir}", "green")) | |
| else: | |
| return | |
| logger.info(colored(" => Downloaded Songs to ../Songs.", "green")) | |
| except Exception as e: | |
| logger.error(colored(f"Error occurred while fetching songs: {str(e)}", "red")) | |
| def choose_random_song() -> str: | |
| """ | |
| Chooses a random song from the songs/ directory. | |
| Returns: | |
| str: The path to the chosen song. | |
| """ | |
| try: | |
| songs = os.listdir("../Songs") | |
| song = random.choice(songs) | |
| logger.info(colored(f"Chose song: {song}", "green")) | |
| return f"../Songs/{song}" | |
| except Exception as e: | |
| logger.error(colored(f"Error occurred while choosing random song: {str(e)}", "red")) | |
| def check_env_vars() -> None: | |
| """ | |
| Checks if the necessary environment variables are set. | |
| Returns: | |
| None | |
| Raises: | |
| SystemExit: If any required environment variables are missing. | |
| """ | |
| try: | |
| required_vars = ["PEXELS_API_KEY", "TIKTOK_SESSION_ID", "IMAGEMAGICK_BINARY"] | |
| missing_vars = [var + os.getenv(var) for var in required_vars if os.getenv(var) is None or (len(os.getenv(var)) == 0)] | |
| if missing_vars: | |
| missing_vars_str = ", ".join(missing_vars) | |
| logger.error(colored(f"The following environment variables are missing: {missing_vars_str}", "red")) | |
| logger.error(colored("Please consult 'EnvironmentVariables.md' for instructions on how to set them.", "yellow")) | |
| sys.exit(1) # Aborts the program | |
| except Exception as e: | |
| logger.error(f"Error occurred while checking environment variables: {str(e)}") | |
| sys.exit(1) # Aborts the program if an unexpected error occurs | |
| if __name__=="__main__": | |
| print(choose_random_song()) |