启航绘图和TTS的 API 使用文档
启航绘图和TTS的API 使用文档
本文档介绍了如何使用 启航AI 提供的绘图 API 和 TTS API。
1. 绘图 API (Image Generation)
功能描述
此 API 允许您根据文本提示词生成图像。
请求方法
`POST`
请求地址
`https://api.qhaigc.net/v1/images/generations`
请求头 (Headers)
Header | 描述 | 示例值 | 是否必须 |
---|---|---|---|
`Authorization` | 您的 API Key。请替换`'你的api'` 为您的实际 Key。 | `你的api` | 是 |
`Content-Type` | 请求体格式,必须为 JSON。 | `application/json` | 是 |
`User-Agent` | 客户端标识(可选,但建议提供)。 | `Apifox/1.0.0 (https://apifox.com)` | 否 |
`Accept` | 期望接收的响应格式(通常为`*/*` 或 `application/json`)。 | `*/*` | 否 |
`Host` | API 服务器地址。 | `api.qhaigc.net` | 是 |
`Connection` | 连接类型。 | `keep-alive` | 否 |
请求体 (Request Body)
请求体需要是 JSON 格式,包含以下参数:
参数 | 类型 | 描述 | 示例值 | 是否必须 |
---|---|---|---|---|
`prompt` | string | 描述您想要生成的图像的文本提示词。 | `"girl, white hair, winter"` | 是 |
`model` | string | 指定使用的图像生成模型。 | `"AbsoluteReality"` | 是 |
`size` | string | 指定生成图像的分辨率。可选值包括:`"512x768"`, `"768x1280"`, `"512x512"`, `"1280x768"`。 | `"512x768"` | 是 |
Python 示例代码
import http.client
import json
conn = http.client.HTTPSConnection("api.qhaigc.net")
# 构建请求体 payload
payload = json.dumps({
"prompt": "girl, white hair, winter", # 图像描述提示词
"model": "AbsoluteReality", # 使用的模型
"size": "512x768" # 图像分辨率
})
# 构建请求头 headers
headers = {
'Authorization': '你的api', # !!!请替换为你的真实 API Key !!!
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json',
'Accept': '\*/\*',
'Host': 'api.qhaigc.net',
'Connection': 'keep-alive'
}
try:
# 发送 POST 请求
conn.request("POST", "/v1/images/generations", payload, headers)
# 获取响应
res = conn.getresponse()
# 读取响应数据
data = res.read()
# 打印解码后的响应内容 (通常是 JSON 格式,包含图片 URL 或状态信息)
print(f"Status: {res.status} {res.reason}")
print("Response Body:")
print(data.decode("utf-8"))
except Exception as e:
print(f"An error occurred: {e}")
finally:
# 关闭连接
conn.close()
响应说明
API 的响应通常是一个 JSON 对象,包含生成图像的 URL。示例代码中的 `data.decode("utf-8")` 用于打印响应体内容。
注意事项
* 请务必将 `'你的api'` 替换为您自己的有效 API Key。
* 确保请求体是合法的 JSON 格式。
* 根据需要选择合适的 `model` 和 `size`。
2. TTS API (Text-to-Speech)
功能描述
此 API 允许您将文本转换为语音音频。
请求方法
`POST`
请求地址
`https://api.qhaigc.net/v1/audio/speech`
请求头 (Headers)
Header | 描述 | 示例值 | 是否必须 |
---|---|---|---|
`Authorization` | 您的 API Key。请替换`'你的key'` 为您的实际 Key。 | `你的key` | 是 |
`Content-Type` | 请求体格式,必须为 JSON。 | `application/json` | 是 |
`User-Agent` | 客户端标识(可选,但建议提供)。 | `Apifox/1.0.0 (https://apifox.com)` | 否 |
`Accept` | 期望接收的响应格式(通常为`*/*` 或 `audio/*`)。 | `*/*` | 否 |
`Host` | API 服务器地址。 | `api.qhaigc.net` | 是 |
`Connection` | 连接类型。 | `keep-alive` | 否 |
请求体 (Request Body)
请求体需要是 JSON 格式,包含以下参数:
参数 | 类型 | 描述 | 示例值 | 是否必须 |
---|---|---|---|---|
`model` | string | 指定使用的 TTS 声音模型。 | `"qhai-tts:爱丽丝"` | 是 |
`input` | string | 需要转换为语音的文本内容。 | `"这是一段测试文本"` | 是 |
Python 示例代码
import http.client
import json
conn = http.client.HTTPSConnection("api.qhaigc.net")
# 构建请求体 payload
payload = json.dumps({
"model": "qhai-tts:爱丽丝", # 使用的声音模型
"input": "这是一段测试文本" # 需要转换的文本
})
# 构建请求头 headers
headers = {
'Authorization': '你的key', # !!!请替换为你的真实 API Key !!!
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json',
'Accept': '\*/\*', # 通常期望接收音频数据
'Host': 'api.qhaigc.net',
'Connection': 'keep-alive'
}
try:
# 发送 POST 请求
conn.request("POST", "/v1/audio/speech", payload, headers)
# 获取响应
res = conn.getresponse()
# 读取响应数据 (这部分通常是二进制的音频数据)
data = res.read()
print(f"Status: {res.status} {res.reason}")
# 注意:直接 decode("utf-8") 可能不适用于二进制音频数据
# print(data.decode("utf-8")) # 打印原始响应可能不是预期行为
# 处理响应数据 (例如,保存为音频文件)
if res.status == 200 and res.getheader('Content-Type', '').startswith('audio/'):
# 假设返回的是 mp3 文件
filename = "output\_audio.mp3"
with open(filename, "wb") as f:
f.write(data)
print(f"Audio saved to {filename}")
else:
# 如果不是成功或不是音频,尝试解码打印看是否为错误信息
try:
print("Response Body (possible error message):")
print(data.decode("utf-8"))
except UnicodeDecodeError:
print("Response Body is likely binary or non-UTF8 encoded.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# 关闭连接
conn.close()
响应说明
* **成功时:** API 的响应通常是**原始的音频数据流**(mpeg)。响应头中的 `Content-Type` 会指明音频的格式 (如 `audio/mpeg`, `audio/wav`)。您需要将响应体 (`data`) 直接保存为相应的音频文件,而不是尝试用 `decode("utf-8")` 解码。
* **失败时:** API 可能返回一个 JSON 对象,其中包含错误信息。这种情况下 `data.decode("utf-8")` 可能会成功并显示错误详情。
注意事项
* 请务必将 `'你的key'` 替换为您自己的有效 API Key。
* 确保请求体是合法的 JSON 格式。
* 根据需要选择合适的 `model` (声音)。
* 正确处理响应:如果是音频数据,请将其**二进制保存**到文件;如果是错误信息,可以尝试解码查看。