Skip to content

Commit

Permalink
[ADD] locations of the detections & signed URL to the mail
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCretois committed Oct 11, 2023
1 parent 73e6f06 commit c5e8a99
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ audioclip/assets/*
**/*/__pycache__/*
singing_in_noise/Figures_MS/*
cloud_analysis/gmail_logs.env
cloud_analysis/key-file.json
cloud_analysis/*.json
logs/logfile.log
10 changes: 8 additions & 2 deletions cloud_analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ gcloud run deploy \
--memory 8Gi \
--region europe-north1 \
--platform managed \
--cpus 4
--cpu 4
```

Be sure to allocate enough memory for the service to be able to process the files and run the model.
Expand Down Expand Up @@ -177,4 +177,10 @@ Explanation of the command:

`--entry-point trigger_audio_analysis`: Specifies that the function to execute is named trigger_audio_analysis within your source code.

`--source .`: Specifies that the source code that should be deployed is in the current directory.
`--source .`: Specifies that the source code that should be deployed is in the current directory.

## USEFUL POINTS

- It is possible to create a **signed URL** which can be included in the output email and that allow the end user to download the file even though he / she does not have credentials to access the Google Cloud Bucket
- In the `config.json` file of the audio recorder, add **project_id** as the location so that the folder created has the name of the location. When sending the email, it is then possible to infer **where** the detections have been made.

49 changes: 47 additions & 2 deletions cloud_analysis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from utils.utils import AudioList

from google.cloud import storage
from google.oauth2 import service_account
import datetime

import io

import logging
Expand Down Expand Up @@ -108,6 +111,38 @@ def fetch_audio_data(bucket_name, blob_name):

return wav_file_object

def generate_signed_url(bucket_name, blob_name, expiration_time=600):
"""
Generates a signed URL for a GCS object.
Parameters:
bucket_name (str): Name of the GCS bucket.
blob_name (str): Name of the blob (file) in the GCS bucket.
expiration_time (int): Time, in seconds, until the signed URL expires.
Returns:
str: A signed URL to download the object.
"""
# Path to the service account key file
key_path = '/app/cloud_analysis/key-file.json'

# Initialize a GCS client
credentials = service_account.Credentials.from_service_account_file(key_path)
client = storage.Client(credentials=credentials)

# Get the bucket and blob objects
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)

# Generate the signed URL
url = blob.generate_signed_url(
version="v4",
expiration=datetime.timedelta(seconds=expiration_time),
method="GET"
)

return url


def analyseAudioFile(
audio_file_object, min_hr, min_conf, batch_size=1, num_workers=2,
Expand Down Expand Up @@ -178,7 +213,7 @@ def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name
#u"analysisId": analysis_id,
# Add any other information you want to record here
})

return count


Expand All @@ -195,7 +230,17 @@ def process_audio_endpoint():
results = on_process_audio(audio_id, audio_rec, bucket_name, blob_name, hr, conf)

if results > 0:
send_email("Snowmobile Detection Alert", f"{results} snowmobile detections were made in the audio file!")
# Create a signed URL
download_url = generate_signed_url(bucket_name, blob_name)

# Extract folder name (location) from the blob name
location = blob_name.split("/")[0]

# Write and send the email
email_body = f"{results} snowmobile detections were made in the audio file!\n"
email_body += f"Detections come from: {location}\n"
email_body += f"Download the audio file here: {download_url}"
send_email("Snowmobile Detection Alert", email_body)

return jsonify({"message": "Audio processing completed!"})

Expand Down
4 changes: 2 additions & 2 deletions cloud_function_directory/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def fetch_service_account_key(bucket_name, blob_name):
def trigger_audio_analysis(data, context):
file_name = data['name']
bucket_name = data['bucket']
harmonic_ratio = 0.05
confidence = 0.80
harmonic_ratio = 0.1
confidence = 0.95

# URL of your Cloud Run service
cloud_run_url = "https://model-4uhtnq5xla-lz.a.run.app/process-audio"
Expand Down

0 comments on commit c5e8a99

Please sign in to comment.