Skip to content

Commit

Permalink
create a storage service; define bucket_name env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Oct 30, 2024
1 parent e6ce206 commit 2ccca1b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 23 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ ANTHROPIC_API_KEY="your-anthropic-api-key"
GEMINI_API_KEY="your-gemini-api-key"
ELEVENLABS_API_KEY="your-elevenlabs-api-key"

BUCKET_NAME="your-bucket-name"
APP_URL=http://localhost:8501
24 changes: 2 additions & 22 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,21 @@ env:
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
ELEVENLABS_API_KEY: ${{ secrets.ELEVENLABS_API_KEY }}
BUCKET_NAME: ${{ secrets.BUCKET_NAME }}

jobs:
prepare:
runs-on: ubuntu-latest
outputs:
VERSION: ${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.number) || format('main-{0}', steps.prepare_env.outputs.SHORT_SHA) }}
<<<<<<< HEAD
=======
MAIN_OR_TAGGED: ${{ fromJSON(env.MAIN) || (fromJSON(steps.prepare_env.outputs.TAGGED) && steps.prepare_env.outputs.TAG_BRANCH_NAME == 'main') }}
>>>>>>> origin/main
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: prepare_env
run: |
echo "TAGGED=${{ startsWith(github.ref, 'refs/tags/api') }}" >> $GITHUB_OUTPUT
<<<<<<< HEAD
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
=======

SHORT_SHA=$(git rev-parse --short HEAD)
echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_OUTPUT

RAW=$(git branch -r --contains $SHORT_SHA)
TAG_BRANCH_NAME="${RAW##*/}"
echo "TAG_BRANCH_NAME=$TAG_BRANCH_NAME" >> $GITHUB_OUTPUT
>>>>>>> origin/main
lint:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -110,21 +97,14 @@ jobs:
GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}
ANTHROPIC_API_KEY=${{ secrets.ANTHROPIC_API_KEY }}
ELEVENLABS_API_KEY=${{ secrets.ELEVENLABS_API_KEY }}
BUCKET_NAME=${{ secrets.BUCKET_NAME }}
flags: "--allow-unauthenticated --memory=32Gi --cpu=8 --execution-environment=gen2 --concurrency=80 --max-instances=10"

<<<<<<< HEAD
- run: curl -f "${{ steps.deploy.outputs.url }}"
- uses: marocchino/sticky-pull-request-comment@v2
with:
header: app
=======
- name: health-check
run: curl -f "${{ steps.deploy.outputs.url }}"
- uses: marocchino/sticky-pull-request-comment@v2
with:
header: api
>>>>>>> origin/main
message: |
app: ${{ steps.deploy.outputs.url }} (${{ github.event.pull_request.head.sha }})
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ __pycache__
!.env.example

reference_code

keys/
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def main():

st.title("🎧 Audiora")
st.subheader("Listen to anything, anytime, leveraging AI")
st.sidebar.info('A VeedoAI project. (c) 2024 ')
st.sidebar.info("A VeedoAI project. (c) 2024")

# Sidebar for content type selection
st.sidebar.title("Audiocast Info")
Expand Down
1 change: 1 addition & 0 deletions src/env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
GEMINI_API_KEY = environ["GEMINI_API_KEY"]
ELEVENLABS_API_KEY = environ["ELEVENLABS_API_KEY"]

BUCKET_NAME = environ.get("BUCKET_NAME")
APP_URL = environ.get("APP_URL", "http://localhost:8501")
100 changes: 100 additions & 0 deletions src/services/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import re
from dataclasses import dataclass
from io import BytesIO
from typing import Any, Dict
from uuid import uuid4

from google.cloud import storage

from src.env_var import BUCKET_NAME

# Instantiates a client
storage_client = storage.Client()
bucket = storage_client.bucket(BUCKET_NAME)


def listBlobs(prefix):
blobs = bucket.list_blobs(prefix=prefix)
return [blob for blob in blobs]


def download_audio_file(root_cloud_path: str, tmp_path: str):
"""download the video file from the bucket"""
blobs = listBlobs(prefix=root_cloud_path)
pattern = re.compile("audio/mpeg|audio/mp3|audio/wav|audio/ogg|audio/aac")
audio_blobs = [blob for blob in blobs if bool(pattern.match(blob.content_type))]

if not len(audio_blobs):
return None

audio_file = audio_blobs[0]
audio_file.download_to_filename(f"{tmp_path}")

return tmp_path


def check_file_exists(root_path: str, filename: str):
"""check if a file exists in the bucket"""
blobname = f"{root_path}/{filename}"
blobs = listBlobs(prefix=root_path)
return any(blob.name == blobname for blob in blobs)


@dataclass
class UploadItemParams:
content_type: str
cache_control: str = "public, max-age=31536000"
metadata: Dict[str, Any] | None = None


def upload_string_to_gcs(content: str, blobname: str, params: UploadItemParams):
"""upload string content to GCS"""
blob = bucket.blob(blobname)
blob.content_type = "text/plain"
blob.cache_control = params.content_type

if params.metadata:
blob.metadata = {**(blob.metadata or dict()), **params.metadata}

blob.upload_from_string(content)

return f"gs://{BUCKET_NAME}/{blob.name}"


def upload_file_to_gcs(tmp_path: str, blobname: str, params: UploadItemParams):
"""upload file to GCS"""
blob = bucket.blob(blobname)
blob.content_type = params.content_type
blob.cache_control = params.cache_control

if params.metadata:
blob.metadata = {**(blob.metadata or dict()), **params.metadata}

blob.upload_from_filename(tmp_path)

return f"gs://{BUCKET_NAME}/{blob.name}"


def upload_bytes_to_gcs(bytes: BytesIO, blobname: str, params: UploadItemParams):
"""upload bytes to GCS"""
blob = bucket.blob(blobname)
blob.content_type = params.content_type
blob.cache_control = params.cache_control

if params.metadata:
blob.metadata = {**(blob.metadata or dict()), **params.metadata}

blob.upload_from_file(bytes)

return f"gs://{BUCKET_NAME}/{blobname}"


def download_file_from_gcs(blobname: str):
"""
Download any item on GCS to disk
"""
blob = bucket.blob(blobname)
tmp_file_path = f"/tmp/{str(uuid4())}"
blob.download_to_filename(tmp_file_path)

return tmp_file_path

0 comments on commit 2ccca1b

Please sign in to comment.