@@ -14,6 +14,7 @@ def __init__(self, api_token, api_url):
1414 self .api_token = api_token
1515 # 初始化 HTTP 连接
1616 self .conn = http .client .HTTPSConnection (self .api_url )
17+ self .endpoint = "/kling/v1/images/generations"
1718 # 设置请求头
1819 self .headers = {
1920 'Authorization' : f'Bearer { self .api_token } ' ,
@@ -46,14 +47,18 @@ def _kling_generate_image(self, model_name, prompt, negative_prompt, output_form
4647 })
4748
4849 # 发送 POST 请求,提交图像生成任务
49- self .conn .request ("POST" , "/kling/v1/images/generations?=null" , payload , self .headers )
50+ self .conn .request ("POST" , self . endpoint , payload , self .headers )
5051 # 获取响应
5152 res = self .conn .getresponse ()
5253 # 读取响应内容并解析为 JSON
5354 json_data = json .loads (res .read ().decode ("utf-8" ))
5455 # print(json_data)
55- # 从 json 文件中返回任务 ID
56- return json_data ['data' ]['task_id' ]
56+ if 'code' in json_data and json_data ['code' ] == 0 :
57+ # 成功则返回提交的任务 id
58+ return json_data ['data' ]['task_id' ]
59+ else :
60+ # 失败则返回错误信息
61+ raise Exception (f"API调用失败:{ json_data ['message' ]} " )
5762
5863 def _query_kling_image_url (self , task_id ):
5964 """使用查询接口获取生成图像 url
@@ -64,10 +69,7 @@ def _query_kling_image_url(self, task_id):
6469 image_url: 图像 url
6570 """
6671 # 构建查询路径
67- action = "images"
68- action2 = "generations"
69-
70- query_path = f"/kling/v1/{ action } /{ action2 } /{ task_id } "
72+ query_path = f"{ self .endpoint } /{ task_id } "
7173
7274 # 发送 GET 请求,查询图像生成任务状态
7375 self .conn .request ("GET" , query_path , None , self .headers )
@@ -125,7 +127,7 @@ def generate_image(self, model_name, prompt, negative_prompt="", output_format="
125127
126128 # 生成图像
127129 image_url = kling_text_to_image .generate_image (
128- model_name = "kling-v1-5" , # [必选]模型名称 可选择 kling-v1-5 或 kling-v1
130+ model_name = "kling-v1-5" , # [必选]模型名称 可选择 kling-v1-5 或 kling-v1 或 kling-v2
129131 prompt = "生成一张袋鼠的照片,手里拿着一个写着'DMXAPI'的牌子" , # [必选]文本提示词
130132 # negative_prompt="", # 负向文本提示词
131133 # output_format="png", # 输出格式:png 或 jpg
0 commit comments