style: Format code with linter

This commit is contained in:
Paul Gauthier (aider) 2025-03-14 18:47:12 -07:00
parent 278f748c1c
commit 38b8b85ec2

View file

@ -4,12 +4,13 @@ Generate TTS audio files for recording commentary using OpenAI's API.
Usage: python scripts/recording_audio.py path/to/recording.md Usage: python scripts/recording_audio.py path/to/recording.md
""" """
import argparse
import os import os
import re import re
import sys import sys
import argparse
import requests
from pathlib import Path from pathlib import Path
import requests
from dotenv import load_dotenv from dotenv import load_dotenv
# Load environment variables from .env file # Load environment variables from .env file
@ -20,17 +21,19 @@ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
OUTPUT_DIR = "aider/website/assets/audio" OUTPUT_DIR = "aider/website/assets/audio"
VOICE = "onyx" # Options: alloy, echo, fable, onyx, nova, shimmer VOICE = "onyx" # Options: alloy, echo, fable, onyx, nova, shimmer
def extract_recording_id(markdown_file): def extract_recording_id(markdown_file):
"""Extract recording ID from the markdown file path.""" """Extract recording ID from the markdown file path."""
return Path(markdown_file).stem return Path(markdown_file).stem
def extract_commentary(markdown_file): def extract_commentary(markdown_file):
"""Extract commentary markers from markdown file.""" """Extract commentary markers from markdown file."""
with open(markdown_file, 'r') as f: with open(markdown_file, "r") as f:
content = f.read() content = f.read()
# Find Commentary section # Find Commentary section
commentary_match = re.search(r'## Commentary\s+(.*?)(?=##|\Z)', content, re.DOTALL) commentary_match = re.search(r"## Commentary\s+(.*?)(?=##|\Z)", content, re.DOTALL)
if not commentary_match: if not commentary_match:
print(f"No Commentary section found in {markdown_file}") print(f"No Commentary section found in {markdown_file}")
return [] return []
@ -39,11 +42,11 @@ def extract_commentary(markdown_file):
# Extract timestamp-message pairs # Extract timestamp-message pairs
markers = [] markers = []
for line in commentary.split('\n'): for line in commentary.split("\n"):
line = line.strip() line = line.strip()
if line.startswith('- '): if line.startswith("- "):
line = line[2:] # Remove the list marker line = line[2:] # Remove the list marker
match = re.match(r'(\d+):(\d+)\s+(.*)', line) match = re.match(r"(\d+):(\d+)\s+(.*)", line)
if match: if match:
minutes, seconds, message = match.groups() minutes, seconds, message = match.groups()
time_in_seconds = int(minutes) * 60 + int(seconds) time_in_seconds = int(minutes) * 60 + int(seconds)
@ -51,6 +54,7 @@ def extract_commentary(markdown_file):
return markers return markers
def generate_audio_openai(text, output_file): def generate_audio_openai(text, output_file):
"""Generate audio using OpenAI TTS API.""" """Generate audio using OpenAI TTS API."""
if not OPENAI_API_KEY: if not OPENAI_API_KEY:
@ -58,21 +62,14 @@ def generate_audio_openai(text, output_file):
return False return False
url = "https://api.openai.com/v1/audio/speech" url = "https://api.openai.com/v1/audio/speech"
headers = { headers = {"Authorization": f"Bearer {OPENAI_API_KEY}", "Content-Type": "application/json"}
"Authorization": f"Bearer {OPENAI_API_KEY}", data = {"model": "tts-1", "input": text, "voice": VOICE}
"Content-Type": "application/json"
}
data = {
"model": "tts-1",
"input": text,
"voice": VOICE
}
try: try:
response = requests.post(url, headers=headers, json=data) response = requests.post(url, headers=headers, json=data)
if response.status_code == 200: if response.status_code == 200:
with open(output_file, 'wb') as f: with open(output_file, "wb") as f:
f.write(response.content) f.write(response.content)
return True return True
else: else:
@ -82,12 +79,17 @@ def generate_audio_openai(text, output_file):
print(f"Exception during API call: {e}") print(f"Exception during API call: {e}")
return False return False
def main(): def main():
parser = argparse.ArgumentParser(description='Generate TTS audio for recording commentary.') parser = argparse.ArgumentParser(description="Generate TTS audio for recording commentary.")
parser.add_argument('markdown_file', help='Path to the recording markdown file') parser.add_argument("markdown_file", help="Path to the recording markdown file")
parser.add_argument('--voice', default=VOICE, help=f'OpenAI voice to use (default: {VOICE})') parser.add_argument("--voice", default=VOICE, help=f"OpenAI voice to use (default: {VOICE})")
parser.add_argument('--output-dir', default=OUTPUT_DIR, help=f'Output directory (default: {OUTPUT_DIR})') parser.add_argument(
parser.add_argument('--dry-run', action='store_true', help='Print what would be done without generating audio') "--output-dir", default=OUTPUT_DIR, help=f"Output directory (default: {OUTPUT_DIR})"
)
parser.add_argument(
"--dry-run", action="store_true", help="Print what would be done without generating audio"
)
args = parser.parse_args() args = parser.parse_args()
@ -131,5 +133,6 @@ def main():
else: else:
print(f" ✗ Failed to generate audio") print(f" ✗ Failed to generate audio")
if __name__ == "__main__": if __name__ == "__main__":
main() main()