Skip to content
Snippets Groups Projects
asr_processor.py 1.01 KiB
Newer Older
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional

import requests


class AsrProcessor(ABC):
    @abstractmethod
Marcin Wątroba's avatar
Marcin Wątroba committed
    def call_recognise(self, file_path: str) -> Dict[str, Any]:
        """
        Currently most important is field `transcript` with list of transcript
        words.
        """
        pass


class AsrWebClient(AsrProcessor):
    _url: str
    _auth_token: Optional[str]
    def __init__(self, url: str, auth_token: Optional[str]):
        super(AsrWebClient, self).__init__()
        self._url = url
        self._auth_token = auth_token
Marcin Wątroba's avatar
Marcin Wątroba committed
    def call_recognise(self, file_path: str) -> Dict[str, Any]:
Marcin Wątroba's avatar
Marcin Wątroba committed
        files = {"file": open(file_path, "rb")}
        headers = (
            dict({"Authorization": f"Bearer {self._auth_token}"})
            if self._auth_token is not None
            else dict()
        )
        res = requests.post(self._url, files=files, headers=headers, timeout=600)
        json_response = res.json()
        print(json_response)
        return json_response