help me improve the speed and performance of this and write out the full optimization:…
- User
help me improve the speed and performance of this and write out the full optimization:
async def generate_audio(clenedUpResponse, check_weight): global TASK_STATUS
codeif "." in clenedUpResponse: clenedUpResponse = clenedUpResponse.replace(".", " komma ") print(clenedUpResponse) print(type(clenedUpResponse)) combined_audio_seg = AudioSegment.empty() # Generate and concatenate audio for each word audio_seg = AudioSegment.from_mp3(f"Audio/{check_weight}.mp3") combined_audio_seg += audio_seg clenedUpResponse = clenedUpResponse.replace('"', "") try: for text in clenedUpResponse.split(): if text != "komma" and len(text) > 2 and text != "hundra": # making sure that text can never be 3 digits so formats it to 2 digits text = text[:2] if text.startswith("0") and len(text) > 1 and text[1:].isdigit(): # Loop through each character in "text" for extra in text: audio_filename = f"Audio/{extra}.mp3" audio_seg = AudioSegment.from_mp3(audio_filename) combined_audio_seg += audio_seg else: audio_filename = f"Audio/{text}.mp3" audio_seg = AudioSegment.from_mp3(audio_filename) combined_audio_seg += audio_seg except Exception as e: print(e) # Export the combined audio file audio_seg = AudioSegment.from_mp3("Audio/kilo.mp3") combined_audio_seg += audio_seg combined_audio_filename = "audio.mp3" combined_audio_seg.export(combined_audio_filename, format="mp3") # Read the audio file and encode it to Base64 with open(combined_audio_filename, "rb") as audio_file: encoded_audio = base64.b64encode(audio_file.read()).decode() # Return the Base64 string as part of a response return {"audio": encoded_audio} - Assistant
- User
is it possible to optimize it even more?
- Assistant
- User
so the digits are from 0 to 100, how can I easily and cleanly preload them all without writting 1 by 1?
- Assistant
If the digits are from 0 to 100 and you want to preload all audio files without writing them out one by one, you can use a loop to create a dictionary that maps each number to its corresponding preloaded audio segment. Below is an example of how you could achieve this:
python - User
right now it only exports the kilo audio:
async def generate_audio(cleaned_up_response, check_weight): # Use memory stream to avoid writing to disk from io import BytesIO
code# Create an empty BytesIO stream to hold the final audio combined_audio_stream = BytesIO() # Get the preloaded audio segment for check_weight preload_audio_segments[check_weight].export(combined_audio_stream, format="mp3") # Pre-clean the entire response string once cleaned_up_response = cleaned_up_response.replace(".", " komma ").replace('"', "") for text in cleaned_up_response.split(): if text != "komma" and len(text) > 2 and text != "hundra": text = text[:2] if text.startswith("0") and len(text) > 1 and text[1:].isdigit(): for digit in text: preload_audio_segments[digit].export( combined_audio_stream, format="mp3" ) else: preload_audio_segments[text].export(combined_audio_stream, format="mp3") # Append the preloaded "kilo" audio segment preload_audio_segments["kilo"].export(combined_audio_stream, format="mp3") # Encode the combined audio data to base64 directly from the BytesIO stream encoded_audio = base64.b64encode(combined_audio_stream.getvalue()).decode() combined_audio_stream.close() return {"audio": encoded_audio} - Assistant