通过用户输入文字来生成图片,
天启开放平台:多模态API接口文档-以文生图,输入文字生成图片的方式,多模态api
https://tianqi.aminer.cn/open/document/mm_ref/cogview
和midijouney类似的ai绘画平台。
流程图:
api 调用平台代码:
# encoding:utf-8
import requests
import json
import time
'''
cogview
天启开放平台:多模态API接口文档-以文生图
https://tianqi.aminer.cn/open/document/mm_ref/cogview
'''
API_KEY = "" # 从控制台获取
API_SECRET = "" # 从控制台获取
QUERY = "" # 图片主题
STYLE = "" # 图片风格
request_url = "https://pretrain.aminer.cn/api/v2/"
api = 'cogview'
# 指定请求参数格式为json
headers = {'Content-Type': 'application/json'}
request_url = request_url + api
data = {
"query": QUERY,
"style": STYLE,
"apikey": API_KEY,
"apisecret": API_SECRET
}
response = requests.post(request_url, headers=headers, data=json.dumps(data))
if response:
print (response.json())
# 返回的response里有task_id,用task_id去请求"https://pretrain.aminer.cn/api/v2/status"接口
'''
请求status接口返回api调用结果
'''
task_id = response.json()["result"]["task_id"] # 从之前请求api的结果中获取
request_url = 'https://pretrain.aminer.cn/api/v2/status?task_id=' + task_id
response = requests.get(request_url)
if response:
while "output" not in response.json()["result"]:
time.sleep(10)
response = requests.get(request_url)
print(response.json())