python 调用百度智能写诗

一、首先你要有个百度只能云账号 注册链接

二、在  产品服务 --> 人工智能平台 --> 智能创作平台 创建应用拿到API Key 和Secret Key

三、代码如下

# -*- coding:utf-8 -*-
import requests,base64
APIKEY = 'xxxxxx'
SECRETKEY = 'xxxxxxxx'
def GetAccessToeken():
    token_host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'.format(
      ak=APIKEY, sk=SECRETKEY)
    header = {'Content-Type': 'application/json; charset=UTF-8'}
    response = requests.post(url=token_host, headers=header)
    content = response.json()
    access_token = content.get("access_token")
    return access_token


import json

request_url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/poem' # 智能写诗
access_token = GetAccessToeken()

print(access_token)
datamsg = {
	"text": "雨夜",
	"index": 1,
}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=json.dumps(datamsg), headers=headers)
if response:
    ans = response.json()
    print(ans)

四、写诗效果

                 雨夜

         独倚西窗听晚风

         梧桐落雨夜朦胧

         灯花瘦尽人憔悴

         寂寞秋声入梦中

©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页