yoruba-practice / test.py
Olaolu Olugbenle
python file used for testing translator
10b4ef5
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())