Skip to content

new: MachineTranslation Provider API #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

All notable changes to this project will be documented in this file.

## [0.8.1 - 2024-0x-xx]
## [0.8.1 - 2024-01-xx]

### Added

- NextcloudApp: `AppAPIAuthMiddleware` for easy cover all endpoints. #205
- class `Share`: added missing `file_source_id`, `can_edit`, `can_delete` properties. #206
- NextcloudApp: `AppAPIAuthMiddleware` for easy cover all endpoints. #205
- NextcloudApp: API for registering `MachineTranslation` providers(*avalaible from Nextcloud 29*). #207

### Changed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Python library that provides a robust and well-documented API that allows develo
| AI Providers API** | N/A | N/A | N/A | ✅ |

&ast;_available only for **NextcloudApp**_<br>
&ast;&ast;_available only for **NextcloudApp**: SpeechToText, TextProcessing_<br>
&ast;&ast;_available only for **NextcloudApp**: SpeechToText, TextProcessing, Translation_<br>
&ast;&ast;&ast;_Activity, Notes_

### Differences between the Nextcloud and NextcloudApp classes
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/ExApp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ UI methods should be accessed with the help of :class:`~nc_py_api.nextcloud.Next

.. autoclass:: nc_py_api.ex_app.providers.text_processing._TextProcessingProviderAPI
:members:

.. autoclass:: nc_py_api.ex_app.providers.translations.TranslationsProvider
:members:

.. autoclass:: nc_py_api.ex_app.providers.translations._TranslationsProviderAPI
:members:
7 changes: 7 additions & 0 deletions nc_py_api/ex_app/providers/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..._session import AsyncNcSessionApp, NcSessionApp
from .speech_to_text import _AsyncSpeechToTextProviderAPI, _SpeechToTextProviderAPI
from .text_processing import _AsyncTextProcessingProviderAPI, _TextProcessingProviderAPI
from .translations import _AsyncTranslationsProviderAPI, _TranslationsProviderAPI


class ProvidersApi:
Expand All @@ -12,10 +13,13 @@ class ProvidersApi:
"""SpeechToText Provider API."""
text_processing: _TextProcessingProviderAPI
"""TextProcessing Provider API."""
translations: _TranslationsProviderAPI
"""Translations Provider API."""

def __init__(self, session: NcSessionApp):
self.speech_to_text = _SpeechToTextProviderAPI(session)
self.text_processing = _TextProcessingProviderAPI(session)
self.translations = _TranslationsProviderAPI(session)


class AsyncProvidersApi:
Expand All @@ -25,7 +29,10 @@ class AsyncProvidersApi:
"""SpeechToText Provider API."""
text_processing: _AsyncTextProcessingProviderAPI
"""TextProcessing Provider API."""
translations: _AsyncTranslationsProviderAPI
"""Translations Provider API."""

def __init__(self, session: AsyncNcSessionApp):
self.speech_to_text = _AsyncSpeechToTextProviderAPI(session)
self.text_processing = _AsyncTextProcessingProviderAPI(session)
self.translations = _AsyncTranslationsProviderAPI(session)
26 changes: 12 additions & 14 deletions nc_py_api/ex_app/providers/speech_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from ..._misc import require_capabilities
from ..._session import AsyncNcSessionApp, NcSessionApp

_EP_SUFFIX: str = "ai_provider/speech_to_text"


@dataclasses.dataclass
class SpeechToTextProvider:
Expand Down Expand Up @@ -35,9 +37,7 @@ def __repr__(self):


class _SpeechToTextProviderAPI:
"""API for registering Speech2Text providers."""

_ep_suffix: str = "ai_provider/speech_to_text"
"""API for Speech2Text providers."""

def __init__(self, session: NcSessionApp):
self._session = session
Expand All @@ -50,13 +50,13 @@ def register(self, name: str, display_name: str, callback_url: str) -> None:
"displayName": display_name,
"actionHandler": callback_url,
}
self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)
self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)

def unregister(self, name: str, not_fail=True) -> None:
"""Removes SpeechToText provider."""
require_capabilities("app_api", self._session.capabilities)
try:
self._session.ocs("DELETE", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
except NextcloudExceptionNotFound as e:
if not not_fail:
raise e from None
Expand All @@ -66,7 +66,7 @@ def get_entry(self, name: str) -> SpeechToTextProvider | None:
require_capabilities("app_api", self._session.capabilities)
try:
return SpeechToTextProvider(
self._session.ocs("GET", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
self._session.ocs("GET", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
)
except NextcloudExceptionNotFound:
return None
Expand All @@ -77,15 +77,13 @@ def report_result(self, task_id: int, result: str = "", error: str = "") -> None
with contextlib.suppress(NextcloudException):
self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
f"{self._session.ae_url}/{_EP_SUFFIX}",
json={"taskId": task_id, "result": result, "error": error},
)


class _AsyncSpeechToTextProviderAPI:
"""API for registering Speech2Text providers."""

_ep_suffix: str = "ai_provider/speech_to_text"
"""API for Speech2Text providers."""

def __init__(self, session: AsyncNcSessionApp):
self._session = session
Expand All @@ -98,13 +96,13 @@ async def register(self, name: str, display_name: str, callback_url: str) -> Non
"displayName": display_name,
"actionHandler": callback_url,
}
await self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)
await self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)

async def unregister(self, name: str, not_fail=True) -> None:
"""Removes SpeechToText provider."""
require_capabilities("app_api", await self._session.capabilities)
try:
await self._session.ocs("DELETE", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
await self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
except NextcloudExceptionNotFound as e:
if not not_fail:
raise e from None
Expand All @@ -114,7 +112,7 @@ async def get_entry(self, name: str) -> SpeechToTextProvider | None:
require_capabilities("app_api", await self._session.capabilities)
try:
return SpeechToTextProvider(
await self._session.ocs("GET", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
await self._session.ocs("GET", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
)
except NextcloudExceptionNotFound:
return None
Expand All @@ -125,6 +123,6 @@ async def report_result(self, task_id: int, result: str = "", error: str = "") -
with contextlib.suppress(NextcloudException):
await self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
f"{self._session.ae_url}/{_EP_SUFFIX}",
json={"taskId": task_id, "result": result, "error": error},
)
26 changes: 12 additions & 14 deletions nc_py_api/ex_app/providers/text_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from ..._misc import require_capabilities
from ..._session import AsyncNcSessionApp, NcSessionApp

_EP_SUFFIX: str = "ai_provider/text_processing"


@dataclasses.dataclass
class TextProcessingProvider:
Expand Down Expand Up @@ -40,9 +42,7 @@ def __repr__(self):


class _TextProcessingProviderAPI:
"""API for registering TextProcessing providers."""

_ep_suffix: str = "ai_provider/text_processing"
"""API for TextProcessing providers."""

def __init__(self, session: NcSessionApp):
self._session = session
Expand All @@ -56,13 +56,13 @@ def register(self, name: str, display_name: str, callback_url: str, task_type: s
"actionHandler": callback_url,
"taskType": task_type,
}
self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)
self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)

def unregister(self, name: str, not_fail=True) -> None:
"""Removes TextProcessing provider."""
require_capabilities("app_api", self._session.capabilities)
try:
self._session.ocs("DELETE", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
except NextcloudExceptionNotFound as e:
if not not_fail:
raise e from None
Expand All @@ -72,7 +72,7 @@ def get_entry(self, name: str) -> TextProcessingProvider | None:
require_capabilities("app_api", self._session.capabilities)
try:
return TextProcessingProvider(
self._session.ocs("GET", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
self._session.ocs("GET", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
)
except NextcloudExceptionNotFound:
return None
Expand All @@ -83,15 +83,13 @@ def report_result(self, task_id: int, result: str = "", error: str = "") -> None
with contextlib.suppress(NextcloudException):
self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
f"{self._session.ae_url}/{_EP_SUFFIX}",
json={"taskId": task_id, "result": result, "error": error},
)


class _AsyncTextProcessingProviderAPI:
"""API for registering TextProcessing providers."""

_ep_suffix: str = "ai_provider/text_processing"
"""API for TextProcessing providers."""

def __init__(self, session: AsyncNcSessionApp):
self._session = session
Expand All @@ -105,13 +103,13 @@ async def register(self, name: str, display_name: str, callback_url: str, task_t
"actionHandler": callback_url,
"taskType": task_type,
}
await self._session.ocs("POST", f"{self._session.ae_url}/{self._ep_suffix}", json=params)
await self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)

async def unregister(self, name: str, not_fail=True) -> None:
"""Removes TextProcessing provider."""
require_capabilities("app_api", await self._session.capabilities)
try:
await self._session.ocs("DELETE", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
await self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
except NextcloudExceptionNotFound as e:
if not not_fail:
raise e from None
Expand All @@ -121,7 +119,7 @@ async def get_entry(self, name: str) -> TextProcessingProvider | None:
require_capabilities("app_api", await self._session.capabilities)
try:
return TextProcessingProvider(
await self._session.ocs("GET", f"{self._session.ae_url}/{self._ep_suffix}", params={"name": name})
await self._session.ocs("GET", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name})
)
except NextcloudExceptionNotFound:
return None
Expand All @@ -132,6 +130,6 @@ async def report_result(self, task_id: int, result: str = "", error: str = "") -
with contextlib.suppress(NextcloudException):
await self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
f"{self._session.ae_url}/{_EP_SUFFIX}",
json={"taskId": task_id, "result": result, "error": error},
)
Loading