How do you handle AI-generated HD images in mobile apps while avoiding timeouts?

Ghislain Ndeuchi 20 Reputation points
2025-03-31T13:08:04.8633333+00:00

Would appreciate your expertise on how to:

1- Display a fast-loading preview while the full HD image generates

2 Prevent timeouts during HD image loading

3- Optimize the backend (Python) and mobile client sync

Would appreciate sample code or architecture advice for:

  • Backend preview generation/compression
  • Mobile pre-loading and fallback strategies

Thank you

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,382 questions
{count} votes

Accepted answer
  1. Manas Mohanty 3,130 Reputation points Microsoft External Staff
    2025-03-31T15:46:28.62+00:00

    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

    API options

    Additionally, you can enable a caching mechanism on the mobile client to save frequently generated images.

    Prevent Timeouts During HD Image Loading

    1. 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
    

    Reference python code

    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!

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.