TTS API access, not finding the key

Hi All

I am trying to use the MyShell OpenVoice to “clone” a voice, I am in my early days of experimentation. I have been looking and noticed a few people are having the same issue, but nothing yet that resolved it.

But, when I try and run the script I keep getting:

eight_norm.py:28: UserWarning: torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.
warnings.warn(“torch.nn.utils.weight_norm is deprecated in favor of torch.nn.utils.parametrizations.weight_norm.”)
Loaded checkpoint ‘/home/user/Documents/GitHub/OpenVoice/checkpoints/converter/checkpoint.pth’
missing/unexpected keys:
Traceback (most recent call last):
File “/home/user/Documents/OpenVoice/SampleOV.py”, line 21, in
client = OpenAI(api_key=os.environ.get(“OPENAI_API_KEY”))
File “/home/user/anaconda3/envs/openvoice/lib/python3.9/site-packages/openai/_client.py”, line 98, in init
raise OpenAIError(
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

I have tried an apitest.py script with this in it:

import os

from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
print(os.environ.get("OPENAI_API_KEY")) #key should now be available

But, when I run it, I just get a response saying “None”.

I have set an environment file, OpenAI.env that is located in the directory that I have the Python scripts in, this is what I have in that file:

OPENAI_API_KEY=sk-

I’m really not sure what I’m doing wrong. Can anyone give any guidance?

This is my OpenVoice script, more or less the same as the OpenVoice tutorials:

import os
import torch
from openvoice import se_extractor
from openvoice.api import ToneColorConverter
from openai import OpenAI
from dotenv import load_dotenv

ckpt_converter = '/home/user/Documents/GitHub/OpenVoice/checkpoints/converter'
device="cuda:0" if torch.cuda.is_available() else "cpu"
output_dir = '/home/user/Documents/OpenVoice/ovout'

tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)
tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')

os.makedirs(output_dir, exist_ok=True)

# Please create a file named .env and place your
# OpenAI key as OPENAI_API_KEY=xxx
load_dotenv() 

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

response = client.audio.speech.create(
    model="tts-1",
    voice="nova",
    input="This audio will be used to extract the base speaker tone color embedding. " + \
        "Typically a very short audio should be sufficient, but increasing the audio " + \
        "length will also improve the output audio quality."
)

response.stream_to_file(f"{output_dir}/openai_source_output.mp3")

base_speaker = f"{output_dir}/openai_source_output.mp3"
source_se, audio_name = se_extractor.get_se(base_speaker, tone_color_converter, vad=True)

reference_speaker = '/home/gary/Documents/OpenVoice/SampleVoice.mp3'
target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, vad=True)

# Run the base speaker tts
text = [
    "MyShell is a decentralized and comprehensive platform for discovering, creating, and staking AI-native apps.",
    "MyShell es una plataforma descentralizada y completa para descubrir, crear y apostar por aplicaciones nativas de IA.",
    "MyShell est une plateforme décentralisée et complète pour découvrir, créer et miser sur des applications natives d'IA.",
    "MyShell ist eine dezentralisierte und umfassende Plattform zum Entdecken, Erstellen und Staken von KI-nativen Apps.",
    "MyShell è una piattaforma decentralizzata e completa per scoprire, creare e scommettere su app native di intelligenza artificiale.",
    "MyShellは、AIネイティブアプリの発見、作成、およびステーキングのための分散型かつ包括的なプラットフォームです。",
    "MyShell — это децентрализованная и всеобъемлющая платформа для обнаружения, создания и стейкинга AI-ориентированных приложений.",
    "MyShell هي منصة لامركزية وشاملة لاكتشاف وإنشاء ورهان تطبيقات الذكاء الاصطناعي الأصلية.",
    "MyShell是一个去中心化且全面的平台,用于发现、创建和投资AI原生应用程序。",
    "MyShell एक विकेंद्रीकृत और व्यापक मंच है, जो AI-मूल ऐप्स की खोज, सृजन और स्टेकिंग के लिए है।",
    "MyShell é uma plataforma descentralizada e abrangente para descobrir, criar e apostar em aplicativos nativos de IA."
]
src_path = f'{output_dir}/tmp.wav'

for i, t in enumerate(text):

    response = client.audio.speech.create(
        model="tts-1",
        voice="nova",
        input=t,
    )

    response.stream_to_file(src_path)

    save_path = f'{output_dir}/output_crosslingual_{i}.wav'

    # Run the tone color converter
    encode_message = "@MyShell"
    tone_color_converter.convert(
        audio_src_path=src_path, 
        src_se=source_se, 
        tgt_se=target_se, 
        output_path=save_path,
        message=encode_message)

That line is either something an AI has written for you, or you know exactly why windows libraries are necessary.

“key should now be available” just from printing it is something an AI might also write.

Assuming Windows, go to or type “run” in the start menu, and run this.

"C:\Windows\system32\rundll32.exe" sysdm.cpl,EditEnvironmentVariables

In the bottom, create a new OPENAI_API_KEY environment variable with your API key received from the openai project in your account.

Then your code does not need anything within client = OpenAI() - the environment API key is automatically used.

1 Like

Thanks for the tip, that line was suggested by ChatGPT. I am very new to Python.

I am using Ubuntu 22.04 not Windows.

OK, I have figured it out, I think. I have found the relevent alternative to this using Ubuntu via XDA Developers.

I have so far run export OPENAI_API_KEY=“sk-the rest of my key”

Looking at the guide, it looks like this has to be set each time, but there is a permanent way of setting it using /etc/environment.