Skip to content

Speech2txt provider API rework #202

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 4 commits into from
Jan 12, 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
31 changes: 0 additions & 31 deletions .run/Speech2TxtProvider (last).run.xml

This file was deleted.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## [0.8.0 - 2024-01-xx]
## [0.8.0 - 2024-01-12]

### Added

Expand Down
15 changes: 0 additions & 15 deletions examples/as_app/speech2text/Dockerfile

This file was deleted.

43 changes: 0 additions & 43 deletions examples/as_app/speech2text/Makefile

This file was deleted.

37 changes: 0 additions & 37 deletions examples/as_app/speech2text/appinfo/info.xml

This file was deleted.

78 changes: 0 additions & 78 deletions examples/as_app/speech2text/lib/main.py

This file was deleted.

1 change: 0 additions & 1 deletion examples/as_app/speech2text/requirements.txt

This file was deleted.

2 changes: 1 addition & 1 deletion nc_py_api/ex_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
set_handlers,
talk_bot_app,
)
from .misc import persistent_storage, verify_version
from .misc import get_model_path, persistent_storage, verify_version
from .ui.files_actions import UiActionFileInfo
from .uvicorn_fastapi import run_app
7 changes: 7 additions & 0 deletions nc_py_api/ex_app/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ def verify_version(finalize_update: bool = True) -> tuple[str, str] | None:
version_file.write(os.environ["APP_VERSION"])
version_file.truncate()
return r


def get_model_path(model_name: str) -> str:
"""Wrapper around hugging_face's ``snapshot_download`` to return path to downloaded model directory."""
from huggingface_hub import snapshot_download # noqa isort:skip pylint: disable=C0415 disable=E0401

return snapshot_download(model_name, local_files_only=True, cache_dir=persistent_storage())
23 changes: 22 additions & 1 deletion nc_py_api/ex_app/providers/speech_to_text.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Nextcloud API for declaring SpeechToText provider."""

import contextlib
import dataclasses

from ..._exceptions import NextcloudExceptionNotFound
from ..._exceptions import NextcloudException, NextcloudExceptionNotFound
from ..._misc import require_capabilities
from ..._session import AsyncNcSessionApp, NcSessionApp

Expand Down Expand Up @@ -70,6 +71,16 @@ def get_entry(self, name: str) -> SpeechToTextProvider | None:
except NextcloudExceptionNotFound:
return None

def report_result(self, task_id: int, result: str = "", error: str = "") -> None:
"""Report results of speech to text task to Nextcloud."""
require_capabilities("app_api", self._session.capabilities)
with contextlib.suppress(NextcloudException):
self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
json={"taskId": task_id, "result": result, "error": error},
)


class _AsyncSpeechToTextProviderAPI:
"""API for registering Speech2Text providers."""
Expand Down Expand Up @@ -107,3 +118,13 @@ async def get_entry(self, name: str) -> SpeechToTextProvider | None:
)
except NextcloudExceptionNotFound:
return None

async def report_result(self, task_id: int, result: str = "", error: str = "") -> None:
"""Report results of speech to text task to Nextcloud."""
require_capabilities("app_api", await self._session.capabilities)
with contextlib.suppress(NextcloudException):
await self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
json={"taskId": task_id, "result": result, "error": error},
)
4 changes: 2 additions & 2 deletions nc_py_api/ex_app/providers/text_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def report_result(self, task_id: int, result: str = "", error: str = "") -> None
self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
params={"taskId": task_id, "result": result, "error": error},
json={"taskId": task_id, "result": result, "error": error},
)


Expand Down Expand Up @@ -133,5 +133,5 @@ async def report_result(self, task_id: int, result: str = "", error: str = "") -
await self._session.ocs(
"PUT",
f"{self._session.ae_url}/{self._ep_suffix}",
params={"taskId": task_id, "result": result, "error": error},
json={"taskId": task_id, "result": result, "error": error},
)
11 changes: 11 additions & 0 deletions tests/actual_tests/speech2text_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ async def test_speech2text_provider_async(anc_app):
await anc_app.providers.speech_to_text.unregister(result2.name, not_fail=False)
assert await anc_app.providers.speech_to_text.get_entry(result2.name) is None
assert str(result).find("name=") != -1


@pytest.mark.require_nc(major=29)
def test_speech2text_provider_fail_report(nc_app):
nc_app.providers.speech_to_text.report_result(999999)


@pytest.mark.asyncio(scope="session")
@pytest.mark.require_nc(major=29)
async def test_speech2text_provider_fail_report_async(anc_app):
await anc_app.providers.speech_to_text.report_result(999999)