Skip to content

Commit

Permalink
Move to Poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
AliOsm committed Jun 26, 2024
1 parent b4a61ea commit 04e4162
Show file tree
Hide file tree
Showing 25 changed files with 3,309 additions and 52 deletions.
31 changes: 15 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
---
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-json
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
- id: check-symlinks
Expand All @@ -22,21 +18,24 @@ repos:
- id: destroyed-symlinks
- id: detect-private-key
- id: end-of-file-fixer
types: [ python ]
types: [python]
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: name-tests-test
args: [ --pytest-test-first ]
args: [--pytest-test-first]
- id: trailing-whitespace
types: [ python ]

- repo: https://github.com/psf/black
rev: 23.7.0
types: [python]
- repo: https://github.com/hhatto/autopep8
rev: v2.2.0
hooks:
- id: black
additional_dependencies: [click==8.0.4]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- id: autopep8
args: ['-i', '--max-line-length=120', '--indent-size=2', '--ignore=E121']
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.0
hooks:
- id: mypy
additional_dependencies: ['types-requests']
1,971 changes: 1,971 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

66 changes: 30 additions & 36 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
[tool.poetry]
name = "tafrigh"
version = "1.1.4"
description = "تفريغ النصوص وإنشاء ملفات SRT و VTT باستخدام نماذج Whisper وتقنية wit.ai."
authors = ["EasyBooks <[email protected]>"]
license = "MIT"
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.9"
packages = [{include = "src"}]
keywords = ["tafrigh", "speech-to-text", "wit.ai", "whisper"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand All @@ -17,45 +15,41 @@ classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
]
authors = [{ name = "الكتب المٌيسّرة", email = "[email protected]" }]
keywords = ["tafrigh", "speech-to-text", "wit.ai", "whisper"]
dependencies = ["tqdm>=4.66.4", "yt-dlp>=2024.4.9"]
homepage = "https://tafrigh.ieasybooks.com"
repository = "https://github.com/ieasybooks/tafrigh"

[project.optional-dependencies]
wit = [
"auditok>=0.2.0",
"numpy>=1.26.4",
"pydub>=0.25.1",
"requests>=2.32.0",
]
whisper = [
"faster-whisper>=1.0.2",
"openai-whisper>=20231117",
"stable-ts>=2.17.2",
]
[tool.poetry.dependencies]
python = ">=3.10,<3.12"
tqdm = ">=4.66.4"
yt-dlp = ">=2024.4.9"
auditok = {version = ">=0.2.0", extras = ["wit"]}
pydub = {version = ">=0.25.1", extras = ["wit"]}
requests = {version = ">=2.32.0", extras = ["wit"]}
faster-whisper = {version = ">=1.0.2", extras = ["whisper"]}
openai-whisper = {git = "https://github.com/openai/whisper.git", extras = ["whisper"]}
stable-ts = {version = ">=2.17.2", extras = ["whisper"]}

[project.urls]
homepage = "https://github.com/ieasybooks/tafrigh"
repository = "https://github.com/ieasybooks/tafrigh"
[tool.poetry.scripts]
tafrigh = "src.cli:main"

[project.scripts]
tafrigh = "tafrigh.cli:main"
[tool.poetry.group.dev.dependencies]
pre-commit = "^3.7.1"

[tool.black]
line-length = 120
skip-string-normalization = true
[tool.autopep8]
max-line-length = 120
indent-size = 2
ignore = ["E121"]

[tool.isort]
profile = "black"
src_paths = "tafrigh"
line_length = 120
src_paths = ["src"]
lines_between_types = 1
lines_after_imports = 2
case_sensitive = true
include_trailing_comma = true

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
29 changes: 29 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
__all__ = [
"farrigh",
"Config",
"Downloader",
"TranscriptType",
"Writer",
"WhisperRecognizer",
"AudioSplitter",
"WitRecognizer",
]


from .cli import farrigh
from .config import Config
from .downloader import Downloader
from .types.transcript_type import TranscriptType
from .writer import Writer


try:
from .recognizers.whisper_recognizer import WhisperRecognizer
except ModuleNotFoundError:
pass

try:
from .audio_splitter import AudioSplitter
from .recognizers.wit_recognizer import WitRecognizer
except ModuleNotFoundError:
pass
77 changes: 77 additions & 0 deletions src/audio_splitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os

from auditok import AudioRegion
from auditok.core import split
from pydub import AudioSegment
from pydub.generators import WhiteNoise


class AudioSplitter:
def split(
self,
file_path: str,
output_dir: str,
min_dur: float = 0.5,
max_dur: float = 15,
max_silence: float = 0.5,
energy_threshold: float = 50,
expand_segments_with_noise: bool = False,
noise_seconds: int = 1,
noise_amplitude: int = 0,
) -> list[tuple[str, float, float]]:
segments = split(
file_path,
min_dur=min_dur,
max_dur=max_dur,
max_silence=max_silence,
energy_threshold=energy_threshold,
)

if expand_segments_with_noise:
segments = [
(
self._expand_segment_with_noise(segment, noise_seconds, noise_amplitude),
segment.meta.start,
segment.meta.end,
) for segment in segments
]

return self._save_segments(output_dir, segments)

def _expand_segment_with_noise(
self,
segment: AudioRegion,
noise_seconds: int,
noise_amplitude: int,
) -> AudioSegment:

audio_segment = AudioSegment(
segment._data,
frame_rate=segment.sampling_rate,
sample_width=segment.sample_width,
channels=segment.channels,
)

pre_noise = WhiteNoise().to_audio_segment(duration=noise_seconds * 1000, volume=noise_amplitude)
post_noise = WhiteNoise().to_audio_segment(duration=noise_seconds * 1000, volume=noise_amplitude)

return pre_noise + audio_segment + post_noise

def _save_segments(
self,
output_dir: str,
segments: list[AudioSegment | tuple[AudioSegment, float, float]],
) -> list[tuple[str, float, float]]:
segment_paths = []

for i, segment in enumerate(segments):
output_file = os.path.join(output_dir, f'segment_{i + 1}.mp3')

if isinstance(segment, tuple):
segment[0].export(output_file, format='mp3')
segment_paths.append((output_file, segment[1], segment[2]))
else:
segment.save(output_file)
segment_paths.append((output_file, segment.meta.start, segment.meta.end))

return segment_paths
Loading

0 comments on commit 04e4162

Please sign in to comment.