An api engine for openvoice written in python
OpenVoice
https://github.com/myshell-ai/OpenVoice/tree/main
API Client
https://github.com/ground-creative/openvoice-api-client-python
OpenAI sdk example
https://github.com/ground-creative/openvoice-api-python/blob/main/examples/openai_lib_example.py
Follow instructions here to install with docker
https://github.com/ground-creative/openvoice-docker
You are going to need to include OpenVoice dependencies manually.
- Clone the repository
git clone https://github.com/ground-creative/openvoice-api-python.git
- Change environment variables in env.sample file and rename it to .env
# install packages
pip install -r requirements.txt # install dependencies
# Run the server
python3 app.py
Method: POST
Endpoint: /{VERSION}/generate-audio
Params:
model(required)
the model to useinput(required)
the text to convert to speechspeed(default: 1.0)
the speed of the voiceresponse_format(url|bytes|base64|stream)(default: url)
the response formatvoice(default: raw)
the voice to use
Extra params V1:
style('default','whispering','shouting','excited','cheerful','terrified','angry','sad','friendly')
a style for the voice
Extra params V2:
accent(default: default language)
an accent for the voice
Method: POST
Endpoint: /{VERSION}/change-voice
Params:
model(required)
the model to useaudio_data(required)
base64 encoded audio datavoice(required)
the voice to useresponse_format(url|bytes|base64|stream)(default: url)
the response format
Method: GET
Endpoint: /audio-file/{FILENAME}
Params:
stream(true|false)(default: false)
Generate speech example
import requests, os
SERVER_PORT = os.getenv("SERVER_PORT", 5000)
output_file = 'outputs/generate_audio_url.wav'
if os.path.exists(output_file):
os.remove(output_file)
version = 'v2'
# Define the URL of the generate_audio endpoint
url = f'http://localhost:{SERVER_PORT}/{version}/generate-audio'
# Define the parameters for the POST request
payload = {
'model': 'en',
'input': 'Hello, this is a test. I am here, there and everywhere. Let me know how you feel, perhaps this can be real.',
'speed': 1.0,
'voice': 'elon',
'response_format': 'url',
#'style': 'excited' # v1 only
#'accent': 'en-au' #v2 only
}
try:
# Send the POST request to generate the audio
response = requests.post(url, json=payload, stream=False)
if response.status_code == 200:
response_data = response.json()
file_url = response_data['result']['data']['url']
print(f'Generated url: {file_url}')
response = requests.get(file_url, stream=False)
if response.status_code == 200:
with open(output_file, 'wb') as audio_file:
audio_file.write(response.content)
print(f'Audio file saved as {output_file}')
else:
print(f'Error getting file url: {response.status_code}')
print(response.json())
else:
print(f'Error: {response.status_code}')
print(response.json())
except requests.exceptions.RequestException as e:
print(f'Request failed: {e}')
Generate speech with openai SDK example
import logging, os
from openai import OpenAI
SERVER_PORT = os.getenv("SERVER_PORT", 5000)
version = 'v2'
output_file = 'outputs/openai_lib_example.wav'
if os.path.exists(output_file):
os.remove(output_file)
try:
# Initialize OpenAI client
client = OpenAI(
base_url=f'http://localhost:{SERVER_PORT}/{version}',
api_key='not-needed', # Required but ignored by OpenAI
)
# Make request to OpenAI API
with client.audio.speech.with_streaming_response.create(
input='Hello, this is a test. I am here, there and everywhere',
model='en',
voice="elon",
#extra_body={"accent": "en-au"} , # v2 only
#extra_body={"style": "angry"} , # v1 only
response_format="wav"
) as response:
response.stream_to_file(output_file)
except Exception as e:
logging.error(f"Unexpected error: {e}")
Look inside examples folder for more examples
cd tests
python -m unittest __FILE__
# or
python -m unittest __FILE__.CLASS__
# or
python -m unittest __FILE__.CLASS__.FUNCTION__