|
|
|
|
|
""" |
|
|
Utility script to generate the OpenAI TTS sample audio file. |
|
|
|
|
|
This script generates sample_audio_summary_openai.mp3 using the same script |
|
|
that's used for the Eleven Labs sample, allowing users to compare voice quality |
|
|
between the two TTS providers in demo mode. |
|
|
|
|
|
Usage: |
|
|
python generate_openai_sample.py |
|
|
|
|
|
Requirements: |
|
|
- OPENAI_API_KEY must be set in .env file |
|
|
- OPENAI_TTS_VOICE must be set in .env file (defaults to 'onyx') |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv(override=True) |
|
|
|
|
|
|
|
|
from openai import OpenAI |
|
|
|
|
|
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "").strip() |
|
|
OPENAI_TTS_VOICE = os.getenv("OPENAI_TTS_VOICE", "onyx").strip() |
|
|
OUTPUT_FILE = "sample_audio_summary_openai.mp3" |
|
|
|
|
|
|
|
|
SAMPLE_AUDIO_SCRIPT = ( |
|
|
"Here's your spending summary for January 1st, 2014 to January 31st, 2014. " |
|
|
"You had 16 expense transactions, totaling 8 thousand 250 dollars and 65 cents. " |
|
|
"Your top spending categories were: First, Home Improvement at 5 thousand dollars, " |
|
|
"across 2 transactions. Next, Other Expenses at 1 thousand 985 dollars. " |
|
|
"Next, Postage and Shipping at 500 dollars. Next, Gasoline/Fuel at 350 dollars. " |
|
|
"And finally, Automotive Expenses at 200 dollars. " |
|
|
"Home Improvement represents 61% of your total spending. That's your spending overview." |
|
|
) |
|
|
|
|
|
|
|
|
def main(): |
|
|
print("=" * 60) |
|
|
print("OpenAI TTS Sample Audio Generator") |
|
|
print("=" * 60) |
|
|
|
|
|
|
|
|
if not OPENAI_API_KEY: |
|
|
print("β Error: OPENAI_API_KEY not found in .env file") |
|
|
sys.exit(1) |
|
|
|
|
|
if not OPENAI_TTS_VOICE: |
|
|
print("β Error: OPENAI_TTS_VOICE not found in .env file") |
|
|
sys.exit(1) |
|
|
|
|
|
print(f"API Key: {OPENAI_API_KEY[:20]}...") |
|
|
print(f"Voice: {OPENAI_TTS_VOICE}") |
|
|
print(f"Output: {OUTPUT_FILE}") |
|
|
print(f"Script length: {len(SAMPLE_AUDIO_SCRIPT)} characters") |
|
|
print() |
|
|
|
|
|
try: |
|
|
print("π Generating audio with OpenAI TTS...") |
|
|
client = OpenAI(api_key=OPENAI_API_KEY) |
|
|
|
|
|
response = client.audio.speech.create( |
|
|
model="tts-1", |
|
|
voice=OPENAI_TTS_VOICE, |
|
|
input=SAMPLE_AUDIO_SCRIPT, |
|
|
response_format="mp3" |
|
|
) |
|
|
|
|
|
|
|
|
with open(OUTPUT_FILE, "wb") as f: |
|
|
f.write(response.content) |
|
|
|
|
|
file_size = os.path.getsize(OUTPUT_FILE) |
|
|
print() |
|
|
print(f"β
Successfully generated: {OUTPUT_FILE}") |
|
|
print(f" File size: {file_size:,} bytes") |
|
|
print() |
|
|
print("You can now restart the Gradio app to use provider-specific") |
|
|
print("sample audio files in demo mode.") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Error: {e}") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|
|
|
|