Spaces:
Paused
Paused
alan
commited on
Commit
·
c5c2329
1
Parent(s):
ba65936
added openai
Browse files- app.py +10 -6
- requirements.txt +1 -0
- utils.py +16 -1
app.py
CHANGED
|
@@ -15,11 +15,10 @@ import tempfile
|
|
| 15 |
from pydub import AudioSegment
|
| 16 |
import requests
|
| 17 |
import json
|
| 18 |
-
from google.cloud import texttospeech
|
| 19 |
|
| 20 |
-
from utils import
|
| 21 |
|
| 22 |
-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] =
|
| 23 |
|
| 24 |
def match_target_amplitude(sound, target_dBFS):
|
| 25 |
change_in_dBFS = target_dBFS - sound.dBFS
|
|
@@ -58,7 +57,8 @@ AVAILABLE_MODELS = {
|
|
| 58 |
'KOTOBA-SPEECH-JACOB': 'kotoba-speech-jacob',
|
| 59 |
'BLANE-TTS': 'blane-tts',
|
| 60 |
'AMITARO-VITS': 'amitaro-vits',
|
| 61 |
-
'GOOGLE-
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
SPACE_ID = os.getenv('SPACE_ID')
|
|
@@ -396,7 +396,8 @@ model_names = {
|
|
| 396 |
'kotoba-speech-jacob': 'KOTOBA-SPEECH-v0.1-JACOB',
|
| 397 |
'blane-tts': 'BLANE-TTS',
|
| 398 |
'amitaro-vits': 'AMITARO-VITS',
|
| 399 |
-
'google-
|
|
|
|
| 400 |
# 'styletts2': 'StyleTTS 2',
|
| 401 |
}
|
| 402 |
model_licenses = {
|
|
@@ -744,9 +745,12 @@ def synthandreturn(text):
|
|
| 744 |
print(model_args[model])
|
| 745 |
print(model_kwargs[model])
|
| 746 |
result = router.predict(*model_args[model], **model_kwargs[model])
|
| 747 |
-
elif model == "google-
|
| 748 |
local_filename = '/tmp/' + str(mkuuid(None)) + '.wav'
|
| 749 |
result = get_google_tts(text, local_filename=local_filename)
|
|
|
|
|
|
|
|
|
|
| 750 |
else:
|
| 751 |
result = get_tts_file(text, model)
|
| 752 |
# URL to download the file from
|
|
|
|
| 15 |
from pydub import AudioSegment
|
| 16 |
import requests
|
| 17 |
import json
|
|
|
|
| 18 |
|
| 19 |
+
from utils import get_google_credentials, get_google_tts, get_openai_tts
|
| 20 |
|
| 21 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = get_google_credentials()
|
| 22 |
|
| 23 |
def match_target_amplitude(sound, target_dBFS):
|
| 24 |
change_in_dBFS = target_dBFS - sound.dBFS
|
|
|
|
| 57 |
'KOTOBA-SPEECH-JACOB': 'kotoba-speech-jacob',
|
| 58 |
'BLANE-TTS': 'blane-tts',
|
| 59 |
'AMITARO-VITS': 'amitaro-vits',
|
| 60 |
+
'GOOGLE-TTS': 'google-tts',
|
| 61 |
+
'OPENAI-TTS': 'openai-tts'
|
| 62 |
}
|
| 63 |
|
| 64 |
SPACE_ID = os.getenv('SPACE_ID')
|
|
|
|
| 396 |
'kotoba-speech-jacob': 'KOTOBA-SPEECH-v0.1-JACOB',
|
| 397 |
'blane-tts': 'BLANE-TTS',
|
| 398 |
'amitaro-vits': 'AMITARO-VITS',
|
| 399 |
+
'google-tts': 'google-tts',
|
| 400 |
+
'openai-tts': 'openai-tts'
|
| 401 |
# 'styletts2': 'StyleTTS 2',
|
| 402 |
}
|
| 403 |
model_licenses = {
|
|
|
|
| 745 |
print(model_args[model])
|
| 746 |
print(model_kwargs[model])
|
| 747 |
result = router.predict(*model_args[model], **model_kwargs[model])
|
| 748 |
+
elif model == "google-tts":
|
| 749 |
local_filename = '/tmp/' + str(mkuuid(None)) + '.wav'
|
| 750 |
result = get_google_tts(text, local_filename=local_filename)
|
| 751 |
+
elif model == "openai-tts":
|
| 752 |
+
local_filename = '/tmp/' + str(mkuuid(None)) + '.wav'
|
| 753 |
+
result = get_openai_tts(text, local_filename=local_filename)
|
| 754 |
else:
|
| 755 |
result = get_tts_file(text, model)
|
| 756 |
# URL to download the file from
|
requirements.txt
CHANGED
|
@@ -9,3 +9,4 @@ langdetect
|
|
| 9 |
pydub
|
| 10 |
gradio-space-ci @ git+https://huggingface.co/spaces/Wauplin/gradio-space-ci@0.2.1
|
| 11 |
google-cloud-texttospeech
|
|
|
|
|
|
| 9 |
pydub
|
| 10 |
gradio-space-ci @ git+https://huggingface.co/spaces/Wauplin/gradio-space-ci@0.2.1
|
| 11 |
google-cloud-texttospeech
|
| 12 |
+
openai
|
utils.py
CHANGED
|
@@ -3,8 +3,23 @@ import json
|
|
| 3 |
import tempfile
|
| 4 |
from google.cloud import texttospeech
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
creds_json_str = os.getenv("GCP_CREDENTIAL_JSON") # get json credentials stored as a string
|
| 9 |
|
| 10 |
# create a temporary file
|
|
|
|
| 3 |
import tempfile
|
| 4 |
from google.cloud import texttospeech
|
| 5 |
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
|
| 9 |
+
def get_openai_tts(text, local_filename):
|
| 10 |
+
api_key = os.getenv("OPENAI_KEY")
|
| 11 |
+
client = OpenAI(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# speech_file_path = Path(__file__).parent / "speech.mp3"
|
| 14 |
+
response = client.audio.speech.create(
|
| 15 |
+
model="tts-1",
|
| 16 |
+
voice="alloy",
|
| 17 |
+
input=text
|
| 18 |
+
)
|
| 19 |
+
response.stream_to_file(local_filename)
|
| 20 |
+
return local_filename
|
| 21 |
|
| 22 |
+
def get_google_credentials():
|
| 23 |
creds_json_str = os.getenv("GCP_CREDENTIAL_JSON") # get json credentials stored as a string
|
| 24 |
|
| 25 |
# create a temporary file
|