Skip to content

Commit

Permalink
Automatically split words into sentences
Browse files Browse the repository at this point in the history
  • Loading branch information
chromakode committed Oct 18, 2023
1 parent a24f42c commit 4dc3421
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
23 changes: 21 additions & 2 deletions audio-processor/coalesce_audio_processor/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import functools
import tempfile
import pysbd
from faster_whisper import WhisperModel
from math import ceil
from pydub import AudioSegment
Expand Down Expand Up @@ -75,11 +76,25 @@ def word_to_dict(word):
# via https://github.com/guillaumekln/faster-whisper/issues/94#issuecomment-1489916191
def segment_to_dict(segment):
segment = segment._asdict()
if segment["words"] is not None:
segment["words"] = [word_to_dict(word) for word in segment["words"]]
segment["words"] = [word_to_dict(word) for word in segment["words"]]
return segment


class SentenceSplitter:
def __init__(self, language):
self.text = ""
self.segmenter = pysbd.Segmenter(language=language)

def update_sentence_ends(self, segment):
for word in segment["words"]:
self.text += word["text"]

sentences = self.segmenter.segment(self.text)
if len(sentences) > 1:
word["isSentenceStart"] = True
self.text = sentences[-1]


def transcribe_audio(
input_path: str,
output_sink,
Expand All @@ -101,9 +116,13 @@ def transcribe_audio(
vad_filter=True,
)

splitter = SentenceSplitter(language=info.language)

segments = []
for segment in segment_gen:
segment_dict = segment_to_dict(segment)
splitter.update_sentence_ends(segment_dict)

segments.append(segment_dict)
if segment_callback:
segment_callback(segment_dict)
Expand Down
14 changes: 12 additions & 2 deletions audio-processor/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions audio-processor/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ aiohttp = "^3.8.4"
nanoid = "^2.0.0"
websockets = "^11.0.3"
aiohttp-retry = "^2.8.3"
pysbd = "0.3.4"

[tool.poetry.group.runpod]
optional = true
Expand Down
5 changes: 4 additions & 1 deletion project-server/collab/editorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ export function addWordsToEditor({
} else {
const docNodeLocation = docNode.getSoundLocation()
const insertBefore = docNodeLocation.start > word.start
if (docNodeLocation.source === word.source) {
if (
!word.isSentenceStart &&
docNodeLocation.source === word.source
) {
if (word.text.startsWith(' ')) {
const spaceNode = $createTextNode(' ')
newNodes.unshift(spaceNode)
Expand Down
1 change: 1 addition & 0 deletions shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const WordModel = z.object({
start: z.number(),
end: z.number(),
probability: z.number(),
isSentenceStart: z.boolean().optional(),
})
export type Word = z.infer<typeof WordModel>

Expand Down

0 comments on commit 4dc3421

Please sign in to comment.