用户设置
import wenxin_api
wenxin_api. ak = "your ak"
wenxin_api. sk = "your sk"
使用精调模型
Python SDK
模型训练成功后,您可以调用已训练好的模型。
首次调用已训练好模型,可能需要一段时间才能准备好处理请求。如果您的请求超时,很可能是因为您的模型仍在加载部署中。如果发生这种情况,请在几分钟后重试。
和直接调用方式相同,用户可以复用python sdk直接调用的代码,只需将模型替换为用户自己训练好的模型
from wenxin_api import Model
from wenxin_api. tasks. text_to_image_training import TextToImageTuned
text = "戴墨镜的猫[c]"
model_id = "your model_id"
model = Model. retrieve( model_id= model_id, api_type= TYPE_TEXT_TO_IMAGE)
result = TextToImageTuned. create( text= text,
style= "探索无限" ,
resolution= "1024*1024" ,
model= model)
print ( result)
{ 'imgUrls' : [ 'http://szwg-rp-nlpgpu03.szwg01.baidu.com:8010/younger/file/ERNIE-ViLG/22ee3b4215a3d750522a8af654ed9c7730' , 'http://szwg-rp-nlpgpu03.szwg01.baidu.com:8010/younger/file/ERNIE-ViLG/22ee3b4215a3d750522a8af654ed9c775q' , 'http://szwg-rp-nlpgpu03.szwg01.baidu.com:8010/younger/file/ERNIE-ViLG/22ee3b4215a3d750522a8af654ed9c77i4' , 'http://szwg-rp-nlpgpu03.szwg01.baidu.com:8010/younger/file/ERNIE-ViLG/22ee3b4215a3d750522a8af654ed9c77ex' ] }
HTTP请求
基本要求
1. 发送部署请求
curl -X POST -H 'Content-Type:application/json' -d '{ "cmd": 1002, "model_id":1}' https://wenxin.baidu.com/moduleApi/portal/api/rest/1.0/ernievilg/3.0/tuning?access_token= xx
请求字段
类型
描述
cmd
int
固定传1002
model_id
int
模型ID
access_token
string
用户token
返回字段
类型
描述
code
int
错误码
msg
string
错误信息
data
dict
结果对象
-taskId
int
任务ID
2. 查询模型部署状态
curl -X POST -H 'Content-Type:application/json' -d '{ "cmd": 1009, "model_id":1}' https://wenxin.baidu.com/moduleApi/portal/api/rest/1.0/ernievilg/3.0/tuning?access_token= xx
请求字段
类型
描述
cmd
int
指令码,固定传1009
model_id
int
模型ID
access_token
string
用户token
返回字段
类型
描述
code
int
错误码
msg
string
错误信息
data
string
结果对象
-model_state
int
模型状态,2 表示部署成功。可参考【状态码】
3. 发送预测请求
curl -X POST -H 'Content-Type:application/json' -d '{ "cmd": 1003, "text": "戴墨镜的猫[c]", "style": "探索无限", "resolution": "1024*1024", "model_id": 1}' 'https://wenxin.baidu.com/moduleApi/portal/api/rest/1.0/ernievilg/3.0/tuning?access_token=xx'
请求字段
类型
描述
cmd
int
指令码,固定传1003
text
string
文本
style
string
风格
resolution
string
分辨率
model_id
int
用户训练成功后的模型ID
access_token
string
用户token
返回字段
类型
描述
code
int
错误码
msg
string
错误信息
data
string
结果对象
-taskId
int
任务ID
4. 查询预测结果
curl -i -k 'https://wenxin.baidu.com/moduleApi/portal/api/rest/1.0/ernievilg/v1/getImg?access_token=${access_token} ' --data-urlencode 'taskId=${task_id} '
请求字段
类型
描述
cmd
int
指令码,固定传1003
taskId
int
任务ID
model_id
int
模型ID
返回字段
类型
描述
code
int
错误码
msg
string
错误信息
data
string
结果对象