From 5ca0805b10f7a589d2418322451b809e0ac17aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Fr=C4=85tczak?= Date: Thu, 15 Feb 2024 23:22:37 +0100 Subject: [PATCH] moving to not-inotify based file discovery --- dockerfile | 6 +++-- notescriber/dispatcher.py | 4 ++-- notescriber/picker.py | 49 +++++++++++++++++++++------------------ requirements.txt | 1 - 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/dockerfile b/dockerfile index 378f9fe..c52ce9c 100644 --- a/dockerfile +++ b/dockerfile @@ -1,4 +1,4 @@ -FROM python +FROM python:3.9.18-bookworm RUN mkdir /app COPY notescriber /app/notescriber @@ -9,4 +9,6 @@ COPY load_model.py /app/load_model.py WORKDIR /app RUN pip install -r requirements.txt -RUN /app/load_model.py \ No newline at end of file +RUN /app/load_model.py + +RUN apt update && apt install -y ffmpeg diff --git a/notescriber/dispatcher.py b/notescriber/dispatcher.py index dbc0496..cb4a879 100644 --- a/notescriber/dispatcher.py +++ b/notescriber/dispatcher.py @@ -20,8 +20,8 @@ class Dispatcher: # TODO: we need to have a way to wipe old threads, otherwise we may have a memory leak def _execute_thread(self, audio_file): - filename = os.path.basename(audio_file.pathname) - note = self.audioProcessor.execute(audio_file.pathname) + filename = os.path.basename(audio_file) + note = self.audioProcessor.execute(audio_file) self.noteGenerator.generate_note(filename, note) def halt(self): diff --git a/notescriber/picker.py b/notescriber/picker.py index 06efab9..eebc1b7 100644 --- a/notescriber/picker.py +++ b/notescriber/picker.py @@ -1,34 +1,39 @@ -import pyinotify +import time import os -class EventHandler(pyinotify.ProcessEvent): - def process_IN_CREATE(self, event): - print(f'New file: {event.pathname}') - return event.pathname - - def apply_filter(self, audio_file): - # TODO: return True if audio file is valid, based on name, extension, and other criteria - if os.path.isdir(audio_file): - return False - return True - class Picker: directory = None dispatcher = None - notifier = None + observer = None + last_file_time = None def __init__(self, directory, dispatcher): self.directory = directory self.dispatcher = dispatcher + def identify_time_window(self, file_time): + if self.last_file_time is None: + self.last_file_time = time.time() + if file_time > self.last_file_time: + return True + return False + + def list_files(self): + files = [] + for filename in os.listdir(self.directory): + filepath = os.path.join(self.directory, filename) + if os.path.isfile(filepath): + files.append(filepath) + return files + + def observe(self): - wm = pyinotify.WatchManager() - self.notifier = pyinotify.Notifier(wm, EventHandler()) - wm.add_watch(self.directory, pyinotify.IN_CREATE, self.dispatcher.process_audio_file) - - print(f'Watching {self.directory} for changes...') - self.notifier.loop() - - def halt(self): - self.notifier.stop() \ No newline at end of file + while True: + files = self.list_files() + for file in files: + file_time = os.path.getmtime(file) + if self.identify_time_window(file_time): + self.dispatcher.process_audio_file(file) + self.last_file_time = time.time() + time.sleep(10) diff --git a/requirements.txt b/requirements.txt index 7ebb91d..efaff88 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ openai-whisper==20231117 -pyinotify==0.9.6 nextcloud_notes_api==1.0.0 PyYaml==6.0.1 pyasyncore==1.0.3 \ No newline at end of file