Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
githubering182 committed Dec 12, 2024
1 parent 7e1b647 commit 4842ea8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
19 changes: 3 additions & 16 deletions storage-app/src/shared/hasher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from asyncio.events import new_event_loop
from typing_extensions import Coroutine
from videohash import VideoHash
from videohash.utils import (
Expand All @@ -9,7 +8,7 @@
from PIL.ImageFile import ImageFile
from os.path import join, sep, exists
from pathlib import Path
from asyncio import get_event_loop, set_event_loop
from asyncio import run
from motor.motor_asyncio import AsyncIOMotorGridOut
from shared.settings import HASH_SIZE, TEMP_HASH_PATH, MEDIA_SIZE
from numpy import asarray, float32, ndarray
Expand All @@ -20,18 +19,6 @@
Image.ANTIALIAS = Image.Resampling.LANCZOS


def run_with_loop(f: Coroutine) -> Any:
current_loop = get_event_loop()
new_loop = new_event_loop()

try:
set_event_loop(new_loop)
return new_loop.run_until_complete(f)
finally:
new_loop.close()
set_event_loop(current_loop)


def to_embedding(
image: ImageFile,
embedding_type="dctlowfreq",
Expand Down Expand Up @@ -61,7 +48,7 @@ def __init__(self, file: AsyncIOMotorGridOut):
self.embedding = self._get_hash()

def _get_hash(self) -> ndarray:
get_event_loop().run_until_complete(self._get_buffer())
run(self._get_buffer())
image = Image.open(self._buffer)
return to_embedding(image)

Expand Down Expand Up @@ -118,7 +105,7 @@ def _copy_video_to_video_dir(self):
extension = self._file.metadata.get("file_extension")
self.video_path = join(self.video_dir, f"video.{extension}")

get_event_loop().run_until_complete(self._write_file())
run(self._write_file())

async def _write_file(self):
with open(self.video_path, "wb") as file:
Expand Down
7 changes: 2 additions & 5 deletions storage-app/src/shared/worker_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
APP_BACKEND_URL,
ASYNC_PRODUCER_MAX_CONCURRENT as MAX_CONCURENT
)
from asyncio import get_event_loop
from asyncio import run
from time import sleep as stall_for
from shared.app_services import Bucket
from shared.utils import emit_token
Expand Down Expand Up @@ -137,10 +137,7 @@ def __init__(self, bucket_name: str, uid: str):
except Exception: self.project_id = 0

def get_file(self):
file = get_event_loop().run_until_complete(
Bucket(self.bucket_name)
.get_object(self.file_id)
)
file = run(Bucket(self.bucket_name).get_object(self.file_id))
assert file, "No file found"

self.file = file.file
Expand Down

0 comments on commit 4842ea8

Please sign in to comment.