Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ The `generate()` function provides a complete end-to-end solution:
- `download_outputs` (bool, default True): Whether to download the generated files
- `download_directory` (str, optional): Directory to save downloaded files (defaults to current directory)

Automatic downloads use filenames prefixed with the project ID. Use `response.downloaded_paths` to find saved files; downloading the same project again may overwrite its previous files.

```python
# Generate function - handles everything automatically
response = client.v1.ai_image_generator.generate(
Expand Down
16 changes: 13 additions & 3 deletions magic_hour/helpers/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@


def _compute_download_path(
url: str, download_directory: Union[str, None] = None
url: str,
download_directory: Union[str, None] = None,
project_id: Union[str, None] = None,
) -> str:
url_path = urlparse(url).path
filename = Path(url_path).name
if project_id:
filename = f"{Path(project_id).name}-{filename}"
if download_directory:
return os.path.join(download_directory, filename)
return filename
Expand All @@ -26,6 +30,7 @@ def download_files_sync(
List[models.V1AudioProjectsGetResponseDownloadsItem],
],
download_directory: Union[str, None] = None,
project_id: Union[str, None] = None,
) -> List[str]:
downloaded_paths: List[str] = []

Expand All @@ -35,7 +40,9 @@ def download_files_sync(
download_response.raise_for_status()

download_path = _compute_download_path(
download.url, download_directory=download_directory
download.url,
download_directory=download_directory,
project_id=project_id,
)

with open(download_path, "wb") as f:
Expand All @@ -55,6 +62,7 @@ async def download_files_async(
List[models.V1AudioProjectsGetResponseDownloadsItem],
],
download_directory: Union[str, None] = None,
project_id: Union[str, None] = None,
) -> List[str]:
downloaded_paths: List[str] = []

Expand All @@ -64,7 +72,9 @@ async def download_files_async(
download_response.raise_for_status()

download_path = _compute_download_path(
download.url, download_directory=download_directory
download.url,
download_directory=download_directory,
project_id=project_id,
)

with open(download_path, "wb") as f:
Expand Down
2 changes: 2 additions & 0 deletions magic_hour/resources/v1/audio_projects/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def check_result(
downloaded_paths = download_files_sync(
downloads=api_response.downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1AudioProjectsGetResponseWithDownloads(
Expand Down Expand Up @@ -225,6 +226,7 @@ async def check_result(
downloaded_paths = await download_files_async(
downloads=api_response.downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1AudioProjectsGetResponseWithDownloads(
Expand Down
2 changes: 2 additions & 0 deletions magic_hour/resources/v1/face_detection/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def generate(
downloaded_paths = download_files_sync(
downloads=face_downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1FaceDetectionGetResponseWithDownloads(
Expand Down Expand Up @@ -283,6 +284,7 @@ async def generate(
downloaded_paths = await download_files_async(
downloads=face_downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1FaceDetectionGetResponseWithDownloads(
Expand Down
2 changes: 2 additions & 0 deletions magic_hour/resources/v1/image_projects/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def check_result(
downloaded_paths = download_files_sync(
downloads=api_response.downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1ImageProjectsGetResponseWithDownloads(
Expand Down Expand Up @@ -226,6 +227,7 @@ async def check_result(
downloaded_paths = await download_files_async(
downloads=api_response.downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1ImageProjectsGetResponseWithDownloads(
Expand Down
2 changes: 2 additions & 0 deletions magic_hour/resources/v1/video_projects/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def check_result(
downloaded_paths = download_files_sync(
downloads=api_response.downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1VideoProjectsGetResponseWithDownloads(
Expand Down Expand Up @@ -225,6 +226,7 @@ async def check_result(
downloaded_paths = await download_files_async(
downloads=api_response.downloads,
download_directory=download_directory,
project_id=api_response.id,
)

return V1VideoProjectsGetResponseWithDownloads(
Expand Down
Loading