Fix Topaz API flow: use separate download endpoint to get download URL

This commit is contained in:
DJP 2025-12-10 21:59:40 -05:00
parent f8e9296e62
commit 31e2ad7688

View file

@ -218,11 +218,10 @@ async def upscale(job_id: str):
if i % 5 == 0:
logger.info(f"Topaz Job {request_id} Status: {topaz_status} (Attempt {i}/{max_attempts})")
if topaz_status == "completed" or (status_data is not None and (status_data.get("download_url") or status_data.get("outputUrl"))):
# Try multiple possible field names for the download URL
output_url = status_data.get("download_url") or status_data.get("outputUrl") or status_data.get("output_url")
if output_url:
break
if topaz_status == "completed":
logger.info(f"Topaz job completed, fetching download URL...")
break
elif topaz_status == "failed":
error_msg = status_data.get("error") or "Unknown error"
raise ValueError(f"Topaz enhancement failed: {error_msg}")
@ -240,10 +239,23 @@ async def upscale(job_id: str):
logger.error(f"Error in polling loop: {loop_e}")
continue
if not output_url:
raise TimeoutError("Topaz upscaling timed out or did not return an output URL.")
if topaz_status != "completed":
raise TimeoutError("Topaz upscaling timed out or did not complete.")
logger.info(f"Topaz output URL received: {output_url[:100] if output_url else 'None'}")
# Call the download endpoint to get the download URL
logger.info(f"Calling download endpoint for request_id: {request_id}")
download_response = await client.get(
f"https://api.topazlabs.com/image/v1/download/{request_id}",
headers={"X-API-Key": settings.topaz_api_key}
)
download_response.raise_for_status()
download_data = download_response.json()
output_url = download_data.get("download_url")
if not output_url:
raise ValueError(f"No download_url in response: {download_data}")
logger.info(f"Topaz download URL received: {output_url[:100] if output_url else 'None'}")
# Download result
img_response = await client.get(output_url)
upscaled_data = img_response.content