Skip to content

Commit 6ef2d85

Browse files
authored
Speech2txt provider API rework (#202)
Changes proposed in this pull request: * Example was rewritten, and now has separate repo: https://github.com/cloud-py-api/speech2text_provider * added `report_result` method * added new `get_model_path` function to easy use hugging models in Providers --------- Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent a90dc77 commit 6ef2d85

File tree

12 files changed

+44
-210
lines changed

12 files changed

+44
-210
lines changed

.run/Speech2TxtProvider (last).run.xml

Lines changed: 0 additions & 31 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
## [0.8.0 - 2024-01-xx]
5+
## [0.8.0 - 2024-01-12]
66

77
### Added
88

examples/as_app/speech2text/Dockerfile

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/as_app/speech2text/Makefile

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/as_app/speech2text/appinfo/info.xml

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/as_app/speech2text/lib/main.py

Lines changed: 0 additions & 78 deletions
This file was deleted.

examples/as_app/speech2text/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

nc_py_api/ex_app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
set_handlers,
99
talk_bot_app,
1010
)
11-
from .misc import persistent_storage, verify_version
11+
from .misc import get_model_path, persistent_storage, verify_version
1212
from .ui.files_actions import UiActionFileInfo
1313
from .uvicorn_fastapi import run_app

nc_py_api/ex_app/misc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ def verify_version(finalize_update: bool = True) -> tuple[str, str] | None:
4343
version_file.write(os.environ["APP_VERSION"])
4444
version_file.truncate()
4545
return r
46+
47+
48+
def get_model_path(model_name: str) -> str:
49+
"""Wrapper around hugging_face's ``snapshot_download`` to return path to downloaded model directory."""
50+
from huggingface_hub import snapshot_download # noqa isort:skip pylint: disable=C0415 disable=E0401
51+
52+
return snapshot_download(model_name, local_files_only=True, cache_dir=persistent_storage())

nc_py_api/ex_app/providers/speech_to_text.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Nextcloud API for declaring SpeechToText provider."""
22

3+
import contextlib
34
import dataclasses
45

5-
from ..._exceptions import NextcloudExceptionNotFound
6+
from ..._exceptions import NextcloudException, NextcloudExceptionNotFound
67
from ..._misc import require_capabilities
78
from ..._session import AsyncNcSessionApp, NcSessionApp
89

@@ -70,6 +71,16 @@ def get_entry(self, name: str) -> SpeechToTextProvider | None:
7071
except NextcloudExceptionNotFound:
7172
return None
7273

74+
def report_result(self, task_id: int, result: str = "", error: str = "") -> None:
75+
"""Report results of speech to text task to Nextcloud."""
76+
require_capabilities("app_api", self._session.capabilities)
77+
with contextlib.suppress(NextcloudException):
78+
self._session.ocs(
79+
"PUT",
80+
f"{self._session.ae_url}/{self._ep_suffix}",
81+
json={"taskId": task_id, "result": result, "error": error},
82+
)
83+
7384

7485
class _AsyncSpeechToTextProviderAPI:
7586
"""API for registering Speech2Text providers."""
@@ -107,3 +118,13 @@ async def get_entry(self, name: str) -> SpeechToTextProvider | None:
107118
)
108119
except NextcloudExceptionNotFound:
109120
return None
121+
122+
async def report_result(self, task_id: int, result: str = "", error: str = "") -> None:
123+
"""Report results of speech to text task to Nextcloud."""
124+
require_capabilities("app_api", await self._session.capabilities)
125+
with contextlib.suppress(NextcloudException):
126+
await self._session.ocs(
127+
"PUT",
128+
f"{self._session.ae_url}/{self._ep_suffix}",
129+
json={"taskId": task_id, "result": result, "error": error},
130+
)

nc_py_api/ex_app/providers/text_processing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def report_result(self, task_id: int, result: str = "", error: str = "") -> None
8484
self._session.ocs(
8585
"PUT",
8686
f"{self._session.ae_url}/{self._ep_suffix}",
87-
params={"taskId": task_id, "result": result, "error": error},
87+
json={"taskId": task_id, "result": result, "error": error},
8888
)
8989

9090

@@ -133,5 +133,5 @@ async def report_result(self, task_id: int, result: str = "", error: str = "") -
133133
await self._session.ocs(
134134
"PUT",
135135
f"{self._session.ae_url}/{self._ep_suffix}",
136-
params={"taskId": task_id, "result": result, "error": error},
136+
json={"taskId": task_id, "result": result, "error": error},
137137
)

tests/actual_tests/speech2text_provider_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,14 @@ async def test_speech2text_provider_async(anc_app):
5454
await anc_app.providers.speech_to_text.unregister(result2.name, not_fail=False)
5555
assert await anc_app.providers.speech_to_text.get_entry(result2.name) is None
5656
assert str(result).find("name=") != -1
57+
58+
59+
@pytest.mark.require_nc(major=29)
60+
def test_speech2text_provider_fail_report(nc_app):
61+
nc_app.providers.speech_to_text.report_result(999999)
62+
63+
64+
@pytest.mark.asyncio(scope="session")
65+
@pytest.mark.require_nc(major=29)
66+
async def test_speech2text_provider_fail_report_async(anc_app):
67+
await anc_app.providers.speech_to_text.report_result(999999)

0 commit comments

Comments
 (0)