Skip to content
Snippets Groups Projects
Unverified Commit 9cda9759 authored by Marcin Wątroba's avatar Marcin Wątroba
Browse files

Add Base service improvement

parent 30a40ace
Branches
1 merge request!10Feature/add auth asr service
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, Dict from typing import Any, Dict, Optional
import requests import requests
...@@ -16,14 +16,18 @@ class AsrProcessor(ABC): ...@@ -16,14 +16,18 @@ class AsrProcessor(ABC):
class AsrWebClient(AsrProcessor): class AsrWebClient(AsrProcessor):
_url: str _url: str
_auth_token: Optional[str]
def __init__(self, url: str): def __init__(self, url: str, auth_token: Optional[str]):
super(AsrWebClient, self).__init__() super(AsrWebClient, self).__init__()
self._url = url self._url = url
self._auth_token = auth_token
def call_recognise(self, file_path: str) -> Dict[str, Any]: def call_recognise(self, file_path: str) -> Dict[str, Any]:
files = {"file": open(file_path, "rb")} files = {"file": open(file_path, "rb")}
res = requests.post(self._url, files=files) 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)
json_response = res.json() json_response = res.json()
print(json_response) print(json_response)
return json_response return json_response
...@@ -48,10 +48,14 @@ class AsrProcessor(ABC): ...@@ -48,10 +48,14 @@ class AsrProcessor(ABC):
else: else:
return None return None
def health_check(self) -> Response:
return jsonify({'status': 'running'})
def start_processor(self): def start_processor(self):
app = Flask(__name__) app = Flask(__name__)
auth = HTTPTokenAuth(scheme="Bearer") auth = HTTPTokenAuth(scheme="Bearer")
auth.verify_token(self.is_token_correct) auth.verify_token(self.is_token_correct)
Path(_TEMP_DIRECTORY).mkdir(parents=True, exist_ok=True) Path(_TEMP_DIRECTORY).mkdir(parents=True, exist_ok=True)
app.route("/process_asr", methods=["POST"])(auth.login_required(self.process_request)) app.route("/process_asr", methods=["POST"])(auth.login_required(self.process_request))
app.route("/health_check", methods=["GET"])(self.health_check)
app.run(debug=True, host="0.0.0.0") app.run(debug=True, host="0.0.0.0")
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment