Skip to content
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

Update #9

Merged
merged 5 commits into from
Jun 20, 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
12 changes: 12 additions & 0 deletions examples/speech_to_text/async_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from trengine.speech_to_text import AsyncSpeechToText

speech = AsyncSpeechToText()


async def main():
print(await speech.to_text("audio.ogg", language="en"))


import asyncio

asyncio.run(main())
5 changes: 5 additions & 0 deletions examples/speech_to_text/sync_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from trengine.speech_to_text import SpeechToText

speech = SpeechToText()

print(speech.to_text("audio.ogg", language="en"))
12 changes: 0 additions & 12 deletions examples/speechtotext/async_example.py

This file was deleted.

5 changes: 0 additions & 5 deletions examples/speechtotext/sync_example.py

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "trengine"
version = "2.3"
version = "2.5"
authors = [
{ name="ZAID", email="[email protected]" },
]
Expand Down
44 changes: 10 additions & 34 deletions trengine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<<<<<<< HEAD
from . import ajax, google, hozory, speechtotext, tdict, ocr
=======
from . import ajax, google, hozory, SpeechtoText, tdict, ocr
>>>>>>> 73e5984a89d06211a302484d036a0281de79bd63
from . import ajax, google, hozory, speech_to_text, tdict, ocr

__all__ = ["AsyncEngine", "Engine"]

Expand Down Expand Up @@ -56,25 +52,15 @@ def ocr(self) -> "ocr.OCR":
ocr.OCR: Ocr Class.
"""
return ocr.OCR

@property
<<<<<<< HEAD
def SpeechtoText(self) -> "speechtotext.SpeechToTextService":
"""Use SpeechToText engine.

Returns:
speechtotext.SpeechToTextService: SpeechtoText Engine Class.
"""
return speechtotext.SpeechToTextService
=======
def SpeechtoText(self) -> "stt.SpeechToText":
"""Use SpeechToText engine.
@property
def speech_to_text(self) -> "speech_to_text.SpeechToText":
"""Use speech_to_text service.

Returns:
stt.SpeechToText: stt Engine Class.
speech_to_text.SpeechToText: speech_to_text Engine Class.
"""
return stt.SpeechToText
>>>>>>> 73e5984a89d06211a302484d036a0281de79bd63
return speech_to_text.SpeechToText


class AsyncEngine:
Expand Down Expand Up @@ -124,20 +110,10 @@ def ocr(self) -> "ocr.AsyncOCR":
return ocr.AsyncOCR

@property
<<<<<<< HEAD
def SpeechtoText(self) -> "speechtotext.AsyncSpeechToTextService":
"""Use speechtotext engine.

Returns:
speechtotext.AsyncSpeechToTextService: SpeechtoText Engine Class.
"""
return speechtotext.AsyncSpeechToTextService
=======
def SpeechtoText(self) -> "stt.AsyncSpeechToText":
"""Use SpeechToText engine.
def speech_to_text(self) -> "speech_to_text.AsyncSpeechToText":
"""Use speech_to_text service.

Returns:
stt.AsyncSpeechToText: stt Engine Class.
speech_to_text.AsyncSpeechToText: speech_to_text Engine Class.
"""
return stt.AsyncSpeechToText
>>>>>>> 73e5984a89d06211a302484d036a0281de79bd63
return speech_to_text.AsyncSpeechToText
2 changes: 1 addition & 1 deletion trengine/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class OCR:
@staticmethod
def from_image(self, path: str, language: str = "eng") -> str:
def from_image(path: str, language: str = "eng") -> str:
loop = asyncio.get_event_loop()
return loop.run_until_complete(AsyncOCR.from_image(path, language))

Expand Down
54 changes: 25 additions & 29 deletions trengine/SpeechtoText.py → trengine/speech_to_text.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,67 @@
from aiohttp import ClientSession
import os
import requests

from aiohttp import ClientSession, FormData

from .types import SpeechToTextResult
from .exceptions import ApiException

from json import dumps

import requests


class SpeechToText:
@staticmethod
def speechtotext(file_path: str, language: str = "en") -> "SpeechToTextResult":
def to_text(file_path: str, language: str = "en") -> "SpeechToTextResult":
"""Speech to text using google cloud.

Args:
file_path (str): The path to the audio file to be translated into text. This should be a valid path to an audio file
language (str, optional): he language code for the speech in the audio file. Defaults to "en".
file_path (str): The path to the audio file to be trancribed into text. This should be a valid path to an audio file
language (str, optional): the source language code of the audio file. Defaults to "en".
"""
files = {
'file': open(file_path, 'rb'),
}
data = {
'language': language,
}
with open(file_path, "rb") as f:
b = f.read()
response = requests.post(
f"https://api.devrio.org/api/v1/SpeechToText/",
files=files,
data=data
data={
"language": language,
},
files={"file": b},
)
try:
result = response.json()
except Exception as e:
raise BaseException(str(e))

if result["ok"] is False:
if not result["ok"]:
raise ApiException(dumps(result["erorr"], indent=2, ensure_ascii=False))

return SpeechToTextResult.parse(result, file_path)


class AsyncSpeechToText:
@staticmethod
async def speechtotext(file_path: str, language: str = "en") -> "SpeechToTextResult":
async def to_text(file_path: str, language: str = "en") -> "SpeechToTextResult":
"""Speech to text using google cloud.

Args:
file_path (str): The path to the audio file to be translated into text. This should be a valid path to an audio file
language (str, optional): he language code for the speech in the audio file. Defaults to "en".
file_path (str): The path to the audio file to be trancribed into text. This should be a valid path to an audio file
language (str, optional): the source language code of the audio file. Defaults to "en".
"""
with open(file_path, "rb") as f:
b = f.read()
async with ClientSession() as session:
files = {
'file': open(file_path, 'rb'),
}
data = {
'language': language,
}
async with session.get(
f"https://api.devrio.org/api/v1/SpeechToText/",
files=files,
data=data
form = FormData()
form.add_field("file", b, filename=os.path.basename(file_path))
form.add_field("language", language)
async with session.request(
"post", f"https://api.devrio.org/api/v1/SpeechToText/", data=form
) as response:
try:
result = await response.json()
except Exception as e:
raise BaseException(str(e))

if result["ok"] is False:
if not result["ok"]:
raise ApiException(
dumps(result["erorr"], indent=2, ensure_ascii=False)
)
Expand Down
73 changes: 0 additions & 73 deletions trengine/speechtotext.py

This file was deleted.

1 change: 1 addition & 0 deletions trengine/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def parse(d: dict, dest: str) -> "HozoryTranslateResult":
voice_link=d["Voice_link"],
)


@dataclass
class SpeechToTextResult(Base):
status: bool
Expand Down
Loading