im having an issue with my google cloud function running in python, it trigger of a fill upload to a bucket, when the function runs it invokes an api which runs a machine learning model on the file then sends back a url for the function to download and store in the bucket. the first part works great, the file is sent to the model. the model starts but then the function times out while waiting for a reponse... this is where im stuck. it times out in under a minute. to my knowledge cloud functions can run for up to 9mins
anyway here's the code:
import time
import requests
import json
from google.cloud import storage
def process_audio_file(data, context):
# TODO: Replace these with your actual credentials
client_id = 'your_client_id'
client_secret = 'your_client_secret'
# Create a storage client
storage_client = storage.Client()
# Make the uploaded file publicly readable
bucket = storage_client.get_bucket(data['bucket'])
blob = bucket.get_blob(data['name'])
blob.make_public()
# Get the public URL of the uploaded audio file
file_url = f"https://storage.googleapis.com/{data['bucket']}/{data['name']}"
# TODO: Replace this with the actual API URL and headers
api_url = "****"
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": ****",
"Connection": "keep-alive",
"Content-Type": "application/json"
}
payload = {"video_url": file_url}
# Transmit the file URL to the audio processing service
response = requests.post(api_url, headers=headers, data=json.dumps(payload))
task_id = response.json()['task_id']
# Poll the API to check the status of the task
while True:
status_response = requests.get(f"*****{task_id}/status/", headers=headers)
if status_response.json()['ended_at']:
break
time.sleep(5) # Wait for 5 seconds between checks
# Download the transcription file
output_url = status_response.json()['outputs']['my-output-1']['url']
output_response = requests.get(output_url)
# TODO: Adjust this to match your bucket and folder structure
output_file_path = f"{task_id}.zip"
transcript_bucket_name = 'your_transcript_bucket_name'
bucket = storage_client.bucket(transcript_bucket_name)
blob = bucket.blob(output_file_path)
# Upload the transcription file to the GCS bucket
blob.upload_from_string(output_response.content)
im waiting the function to say alive until it receive the information from the model api
gcloud functions deploy YOUR_FUNCTION_NAME --timeout=TIMEOUT_DURATION ...