Skip to content

[Feat] Deepgram TTS #191

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 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
80 changes: 74 additions & 6 deletions edenai_apis/apis/deepgram/deepgram_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import base64
from io import BytesIO
from pathlib import Path
from time import time
from typing import Dict, List, Optional
Expand All @@ -12,6 +14,9 @@
SpeechDiarizationEntry,
SpeechDiarization,
)
from edenai_apis.features.audio.text_to_speech.text_to_speech_dataclass import (
TextToSpeechDataClass,
)
from edenai_apis.loaders.data_loader import ProviderDataEnum
from edenai_apis.loaders.loaders import load_provider
from edenai_apis.utils.exception import ProviderException
Expand All @@ -20,8 +25,13 @@
AsyncLaunchJobResponseType,
AsyncPendingResponseType,
AsyncResponseType,
ResponseType,
)
from edenai_apis.utils.upload_s3 import (
upload_file_to_s3,
upload_file_bytes_to_s3,
USER_PROCESS,
)
from edenai_apis.utils.upload_s3 import upload_file_to_s3


class DeepgramApi(ProviderInterface, AudioInterface):
Expand Down Expand Up @@ -68,7 +78,6 @@ def audio__speech_to_text_async__launch_job(

data = {"url": content_url}


data_config = {
"language": language,
"callback": self.webhook_url,
Expand All @@ -91,12 +100,14 @@ def audio__speech_to_text_async__launch_job(
if isinstance(value, bool):
data_config[key] = str(value).lower()

response = requests.post(self.url, headers=headers, json=data, params=data_config)
response = requests.post(
self.url, headers=headers, json=data, params=data_config
)
result = response.json()
if response.status_code != 200:
raise ProviderException(
f"{result.get('err_code')}: {result.get('err_msg')}",
code = response.status_code
code=response.status_code,
)

transcribe_id = response.json()["request_id"]
Expand All @@ -119,7 +130,7 @@ def audio__speech_to_text_async__get_job_result(
)

if response_status != 200:
raise ProviderException(wehbook_result, code = response_status)
raise ProviderException(wehbook_result, code=response_status)
try:
original_response = json.loads(wehbook_result[0]["content"])
except Exception:
Expand All @@ -136,7 +147,7 @@ def audio__speech_to_text_async__get_job_result(
if original_response.get("err_code"):
raise ProviderException(
f"{original_response.get('err_code')}: {original_response.get('err_msg')}",
code = response_status
code=response_status,
)

if not "results" in original_response:
Expand Down Expand Up @@ -177,3 +188,60 @@ def audio__speech_to_text_async__get_job_result(
standardized_response=standardized_response,
provider_job_id=public_provider_job_id,
)

def audio__text_to_speech(
self,
language: str,
text: str,
option: str,
voice_id: str,
audio_format: str,
speaking_rate: int,
speaking_pitch: int,
speaking_volume: int,
sampling_rate: int,
) -> ResponseType[TextToSpeechDataClass]:
_, model = voice_id.split("_")
base_url = f"https://api.deepgram.com/v1/speak?model={model}"
if audio_format:
base_url += f"&container={audio_format}"

if sampling_rate and sampling_rate != 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry to be samet :(
we can actually than just write:

if sampling_rate:

Copy link
Contributor Author

@Daggx Daggx Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is not accepted and it will return an error (if the user send 0 or None than it should ignore the sampling_rate)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, with the condition if sampling_rate he will automatically not enter the if condition (0 is considered as False also)

base_url += f"&sample_rate={sampling_rate}"

headers = {
"Authorization": f"Token {self.api_key}",
"Content-Type": "application/json",
}

payload = {"text": text}
response = requests.post(
base_url,
headers=headers,
json=payload,
)
if response.status_code != 200:
try:
result = response.json()
except json.JSONDecodeError as exc:
raise ProviderException(
code=500, message="Internal Server Error"
) from exc

raise ProviderException(
code=response.status_code, message=result.get("err_msg")
)

audio_content = BytesIO(response.content)
audio = base64.b64encode(audio_content.read()).decode("utf-8")
audio_content.seek(0)
resource_url = upload_file_bytes_to_s3(
audio_content, f".{audio_format}", USER_PROCESS
)

return ResponseType[TextToSpeechDataClass](
original_response=response.content,
standardized_response=TextToSpeechDataClass(
audio=audio, voice_type=1, audio_resource_url=resource_url
),
)
32 changes: 32 additions & 0 deletions edenai_apis/apis/deepgram/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@
"default_model": "enhanced"
},
"version": "v1"
},
"text_to_speech": {
"constraints": {
"languages": [
"en"
],
"audio_format": [
"mp3",
"wav",
"flac",
"aac"
],
"voice_ids": {
"FEMALE": [
"en_aura-asteria-en",
"en_aura-luna-en",
"en_aura-stella-en",
"en_aura-athena-en",
"en_aura-hera-en"
],
"MALE": [
"en_aura-orion-en",
"en_aura-arcas-en",
"en_aura-perseus-en",
"en_aura-angus-en",
"en_aura-orpheus-en",
"en_aura-helios-en",
"en_aura-zeus-en"
]
}
},
"version": "v1"
}
}
}
Loading
Loading