Skip to content

Commit

Permalink
Merge pull request #3 from litvan007/master
Browse files Browse the repository at this point in the history
added native algo for recognizer with whisper and applied black
  • Loading branch information
m-danya authored Apr 16, 2024
2 parents 2dd6a2b + f0532da commit a8b2a1d
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 122 deletions.
60 changes: 60 additions & 0 deletions accompanist/collection/recognizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import whisper
import json
import yaml
from functools import lru_cache

from typing import List


class Timestamper:
def __init__(self, data_path: str) -> None:
self.data_path = data_path

with open(self.data_path, "r") as config:
model_cfg = yaml.safe_load(config)
model_type = model_cfg["type"]
device = model_cfg["device"]
self.model = whisper.load_model(model_type).to(device)

def _levenshtein_distance(self, s1: str, s2: str) -> int:
@lru_cache(maxsize=None)
def helper(i, j):
if min(i, j) == 0:
return max(i, j)
elif s1[i - 1] == s2[j - 1]:
return helper(i - 1, j - 1)
else:
return 1 + min(helper(i, j - 1), helper(i - 1, j), helper(i - 1, j - 1))

return helper(len(s1), len(s2))

def get_line_timestamps(
self, path_mp3: str, path_text: str
) -> List[dict[str, str]]:
with open(path_text, "r") as f:
text_lines = f.readlines()

transcript = self.model.transcribe(word_timestamps=True, audio=path_mp3)

output_data = []
segments = transcript["segments"]
prev_n = 0
for line in text_lines:
last_word = line[-1]
curr_n = len(line)
curr_segments = segments[prev_n:curr_n]

min_lev_wordend = None
min_lev_value = 1000000
for segment in curr_segments:
curr_word = segment["words"][0]["word"]
curr_end = segments["words"][0]["end"]
curr_lev_value = self._levenshtein_distance(curr_word, last_word)
if min_lev_value > curr_lev_value:
min_lev_value = curr_lev_value
min_lev_wordend = curr_end

prev_n = curr_n
output_data.append({"line": line, "end_ts": min_lev_wordend})

return output_data
7 changes: 6 additions & 1 deletion accompanist/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
from typing import Literal, Optional

from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic_settings import BaseSettings, SettingsConfigDict, BaseModel:quit:q



class Settings(BaseSettings):
Expand Down Expand Up @@ -46,7 +47,11 @@ def TEST_DATABASE_URL(self):

model_config = SettingsConfigDict(env_file=".env", extra="ignore")

class Models_settins(BaseSettings)


settings = Settings()

settings.STORAGE_PATH.mkdir(exist_ok=True)


2 changes: 2 additions & 0 deletions accompanist/configs/whisper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: 'tiny'
device: 'cpu'
6 changes: 3 additions & 3 deletions alembic/versions/0017d75656ce_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
Create Date: 2024-03-17 20:16:28.957003
"""
from typing import Sequence, Union

from typing import Sequence, Union


# revision identifiers, used by Alembic.
revision: str = '0017d75656ce'
down_revision: Union[str, None] = '12a1d416b35e'
revision: str = "0017d75656ce"
down_revision: Union[str, None] = "12a1d416b35e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

Expand Down
9 changes: 5 additions & 4 deletions alembic/versions/12a1d416b35e_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
Create Date: 2024-03-14 23:42:08.977150
"""

from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = '12a1d416b35e'
down_revision: Union[str, None] = 'd6639af18a56'
revision: str = "12a1d416b35e"
down_revision: Union[str, None] = "d6639af18a56"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('track', sa.Column('lyrics', sa.String(), nullable=True))
op.add_column("track", sa.Column("lyrics", sa.String(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('track', 'lyrics')
op.drop_column("track", "lyrics")
# ### end Alembic commands ###
73 changes: 45 additions & 28 deletions alembic/versions/42060442a9f0_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2024-03-13 11:02:07.772867
"""

from typing import Sequence, Union

import sqlalchemy as sa
Expand All @@ -13,45 +14,61 @@
from alembic import op

# revision identifiers, used by Alembic.
revision: str = '42060442a9f0'
down_revision: Union[str, None] = 'b46fbe9d06e4'
revision: str = "42060442a9f0"
down_revision: Union[str, None] = "b46fbe9d06e4"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('track',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('artist_id', sa.Integer(), nullable=False),
sa.Column('album_id', sa.Integer(), nullable=False),
sa.Column('added_at', sa.DateTime(), nullable=False),
sa.Column('filename_vocals', sa.String(), nullable=False),
sa.Column('filename_instrumental', sa.String(), nullable=False),
sa.Column('number_in_album', sa.Integer(), nullable=False),
sa.Column('duration', sa.String(), nullable=False),
sa.ForeignKeyConstraint(['album_id'], ['album.id'], ),
sa.ForeignKeyConstraint(['artist_id'], ['artist.id'], ),
sa.PrimaryKeyConstraint('id')
op.create_table(
"track",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("artist_id", sa.Integer(), nullable=False),
sa.Column("album_id", sa.Integer(), nullable=False),
sa.Column("added_at", sa.DateTime(), nullable=False),
sa.Column("filename_vocals", sa.String(), nullable=False),
sa.Column("filename_instrumental", sa.String(), nullable=False),
sa.Column("number_in_album", sa.Integer(), nullable=False),
sa.Column("duration", sa.String(), nullable=False),
sa.ForeignKeyConstraint(
["album_id"],
["album.id"],
),
sa.ForeignKeyConstraint(
["artist_id"],
["artist.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.drop_table('song')
op.drop_table("song")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('song',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('artist_id', sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column('album_id', sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column('added_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('filename_original', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('filename_instrumental', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['album_id'], ['album.id'], name='song_album_id_fkey'),
sa.ForeignKeyConstraint(['artist_id'], ['artist.id'], name='song_artist_id_fkey'),
sa.PrimaryKeyConstraint('id', name='song_pkey')
op.create_table(
"song",
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column("name", sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column("artist_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column("album_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column(
"added_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=False
),
sa.Column(
"filename_original", sa.VARCHAR(), autoincrement=False, nullable=False
),
sa.Column(
"filename_instrumental", sa.VARCHAR(), autoincrement=False, nullable=False
),
sa.ForeignKeyConstraint(["album_id"], ["album.id"], name="song_album_id_fkey"),
sa.ForeignKeyConstraint(
["artist_id"], ["artist.id"], name="song_artist_id_fkey"
),
sa.PrimaryKeyConstraint("id", name="song_pkey"),
)
op.drop_table('track')
op.drop_table("track")
# ### end Alembic commands ###
15 changes: 9 additions & 6 deletions alembic/versions/4a303689767f_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
Create Date: 2024-03-19 00:14:13.688031
"""

from typing import Sequence, Union

from alembic import op


# revision identifiers, used by Alembic.
revision: str = '4a303689767f'
down_revision: Union[str, None] = '0017d75656ce'
revision: str = "4a303689767f"
down_revision: Union[str, None] = "0017d75656ce"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('track_album_id_fkey', 'track', type_='foreignkey')
op.create_foreign_key(None, 'track', 'album', ['album_id'], ['id'], ondelete='CASCADE')
op.drop_constraint("track_album_id_fkey", "track", type_="foreignkey")
op.create_foreign_key(
None, "track", "album", ["album_id"], ["id"], ondelete="CASCADE"
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'track', type_='foreignkey')
op.create_foreign_key('track_album_id_fkey', 'track', 'album', ['album_id'], ['id'])
op.drop_constraint(None, "track", type_="foreignkey")
op.create_foreign_key("track_album_id_fkey", "track", "album", ["album_id"], ["id"])
# ### end Alembic commands ###
9 changes: 5 additions & 4 deletions alembic/versions/9de71651198c_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
Create Date: 2024-04-02 20:52:13.322993
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '9de71651198c'
down_revision: Union[str, None] = '4a14745ef9f8'
revision: str = "9de71651198c"
down_revision: Union[str, None] = "4a14745ef9f8"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('track', sa.Column('genius_url', sa.String(), nullable=True))
op.add_column("track", sa.Column("genius_url", sa.String(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('track', 'genius_url')
op.drop_column("track", "genius_url")
# ### end Alembic commands ###
9 changes: 5 additions & 4 deletions alembic/versions/a7f61a8fe7e5_.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
Create Date: 2024-03-22 14:43:31.790523
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'a7f61a8fe7e5'
down_revision: Union[str, None] = '4a303689767f'
revision: str = "a7f61a8fe7e5"
down_revision: Union[str, None] = "4a303689767f"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('track', sa.Column('lyrics_karaoke', sa.JSON(), nullable=True))
op.add_column("track", sa.Column("lyrics_karaoke", sa.JSON(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('track', 'lyrics_karaoke')
op.drop_column("track", "lyrics_karaoke")
# ### end Alembic commands ###
38 changes: 22 additions & 16 deletions alembic/versions/b20480ab6439_create_album_and_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,47 @@
Create Date: 2024-03-10 16:18:43.317770
"""

from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = 'b20480ab6439'
revision: str = "b20480ab6439"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('artist',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('added_at', sa.Date(), nullable=False),
sa.PrimaryKeyConstraint('id')
op.create_table(
"artist",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("added_at", sa.Date(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_table('album',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(), nullable=False),
sa.Column('artist_id', sa.Integer(), nullable=False),
sa.Column('cover_path', sa.String(), nullable=False),
sa.Column('added_at', sa.Date(), nullable=False),
sa.ForeignKeyConstraint(['artist_id'], ['artist.id'], ),
sa.PrimaryKeyConstraint('id')
op.create_table(
"album",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("title", sa.String(), nullable=False),
sa.Column("artist_id", sa.Integer(), nullable=False),
sa.Column("cover_path", sa.String(), nullable=False),
sa.Column("added_at", sa.Date(), nullable=False),
sa.ForeignKeyConstraint(
["artist_id"],
["artist.id"],
),
sa.PrimaryKeyConstraint("id"),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('album')
op.drop_table('artist')
op.drop_table("album")
op.drop_table("artist")
# ### end Alembic commands ###
Loading

0 comments on commit a8b2a1d

Please sign in to comment.