Skip to content

Commit

Permalink
Merge pull request #230 from tattle-made/development
Browse files Browse the repository at this point in the history
merge dev to main
  • Loading branch information
aatmanvaidya authored Mar 21, 2024
2 parents 34f3ed0 + 81a99ed commit ed75278
Show file tree
Hide file tree
Showing 14 changed files with 495 additions and 38 deletions.
14 changes: 12 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.5"
services:
store:
container_name: es
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.0
image: docker.elastic.co/elasticsearch/elasticsearch@sha256:ec72548cf833e58578d8ff40df44346a49480b2c88a4e73a91e1b85ec7ef0d44 # docker.elastic.co/elasticsearch/elasticsearch:8.12.0
volumes:
- ./.docker/es/data:/var/lib/elasticsearch/data
ports:
Expand All @@ -27,7 +27,7 @@ services:
- IPC_LOCK

queue:
image: rabbitmq:3.12.12-management
image: rabbitmq@sha256:e2505f78d58dca8c372cde3930e4d6bee5e02ac9001ce85ece5a11df606c1fa3 # rabbitmq:3.12.12-management
container_name: rabbitmq
hostname: rabbit
volumes:
Expand All @@ -52,6 +52,7 @@ services:
- "GID=${GID:-1000}"
volumes:
- ./src:/usr/app
- venv:/usr/app/venv/
env_file: ./src/development.env
ports:
- 7000:7000
Expand All @@ -69,6 +70,9 @@ services:
context: ./src
dockerfile: Dockerfile
target: debug
args:
- "UID=${UID:-1000}"
- "GID=${GID:-1000}"
env_file: ./src/development.env
command: tail -f /dev/null
depends_on:
Expand All @@ -83,6 +87,9 @@ services:
context: ./src
dockerfile: Dockerfile
target: debug
args:
- "UID=${UID:-1000}"
- "GID=${GID:-1000}"
env_file: ./src/development.env
command: tail -f /dev/null
depends_on:
Expand All @@ -91,6 +98,9 @@ services:
queue:
condition: service_started

volumes:
venv: {}

#networks:
# default:
# external: true
Expand Down
6 changes: 5 additions & 1 deletion src/.env-template
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ ES_IMG_INDEX=imgsearch
ES_TXT_INDEX=txtsearch
ES_VID_INDEX=vidsearch
WSGI_HOST=XXXXX
WSGI_DEBUG=XXXXX
WSGI_DEBUG=XXXXX
PG_HOST=postgres
PG_DB=postgres
PG_USER=tattle
PG_PASS=tattle_pw
24 changes: 21 additions & 3 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ RUN apt-get update \
&& rm -rf /var/lib/apt/lists/*

# Set python user
RUN groupadd -g $GID python \
&& useradd --create-home -r -u $UID -g python python \
&& mkdir /usr/app \
RUN groupadd -f -g $GID python \
&& id -u python >/dev/null 2>&1 || useradd --create-home -r -u $UID -g python python \
&& mkdir -p /usr/app \
&& chown -R python:python /usr/app
WORKDIR /usr/app

Expand Down Expand Up @@ -47,9 +47,27 @@ EXPOSE 7000

#### DEBUG IMAGE ####
FROM base AS debug
ARG UID
ARG GID

RUN apt-get update && apt-get install -y --no-install-recommends vim zsh jq

# Set python user
RUN groupadd -f -g $GID python \
&& id -u python >/dev/null 2>&1 || useradd --create-home -r -u $UID -g python python \
&& mkdir -p /usr/app \
&& chown -R python:python /usr/app
WORKDIR /usr/app

# Set venv path
ENV PATH="/usr/app/venv/bin:$PATH"

# install python packages
RUN pip install --no-cache-dir debugpy nose2

# Set unprivileged user with group membership
USER python:python

CMD python -m debugpy --listen localhost:5678 --wait-for-client -m flask run --debug -h localhost -p 5000

#### PROD IMAGE ####
Expand Down
55 changes: 44 additions & 11 deletions src/core/models/media_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
import tempfile
import boto3

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,21 +69,53 @@ def make_from_file_on_disk(image_path):
def make_from_file_in_memory(image_data: FileStorage):
pass


class VideoFactory:
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
aws_region = os.getenv('AWS_REGION')
aws_bucket = os.getenv('AWS_BUCKET')
session = boto3.Session(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=aws_region
)
s3 = session.client('s3')
@staticmethod
def make_from_url(video_url):
temp_dir = tempfile.gettempdir()
temp_url = video_url.split("?")[0]
file_name = temp_url.split("/")[-1] + ".mp4"
file_path = temp_dir + os.sep + file_name
def download_file_from_s3(bucket_name, file_key, local_file_path):
try:
print("Downloading video from url")
wget.download(video_url, out=file_path)
print("video downloaded")
VideoFactory.s3.download_file(bucket_name, file_key, local_file_path)
print(f"File {file_key} downloaded successfully as {local_file_path}")
except Exception as e:
log.exception("Error downloading video:", e)
raise Exception("Error Downloading Video")
print(f"Error downloading file {file_key}: {e}")

@staticmethod
def make_from_url(video_url):
temp_dir = tempfile.gettempdir()

if video_url.startswith("http"):
temp_url = video_url.split("?")[0]
file_name = temp_url.split("/")[-1] + ".mp4"
file_path = os.path.join(temp_dir, file_name)
try:
print("Downloading video from URL")
wget.download(video_url, out=file_path)
print("Video downloaded")
except Exception as e:
print("Error downloading video:", e)
raise Exception("Error Downloading Video")
else:
bucket_name = VideoFactory.aws_bucket
file_key = video_url
file_name = file_key.split("/")[-1]
file_path = os.path.join(temp_dir, file_name)
try:
print("Downloading video from S3")
VideoFactory.download_file_from_s3(bucket_name, file_key, file_path)
print("Video downloaded")
except Exception as e:
print("Error downloading video from S3:", e)
raise Exception("Error Downloading Video")

return {"path": file_path}

@staticmethod
Expand Down
26 changes: 19 additions & 7 deletions src/core/operators/audio_vec_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ def initialize(param):
global model
global librosa
global np
global contextmanager
global os

import numpy as np
import librosa

# from panns_inference import AudioTagging
from core.operators.audio_cnn_model.inference import AudioTagging
# import os
from contextlib import contextmanager
import os

# load the default model into cpu.
model = AudioTagging(checkpoint_path=None, device="cpu")
Expand All @@ -24,16 +27,25 @@ def normalize(v):

def run(audio_file):
audio = audio_file["path"]
a, _ = librosa.load(audio, sr=44100)
query_audio = a[None, :]
_, emb = model.inference(query_audio)
normalized_v = normalize(emb[0])
return normalized_v

@contextmanager
def audio_load(fname):
a, _ = librosa.load(fname, sr=44100)
try:
yield a
finally:
os.remove(fname)

with audio_load(audio) as audio_var:
query_audio = audio_var[None, :]
_, emb = model.inference(query_audio)
normalized_v = normalize(emb[0])
return normalized_v


# if __name__ == "__main__":
# initialize(param={})
# audio_file_path = {"path": r'sample_data/audio.wav'}
# audio_file_path = {"path": r"core/operators/sample_data/audio.wav"}
# audio_emb = run(audio_file_path)
# audio_emb_list = audio_emb.tolist()
# print(len(audio_emb_list))
6 changes: 3 additions & 3 deletions src/core/operators/audio_vec_embedding_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ uc-micro-py==1.0.3 \
--hash=sha256:d321b92cff673ec58027c04015fcaa8bb1e005478643ff4a500882eaab88c48a \
--hash=sha256:db1dffff340817673d7b466ec86114a9dc0e9d4d9b5ba229d9d60e5c12600cd5
# via linkify-it-py
urllib3==2.2.0 \
--hash=sha256:051d961ad0c62a94e50ecf1af379c3aba230c66c710493493560c0c223c49f20 \
--hash=sha256:ce3711610ddce217e6d113a2732fafad960a03fd0318c91faa79481e35c11224
urllib3==2.2.1 \
--hash=sha256:450b20ec296a467077128bff42b73080516e71b56ff59a60a02bef2232c4fa9d \
--hash=sha256:d0570876c61ab9e520d776c38acbbb5b05a776d3f9ff98a5c8fd5162a444cf19
# via requests
6 changes: 3 additions & 3 deletions src/core/operators/vid_vec_rep_resnet_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ uc-micro-py==1.0.2 \
--hash=sha256:30ae2ac9c49f39ac6dce743bd187fcd2b574b16ca095fa74cd9396795c954c54 \
--hash=sha256:8c9110c309db9d9e87302e2f4ad2c3152770930d88ab385cd544e7a7e75f3de0
# via linkify-it-py
urllib3==2.2.0 \
--hash=sha256:051d961ad0c62a94e50ecf1af379c3aba230c66c710493493560c0c223c49f20 \
--hash=sha256:ce3711610ddce217e6d113a2732fafad960a03fd0318c91faa79481e35c11224
urllib3==2.2.1 \
--hash=sha256:450b20ec296a467077128bff42b73080516e71b56ff59a60a02bef2232c4fa9d \
--hash=sha256:d0570876c61ab9e520d776c38acbbb5b05a776d3f9ff98a5c8fd5162a444cf19
# via requests
Loading

0 comments on commit ed75278

Please sign in to comment.