-1

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

3
  • 1
    You might check parameters when the cloud function is being deployed - "By default, a function times out after one minute (60 seconds)" cloud.google.com/functions/docs/configuring/timeout - gcloud functions deploy YOUR_FUNCTION_NAME --timeout=TIMEOUT_DURATION ...
    – al-dann
    May 18 at 16:47
  • 60s is the default. You can set more if required as explained by @al-dann May 18 at 19:25
  • The code that you posted will not work and has syntax errors (missing double-quote). Post the code you are actually using and not demo code found on the Internet. May 18 at 22:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.