Fixing "Ingestion failed. Ingestion did not complete within the expected time" Error in Azure Computer Vision API

Anirudh C A 20 Reputation points
2024-11-27T09:15:19.8033333+00:00

Experiencing an error while implementing video analysis using Azure Computer Vision and GPT-4 Vision. The code was functioning correctly for the first few attempts, but is now returning the following error:
"Ingestion failed. Ingestion did not complete within the expected time."

Attempts have been made to increase both time.sleep and max retries. The code used is the same as the one in the Azure OpenAI playground, where video analysis still works fine. The third function in the code, which previously functioned correctly, is now failing.

Here is the code snippet:

def create_video_index(vision_api_endpoint, vision_api_key, index_name):
    url = f"{vision_api_endpoint}/computervision/retrieval/indexes/{index_name}?api-version=2023-05-01-preview"
    headers = {"Ocp-Apim-Subscription-Key": vision_api_key, "Content-Type": "application/json"}
    data = {
        "features": [
            {"name": "vision", "domain": "surveillance"}
        ]
    }
    response = requests.put(url, headers=headers, json=data)
    return response

def add_video_to_index(vision_api_endpoint, vision_api_key, index_name, video_url, video_id):
    url = f"{vision_api_endpoint}/computervision/retrieval/indexes/{index_name}/ingestions/my-ingestion?api-version=2023-05-01-preview"
    headers = {"Ocp-Apim-Subscription-Key": vision_api_key, "Content-Type": "application/json"}
    data = {
        'videos': [{'mode': 'add', 'documentId': video_id, 'documentUrl': video_url}]
    }
    response = requests.put(url, headers=headers, json=data)
    return response

def wait_for_ingestion_completion(vision_api_endpoint, vision_api_key, index_name, max_retries=30):
    url = f"{vision_api_endpoint}/computervision/retrieval/indexes/{index_name}/ingestions?api-version=2023-05-01-preview"
    headers = {"Ocp-Apim-Subscription-Key": vision_api_key}
    retries = 0
    while retries < max_retries:
        time.sleep(10)
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            state_data = response.json()
            if state_data['value'][0]['state'] == 'Completed':
                print(state_data)
                print('Ingestion completed.')
                return True
            elif state_data['value'][0]['state'] == 'Failed':
                print(state_data)
                print('Ingestion failed.')
                return False
        retries += 1
    return False

# Step 1: Create an Index
response = create_video_index(VISION_API_ENDPOINT, VISION_API_KEY, VIDEO_INDEX_NAME)
print(response.status_code, response.text)
print("Index is created")

# Step 2: Add a video file to the index
response = add_video_to_index(VISION_API_ENDPOINT, VISION_API_KEY, VIDEO_INDEX_NAME, VIDEO_FILE_SAS_URL, VIDEO_DOCUMENT_ID)
print(response.status_code, response.text)
print("Created index for video")

# Step 3: Wait for ingestion to complete
if not wait_for_ingestion_completion(VISION_API_ENDPOINT, VISION_API_KEY, VIDEO_INDEX_NAME):
    print("Ingestion did not complete within the expected time.")

## Chat with GPT-4V

headers = {
    "Content-Type": "application/json",
    "api-key": GPT_4V_KEY,
}

# Payload for the request
payload = {
    "dataSources": [
        {
            "type": "AzureComputerVisionVideoIndex",
            "parameters": {
                "computerVisionBaseUrl": f"{VISION_API_ENDPOINT}/computervision",
                "computerVisionApiKey": VISION_API_KEY,
                "indexName": VIDEO_INDEX_NAME,
                "videoUrls": [VIDEO_FILE_SAS_URL]
            }
        }
    ],
    "enhancements": {
        "video": {
            "enabled": True
        }
    },
    "messages": [
     {
          "role": "system",
          "content": [
               {
                    "type": "text",
                    "text": "You are an AI assistant that helps people find information."
               }
          ]
     },
     {
          "role": "user",
          "content": [
               {
                    "type": "acv_document_id",
                    "acv_document_id": "AOAIChatDocument"
               },
               {
                    "type": "text",
                    "text": "\n \nGenerate transcript"
               }
          ]
     }
],
    "temperature": 0.7,
    "top_p": 0.95,
    "max_tokens": 800
}

# Send request
try:
    response = requests.post(GPT_4V_ENDPOINT, headers=headers, json=payload)
    response.raise_for_status()  # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
except requests.RequestException as e:
    raise SystemExit(f"Failed to make the request. Error: {e}")

# Handle the response as needed (e.g., print or process)
print(response.json())
Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
388 questions
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,362 questions
{count} votes

Accepted answer
  1. SriLakshmi C 1,140 Reputation points Microsoft Vendor
    2024-11-29T15:09:08.1566667+00:00

    Hi Anirudh C A,

    I'm glad to hear that your issue has been resolved. And thanks for sharing the information, which might be beneficial to other community members reading this thread as solution. Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", so I'll reiterate the previous response to an answer in case you'd like to accept the answer. This will help other users who may have a similar query find the solution more easily. 

     Ask: Fixing "Ingestion failed. Ingestion did not complete within the expected time" Error in Azure Computer Vision API

    Solution: That issue is fixed, it was failing since the sas url got expired.

    If you have any further questions or concerns, please don't hesitate to ask. We're always here to help

    Thank you!


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.