Hi Ghislain Ndeuchi,
Here are best practices as per your queries:
Display a Fast-Loading Preview While the Full HD Image Generates
You can set the Quality (Standard or HD) and Size from the API body parameter in REST API calls or change the resolution using the Pillow image library. You can find more details in the
Additionally, you can enable a caching mechanism on the mobile client to save frequently generated images.
Prevent Timeouts During HD Image Loading
- Adjust Timeout Settings and Enable Retry Logic in Python Code: You can pre-estimate token usage for different sizes and qualities and adjust your total tokens per minute accordingly. Here is a reference Python code snippet:
import asyncio
import requests
async def download_image_with_retry(url, path, retries=3, timeout=10):
for attempt in range(retries):
try:
response = await asyncio.to_thread(requests.get, url, timeout=timeout)
response.raise_for_status() # Raise an exception for HTTP errors
with open(path, "wb") as image_file:
image_file.write(response.content)
return True
except (requests.RequestException, IOError) as e:
print(f"Attempt {attempt + 1} failed: {e}")
await asyncio.sleep(2) # Wait before retrying
return False
Fallback Strategies
Exception Handling: Catch exceptions (server/user-specific) and show a proper message to the user, prompting them to change inputs or try again later.
Lower Resolution Images: Show a lower resolution image if network-related or hardware-related issues occurs.
Alternate Region API: Route traffic to another alternate region API in case of regional outages or server errors. You can use global standard deployment to route traffic to the nearest stable data centers or provision throughput units to ensure high availability of quota.
Backend Preview Generation/Compression
While the image is generating, you can set an image loading GIF to prompt users to wait for image generation.
Mobile pre-loading
Please check your respective mobile code SDK GitHub for pre-loading.
Attached is a relevant document for image pre-loading in Flutter.
Hope this helps.
Thank you!