File size: 2,582 Bytes
10b4ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
import os
import base64
import tempfile
from dotenv import load_dotenv

# # TODO(developer): Update and un-comment below line
# PROJECT_ID = "perfect-spanner-475822-p6"

load_dotenv()

# # Load and decode Google credentials (Base64 Encoding)
# creds_b64 = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON") #get the b64 string
# if creds_b64:
#     creds_json = base64.b64decode(creds_b64).decode("utf-8") #decode to json string
#     temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json") #create a temp file
#     temp_file.write(creds_json.encode("utf-8")) #write json to this file
#     temp_file.flush() #write
#     os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = temp_file.name #update cred
# else:
#     print("⚠️ No GCP creds found.")

# # Instantiates a client
# client = SpeechClient()

# # Reads a file as bytes
# with open("yoruba-practice/audio.m4a", "rb") as f:
#     audio_content = f.read()

# config = cloud_speech.RecognitionConfig(
#     auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
#     language_codes=["en-US"],
#     model="chirp_3",
# )

# request = cloud_speech.RecognizeRequest(
#     recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
#     config=config,
#     content=audio_content,
# )

# # Transcribes the audio into text
# response = client.recognize(request=request)

# for result in response.results:
#     print(f"Transcript: {result.alternatives[0].transcript}")



#SPITCH TEST
import os
from spitch import Spitch

client = Spitch()

with open("new.mp3", "wb") as f:
    response = client.speech.generate(
        text="Oya, no dey do dat kind tin o. I no get time to waste. Johnny my son, I send you to school to learn how to spell your name",
        language="en",
        voice="jude",
        format="mp3"
    )
    f.write(response.read())



#Yarn GPT TTS TEST
import requests


API_URL = "https://yarngpt.ai/api/v1/tts"
API_KEY = os.getenv("YARNGPT_API_KEY")

headers = {
    "Authorization": f"Bearer {API_KEY}"
}

payload = {
    "text": "Ẹ káàárọ̀ ọ̀rẹ́ mi. Mo fẹ́ ra ẹ̀wà.",
    "voice": "Idera",
}

response = requests.post(API_URL, headers=headers, json=payload, stream=True)

if response.status_code == 200:
    with open("output.mp3", "wb") as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    print("Audio file saved as output.mp3")
else:
    print(f"Error: {response.status_code}")
    print(response.json())