Sample Code for gpt-4o-audio/gpt-4o-mini-audio
Request Sandbox
1import requests2import base643import json45def generate_tts(api_key: str, input_text: str, voice: str, format: str, output_file: str):6 """7 Calls the AiApiGiaRe TTS API to generate audio from text and saves it to a file.89 Args:10 api_key: Your AiApiGiaRe API key.11 input_text: The text to convert to speech.12 voice: The desired voice for the audio (e.g., "shimmer").13 format: The desired audio format (e.g., "mp3").14 output_file: The path to save the generated audio file.15 """16 url = "https://api.AiApiGiaRe.io/v1/chat/completions"1718 headers = {19 "Authorization": f"Bearer {api_key}", #API Group openai20 "Content-Type": "application/json",21 }2223 data = {24 "model": "gpt-4o-mini-audio-preview-2024-12-17", #gpt-4o-audio-preview-2024-12-1725 "modalities": ["text", "audio"],26 "audio": {"voice": voice, "format": format},27 "echo": True,28 "messages": [29 {30 "role": "system",31 "content": "output audio as HD quality"32 },33 {34 "role": "user",35 "content": "Act as an echo. Repeat the user's input word for word, without adding, removing, or changing anything."36 },37 {38 "role": "user",39 "content": input_text40 }41 ]42 }4344 try:45 response = requests.post(url, headers=headers, json=data)46 response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)4748 response_data = response.json()4950 # Check if the expected audio data is present51 if (52 response_data.get("choices") and53 len(response_data["choices"]) > 0 and54 response_data["choices"][0].get("message") and55 response_data["choices"][0]["message"].get("audio") and56 response_data["choices"][0]["message"]["audio"].get("data")57 ):58 audio_base64 = response_data["choices"][0]["message"]["audio"]["data"]59 # Assuming the data is base64 encoded binary60 audio_binary = base64.b64decode(audio_base64)6162 with open(output_file, "wb") as f:63 f.write(audio_binary)64 print(f"Audio successfully saved to {output_file}")6566 else:67 print("Error: Could not find audio data in the response.")68 print("Response:", json.dumps(response_data, indent=2))6970 except requests.exceptions.RequestException as e:71 print(f"Error making request to AiApiGiaRe API: {e}")72 except KeyError as e:73 print(f"Error parsing response JSON: Missing key {e}")74 print("Response:", response.text) # Print raw response text for debugging75 except base64.binascii.Error as e:76 print(f"Error decoding base64 audio data: {e}")77 # Optionally print the problematic data snippet if safe/useful78 # print("Received data snippet:", response_data["choices"][0]["message"]["audio"]["data"][:100])79 except Exception as e:80 print(f"An unexpected error occurred: {e}")8182# Example usage (replace with your actual key and desired parameters)83if __name__ == "__main__":84 # Load API key from environment variable or configuration file for security85 # import os86 # api_key = os.environ.get("AiApiGiaRe_API_KEY")87 api_key = "sk-" # Replace with your actual API key8889 if api_key == "YOUR_API_KEY_HERE":90 print("Please replace 'YOUR_API_KEY_HERE' with your actual AiApiGiaRe API key.")91 else:92 text = "Display the resulting text on a screen to provide an accessible experience. In this example, we leverage features like speech to text and phrase list."93 generate_tts(94 api_key=api_key,95 input_text=text,96 voice="shimmer", # Example voice97 format="mp3", # Example format98 output_file="output_audio.mp3"99 )Target: api.aiapigiare.io.vn
TLS 1.3 ActiveSample Code for Audio API: gpt-4o-audio/gpt-4o-mini-audio
Endpoint
- URL:
https://AiApiGiaRe.one/v1/chat/completions - Method:
POST
Headers
(Code extracted to sandbox)