Commit d6681aae authored by Francois Ledoyen's avatar Francois Ledoyen
Browse files

Add langdetectors and tts engines

parent 793840ce
Loading
Loading
Loading
Loading
+0 −17
Original line number Diff line number Diff line
import io
from typing import List, Dict
import langdetect
import googletrans


class LanguageDetector:
    GOOGLE_TRANSLATOR = googletrans.Translator(service_urls=["translate.google.com"])

    @classmethod
    def langdetect(cls, text) -> List[Dict[str, str]]:
        langdetect.DetectorFactory.seed = 0
        yield text, langdetect.detect(text)

    @classmethod
    def google_transalte(cls, text) -> List[Dict[str, str]]:
        yield text, cls.GOOGLE_TRANSLATOR.detect(text).lang
+0 −26
Original line number Diff line number Diff line
from typing import Callable, Dict

from gtts import gTTS
import io

from pipeline.blocks.vocalization._abstract import AbstractVocalizationBlock
from pipeline.blocks.vocalization._utils import LanguageDetector
from pipeline.models.responses import Zone, Keywords, Keyword, Segmentation


class GoogleTTS(AbstractVocalizationBlock):
    LANG_DETECTORS: Dict[str, Callable] = {
        "langdetect": LanguageDetector.langdetect,
        "google_translate": LanguageDetector.google_transalte
    }

    def __call__(self, segmentation: Segmentation, lang_getector: str = "google_translate") -> io.BytesIO:
        fp = io.BytesIO()

        # for k in keywords:
        #     for text, lang in self.LANG_DETECTORS['google_translate'](k.text):
        #         tts = gTTS(text=text, lang=lang, tld="com", slow=False)
        #         tts.write_to_fp(fp)
        # fp.seek(0)

        return fp
+0 −0

Empty file added.

+39 −0
Original line number Diff line number Diff line
import abc
import enum
import io
import zipfile
from typing import List, Dict, Callable
import langdetect
import googletrans
from pydub import AudioSegment
import inspect


class AbstractLanguageDetector(abc.ABC):

    @abc.abstractmethod
    def __call__(self, text) -> List[Dict[str, str]]:
        ...


class GoogleTranslate(AbstractLanguageDetector):
    __GOOGLE_TRANSLATOR = googletrans.Translator(service_urls=["translate.google.com"])

    def __call__(self, text) -> List[Dict[str, str]]:
        yield text, self.__GOOGLE_TRANSLATOR.detect(text).lang


class LangDetect(AbstractLanguageDetector):
    def __call__(self, text) -> List[Dict[str, str]]:
        langdetect.DetectorFactory.seed = 0
        yield text, langdetect.detect(text)


class LanguageDetectors(enum.Enum):
    google_translate = GoogleTranslate()
    langdetect = LangDetect()

    @classmethod
    @property
    def default(cls):
        return cls.google_translate
+20 −0
Original line number Diff line number Diff line
import io
import zipfile
from pydub import AudioSegment


def convert_mp3_to_wav(file):
    # file.seek(0)
    sound = AudioSegment.from_mp3(file)
    res = io.BytesIO()
    sound.export(res, format="wav")
    res.seek(0)
    return res


def zip_files(*files):
    zip_io = io.BytesIO()
    with zipfile.ZipFile(zip_io, mode="w", compression=zipfile.ZIP_DEFLATED) as zip_tmp:
        for file in files:
            zip_tmp.write(file)
    return zip_io
Loading