From abf97340eab7250fb1e55ee301d1dc28d1ef366f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Thu, 12 Sep 2024 20:45:16 +0700 Subject: [PATCH] Reducing updates on processing memories --- backend/database/processing_memories.py | 7 +++++++ backend/routers/transcribe.py | 11 ++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/database/processing_memories.py b/backend/database/processing_memories.py index 3b2b4fcd4..7d839e2f1 100644 --- a/backend/database/processing_memories.py +++ b/backend/database/processing_memories.py @@ -45,6 +45,13 @@ def update_basic(uid: str, id: str, geolocation: dict, emotional_feedback: bool) 'geolocation':geolocation, }) +def update_audio_url(uid: str, id: str, audio_url: str): + user_ref = db.collection('users').document(uid) + memory_ref = user_ref.collection('processing_memories').document(id) + memory_ref.update({ + 'audio_url': audio_url, + }) + def get_last(uid: str): processing_memories_ref = ( db.collection('users').document(uid).collection('processing_memories') diff --git a/backend/routers/transcribe.py b/backend/routers/transcribe.py index 5debcd0af..c4e6459c9 100644 --- a/backend/routers/transcribe.py +++ b/backend/routers/transcribe.py @@ -88,7 +88,6 @@ def _combine_segments(segments: [], new_segments: [], delta_seconds: int = 0): else: joined_similar_segments.append(new_segment) - if (segments and (segments[-1].speaker == joined_similar_segments[0].speaker or (segments[-1].is_user and joined_similar_segments[0].is_user)) and @@ -160,10 +159,11 @@ def stream_transcript(segments, stream_id): memory_transcript_segements = _combine_segments(memory_transcript_segements, list(map(lambda m: TranscriptSegment(**m), segments)), delta_seconds) # Sync processing transcript, periodly - if processing_memory and len(memory_transcript_segements) % 2 == 0: + if processing_memory and int(time.time()) % 3 == 0: processing_memory_synced = len(memory_transcript_segements) processing_memory.transcript_segments = memory_transcript_segements - processing_memories_db.update_processing_memory(uid, processing_memory.id, processing_memory.dict()) + processing_memories_db.update_processing_memory_segments(uid, processing_memory.id, + list(map(lambda m: m.dict(), processing_memory.transcript_segments))) def stream_audio(audio_buffer): if not new_memory_watch: @@ -384,7 +384,7 @@ async def _post_process_memory(memory: Memory): # Store postprocessing audio file signed_url = upload_postprocessing_audio(file_path) processing_memory.audio_url = signed_url - processing_memories_db.update_processing_memory(uid, processing_memory.id, processing_memory.dict()) + processing_memories_db.update_audio_url(uid, processing_memory.id, processing_memory.audio_url) os.remove(file_path) @@ -412,7 +412,8 @@ async def _create_memory(): processing_memory_synced = len(memory_transcript_segements) processing_memory.transcript_segments = memory_transcript_segements[:processing_memory_synced] - processing_memories_db.update_processing_memory(uid, processing_memory.id, processing_memory.dict()) + processing_memories_db.update_processing_memory_segments(uid, processing_memory.id, + list(map(lambda m: m.dict(), processing_memory.transcript_segments))) # Message: creating ok = await _send_message_event(MessageEvent(event_type="new_memory_creating"))