from pydub import AudioSegment import os import zipfile import tempfile def cut_audio(input_path, output_path, interval): audio = AudioSegment.from_file(input_path) duration = len(audio) cuts = [audio[i:i + interval] for i in range(0, duration, interval)] with tempfile.TemporaryDirectory() as temp_dir: for i, cut in enumerate(cuts): cut.export(os.path.join(temp_dir, f'cut_{i}.wav'), format="wav") # Create a zip file zip_file_path = os.path.join(output_path, "cuts.zip") with zipfile.ZipFile(zip_file_path, 'w') as zipf: for root, _, files in os.walk(temp_dir): for file in files: zipf.write(os.path.join(root, file), os.path.basename(file)) return zip_file_path def process_audio(audio, interval): # Convert the interval to an integer interval = int(interval) # Temporary directory to store the processed files temp_output_dir = '/content' # This is writable in Hugging Face Spaces # Process the audio file and get the path to the zip file zip_file_path = cut_audio(audio.name, temp_output_dir, interval) # Move the zip file to a publicly accessible location public_zip_path = f'/content/{os.path.basename(zip_file_path)}' os.rename(zip_file_path, public_zip_path) # Create a download link download_link = f'Click here to download' # Return the download link as HTML return download_link