moving to not-inotify based file discovery

This commit is contained in:
Tomasz Frątczak 2024-02-15 23:22:37 +01:00
parent 96e0615267
commit 5ca0805b10
4 changed files with 33 additions and 27 deletions

View File

@ -1,4 +1,4 @@
FROM python FROM python:3.9.18-bookworm
RUN mkdir /app RUN mkdir /app
COPY notescriber /app/notescriber COPY notescriber /app/notescriber
@ -9,4 +9,6 @@ COPY load_model.py /app/load_model.py
WORKDIR /app WORKDIR /app
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
RUN /app/load_model.py RUN /app/load_model.py
RUN apt update && apt install -y ffmpeg

View File

@ -20,8 +20,8 @@ class Dispatcher:
# TODO: we need to have a way to wipe old threads, otherwise we may have a memory leak # TODO: we need to have a way to wipe old threads, otherwise we may have a memory leak
def _execute_thread(self, audio_file): def _execute_thread(self, audio_file):
filename = os.path.basename(audio_file.pathname) filename = os.path.basename(audio_file)
note = self.audioProcessor.execute(audio_file.pathname) note = self.audioProcessor.execute(audio_file)
self.noteGenerator.generate_note(filename, note) self.noteGenerator.generate_note(filename, note)
def halt(self): def halt(self):

View File

@ -1,34 +1,39 @@
import pyinotify import time
import os 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: class Picker:
directory = None directory = None
dispatcher = None dispatcher = None
notifier = None observer = None
last_file_time = None
def __init__(self, directory, dispatcher): def __init__(self, directory, dispatcher):
self.directory = directory self.directory = directory
self.dispatcher = dispatcher 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): def observe(self):
wm = pyinotify.WatchManager() while True:
self.notifier = pyinotify.Notifier(wm, EventHandler()) files = self.list_files()
wm.add_watch(self.directory, pyinotify.IN_CREATE, self.dispatcher.process_audio_file) for file in files:
file_time = os.path.getmtime(file)
print(f'Watching {self.directory} for changes...') if self.identify_time_window(file_time):
self.notifier.loop() self.dispatcher.process_audio_file(file)
self.last_file_time = time.time()
def halt(self): time.sleep(10)
self.notifier.stop()

View File

@ -1,5 +1,4 @@
openai-whisper==20231117 openai-whisper==20231117
pyinotify==0.9.6
nextcloud_notes_api==1.0.0 nextcloud_notes_api==1.0.0
PyYaml==6.0.1 PyYaml==6.0.1
pyasyncore==1.0.3 pyasyncore==1.0.3