-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from iKonoTelecomunicaciones/83-midleware-to-p…
…roccess-audio Middleware to proccess audio
- Loading branch information
Showing
12 changed files
with
214 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .asr import ASRMiddleware | ||
from .http import HTTPMiddleware | ||
from .irm import IRMMiddleware | ||
from .llm import LLMMiddleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
from typing import Dict, Tuple | ||
|
||
from aiohttp import ClientTimeout, ContentTypeError, FormData | ||
from mautrix.util.config import RecursiveDict | ||
from ruamel.yaml.comments import CommentedMap | ||
|
||
from ..nodes import Base | ||
from ..repository import ASRMiddleware as ASRMiddlewareModel | ||
from ..room import Room | ||
|
||
|
||
class ASRMiddleware(Base): | ||
def __init__( | ||
self, | ||
asr_middleware_content: ASRMiddlewareModel, | ||
room: Room, | ||
default_variables: Dict, | ||
) -> None: | ||
Base.__init__(self, room=room, default_variables=default_variables) | ||
self.log = self.log.getChild(asr_middleware_content.id) | ||
self.content: ASRMiddlewareModel = asr_middleware_content | ||
|
||
@property | ||
def url(self) -> str: | ||
return self.render_data(self.content.url) | ||
|
||
@property | ||
def headers(self) -> Dict: | ||
return self.render_data(self.content.headers) | ||
|
||
@property | ||
def middleware_variables(self) -> Dict: | ||
return self.render_data(self.content.variables) | ||
|
||
@property | ||
def method(self) -> Dict: | ||
return self.render_data(self.content.method) | ||
|
||
@property | ||
def cookies(self) -> Dict: | ||
return self.render_data(self.content.cookies) | ||
|
||
@property | ||
def provider(self) -> str: | ||
return self.render_data(self.content.provider) | ||
|
||
async def run( | ||
self, extended_data: Dict, audio_url: str, audio_name: str = None | ||
) -> Tuple[int, str]: | ||
audio = await self.room.matrix_client.download_media(url=audio_url) | ||
result = await self.http_request(audio=audio, audio_name=audio_name) | ||
|
||
return result | ||
|
||
async def http_request(self, audio, audio_name) -> Tuple[int, str]: | ||
"""Recognize the text and return the status code and the text.""" | ||
request_body = {} | ||
form_data = FormData() | ||
|
||
if self.headers: | ||
request_body["headers"] = self.headers | ||
|
||
if audio: | ||
form_data.add_field("audio", audio, filename=audio_name, content_type="audio/ogg") | ||
|
||
form_data.add_field("provider", self.provider) | ||
else: | ||
self.log.error("Error getting the audio") | ||
return | ||
|
||
try: | ||
timeout = ClientTimeout(total=self.config["menuflow.timeouts.middlewares"]) | ||
response = await self.session.request( | ||
self.method, | ||
self.url, | ||
timeout=timeout, | ||
data=form_data, | ||
**request_body, | ||
) | ||
except Exception as e: | ||
self.log.exception(f"Audio to text conversion error: {e}") | ||
return | ||
|
||
variables = {} | ||
|
||
if self.cookies: | ||
for cookie in self.cookies: | ||
variables[cookie] = response.cookies.output(cookie) | ||
try: | ||
response_data = await response.json() | ||
|
||
except ContentTypeError: | ||
response_data = {} | ||
|
||
if not response_data: | ||
return response.status, None | ||
|
||
if isinstance(response_data, dict): | ||
# Tulir and its magic since time immemorial | ||
serialized_data = RecursiveDict(CommentedMap(**response_data)) | ||
if self.middleware_variables: | ||
for variable in self.middleware_variables: | ||
try: | ||
variables[variable] = self.render_data( | ||
serialized_data[self.middleware_variables[variable]] | ||
) | ||
except KeyError: | ||
pass | ||
|
||
elif isinstance(response_data, str): | ||
if self.middleware_variables: | ||
for variable in self.middleware_variables: | ||
try: | ||
variables[variable] = self.render_data(response_data) | ||
except KeyError: | ||
pass | ||
|
||
break | ||
|
||
if variables: | ||
await self.room.set_variables(variables=variables) | ||
|
||
return response.status, await response.text() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Dict | ||
|
||
from attr import dataclass, ib | ||
|
||
from ..flow_object import FlowObject | ||
|
||
|
||
@dataclass | ||
class ASRMiddleware(FlowObject): | ||
"""ASRMiddleware | ||
Middleware node recognize the text from a sound file. | ||
content: | ||
``` | ||
- id: m1 | ||
type: asr | ||
method: GET | ||
url: "http://localhost:5000/asr" | ||
provider: "azure" | ||
cookies: | ||
cookie1: "value1" | ||
header: | ||
Client-token: "client-token" | ||
variables: | ||
variable1: "value1" | ||
""" | ||
|
||
id: str = ib(default=None) | ||
type: str = ib(default=None) | ||
method: str = ib(default=None) | ||
url: str = ib(default=None) | ||
provider: str = ib(default=None) | ||
cookies: Dict[str, Any] = ib(factory=dict) | ||
headers: Dict[str, Any] = ib(default=None) | ||
variables: Dict[str, Any] = ib(factory=dict) | ||
|
||
@classmethod | ||
def from_dict(cls, data: Dict) -> ASRMiddleware: | ||
return cls( | ||
id=data.get("id"), | ||
type=data.get("type"), | ||
method=data.get("method"), | ||
url=data.get("url"), | ||
provider=data.get("provider"), | ||
variables=data.get("variables"), | ||
cookies=data.get("cookies"), | ||
headers=data.get("headers"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ class Middlewares(SerializableEnum): | |
BASE = "base" | ||
IRM = "irm" | ||
LLM = "llm" | ||
ASR = "asr" |