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())