Sample Code for gpt-4o-audio/gpt-4o-mini-audio

Request Sandbox
1import requests
2import base64
3import json
4
5def 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.
8
9 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"
17
18 headers = {
19 "Authorization": f"Bearer {api_key}", #API Group openai
20 "Content-Type": "application/json",
21 }
22
23 data = {
24 "model": "gpt-4o-mini-audio-preview-2024-12-17", #gpt-4o-audio-preview-2024-12-17
25 "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_text
40 }
41 ]
42 }
43
44 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)
47
48 response_data = response.json()
49
50 # Check if the expected audio data is present
51 if (
52 response_data.get("choices") and
53 len(response_data["choices"]) > 0 and
54 response_data["choices"][0].get("message") and
55 response_data["choices"][0]["message"].get("audio") and
56 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 binary
60 audio_binary = base64.b64decode(audio_base64)
61
62 with open(output_file, "wb") as f:
63 f.write(audio_binary)
64 print(f"Audio successfully saved to {output_file}")
65
66 else:
67 print("Error: Could not find audio data in the response.")
68 print("Response:", json.dumps(response_data, indent=2))
69
70 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 debugging
75 except base64.binascii.Error as e:
76 print(f"Error decoding base64 audio data: {e}")
77 # Optionally print the problematic data snippet if safe/useful
78 # 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}")
81
82# Example usage (replace with your actual key and desired parameters)
83if __name__ == "__main__":
84 # Load API key from environment variable or configuration file for security
85 # import os
86 # api_key = os.environ.get("AiApiGiaRe_API_KEY")
87 api_key = "sk-" # Replace with your actual API key
88
89 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 voice
97 format="mp3", # Example format
98 output_file="output_audio.mp3"
99 )
Target: api.aiapigiare.io.vn
TLS 1.3 Active

Sample 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)

Infrastructure: AiApiGiaRe-v10.0
Gateway Status: Operational