步骤一:领取讯飞文生图资源
步骤二:查看文档编写测试代码
下载库PIL库
pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
# encoding: UTF-8
import time
import requests
from datetime import datetime
from wsgiref.handlers import format_date_time
from time import mktime
import hashlib
import base64
import hmac
from urllib.parse import urlencode
import json
from PIL import Image
from io import BytesIO
class AssembleHeaderException(Exception):
def __init__(self, msg):
self.message = msg
class Url:
def __init__(this, host, path, schema):
this.host = host
this.path = path
this.schema = schema
pass
# calculate sha256 and encode to base64
def sha256base64(data):
sha256 = hashlib.sha256()
sha256.update(data)
digest = base64.b64encode(sha256.digest()).decode(encoding='utf-8')
return digest
def parse_url(requset_url):
stidx = requset_url.index("://")
host = requset_url[stidx + 3:]
schema = requset_url[:stidx + 3]
edidx = host.index("/")
if edidx <= 0:
raise AssembleHeaderException("invalid request url:" + requset_url)
path = host[edidx:]
host = host[:edidx]
u = Url(host, path, schema)
return u
# 生成鉴权url
def assemble_ws_auth_url(requset_url, method="GET", api_key="", api_secret=""):
u = parse_url(requset_url)
host = u.host
path = u.path
now = datetime.now()
date = format_date_time(mktime(now.timetuple()))
# print(date)
# date = "Thu, 12 Dec 2019 01:57:27 GMT"
signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(host, date, method, path)
# print(signature_origin)
signature_sha = hmac.new(api_secret.encode('utf-8'), signature_origin.encode('utf-8'),
digestmod=hashlib.sha256).digest()
signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
api_key, "hmac-sha256", "host date request-line", signature_sha)
authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
# print(authorization_origin)
values = {
"host": host,
"date": date,
"authorization": authorization
}
return requset_url + "?" + urlencode(values)
# 生成请求body体
def getBody(appid,text):
body= {
"header": {
"app_id": appid,
"uid":"123456789"
},
"parameter": {
"chat": {
"domain": "general",
"temperature":0.5,
"max_tokens":4096
}
},
"payload": {
"message":{
"text":[
{
"role":"user",
"content":text
}
]
}
}
}
return body
# 发起请求并返回结果
def main(text,appid,apikey,apisecret):
host = 'http://spark-api.cn-huabei-1.xf-yun.com/v2.1/tti'
url = assemble_ws_auth_url(host,method='POST',api_key=apikey,api_secret=apisecret)
content = getBody(appid,text)
print(time.time())
response = requests.post(url,json=content,headers={'content-type': "application/json"}).text
print(time.time())
return response
#将base64 的图片数据存在本地
def base64_to_image(base64_data, save_path):
# 解码base64数据
img_data = base64.b64decode(base64_data)
# 将解码后的数据转换为图片
img = Image.open(BytesIO(img_data))
# 保存图片到本地
img.save(save_path)
# 解析并保存到指定位置
def parser_Message(message):
data = json.loads(message)
# print("data" + str(message))
code = data['header']['code']
if code != 0:
print(f'请求错误: {code}, {data}')
else:
text = data["payload"]["choices"]["text"]
imageContent = text[0]
# if('image' == imageContent["content_type"]):
imageBase = imageContent["content"]
imageName = data['header']['sid']
savePath = f"D://{imageName}.jpg"
base64_to_image(imageBase,savePath)
print("图片保存路径:" + savePath)
if __name__ == '__main__':
#运行前请配置以下鉴权三要素,获取途径:https://console.xfyun.cn/services/tti
APPID ='229896621'
APISecret = 'NmMyNWU2ZDcyYjcwNTQzZTNlNzFjOTU31'
APIKEY = '8f1009f6d0fc7a15dee96c7c5244198c1'
desc = '''生成一张图:远处有着高山,山上覆盖着冰雪,近处有着一片湛蓝的湖泊'''
res = main(desc,appid=APPID,apikey=APIKEY,apisecret=APISecret)
print(res)
#保存到指定位置
parser_Message(res)
执行结果如下:
D盘根目录下生成图片如下
步骤三:创建sourceai/model/draw/xunfei/word2picture.py,代码如下
# encoding: UTF-8
import time
import requests
from datetime import datetime
from wsgiref.handlers import format_date_time
from time import mktime
import hashlib
import base64
import hmac
from urllib.parse import urlencode
import json
from PIL import Image
from io import BytesIO
APPID = '22989662'
APISecret = 'NmMyNWU2ZDcyYjcwNTQzZTNlNzFjOTU31'
APIKEY = '8f1009f6d0fc7a15dee96c7c5244198c1'
class AssembleHeaderException(Exception):
def __init__(self, msg):
self.message = msg
class Url:
def __init__(this, host, path, schema):
this.host = host
this.path = path
this.schema = schema
pass
# calculate sha256 and encode to base64
def sha256base64(data):
sha256 = hashlib.sha256()
sha256.update(data)
digest = base64.b64encode(sha256.digest()).decode(encoding='utf-8')
return digest
def parse_url(requset_url):
stidx = requset_url.index("://")
host = requset_url[stidx + 3:]
schema = requset_url[:stidx + 3]
edidx = host.index("/")
if edidx <= 0:
raise AssembleHeaderException("invalid request url:" + requset_url)
path = host[edidx:]
host = host[:edidx]
u = Url(host, path, schema)
return u
# 生成鉴权url
def assemble_ws_auth_url(requset_url, method="GET", api_key="", api_secret=""):
u = parse_url(requset_url)
host = u.host
path = u.path
now = datetime.now()
date = format_date_time(mktime(now.timetuple()))
# print(date)
# date = "Thu, 12 Dec 2019 01:57:27 GMT"
signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(host, date, method, path)
# print(signature_origin)
signature_sha = hmac.new(api_secret.encode('utf-8'), signature_origin.encode('utf-8'),
digestmod=hashlib.sha256).digest()
signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
api_key, "hmac-sha256", "host date request-line", signature_sha)
authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
# print(authorization_origin)
values = {
"host": host,
"date": date,
"authorization": authorization
}
return requset_url + "?" + urlencode(values)
# 生成请求body体
def getBody(appid,text):
body= {
"header": {
"app_id": appid,
"uid":"123456789"
},
"parameter": {
"chat": {
"domain": "general",
"temperature":0.5,
"max_tokens":4096
}
},
"payload": {
"message":{
"text":[
{
"role":"user",
"content":text
}
]
}
}
}
return body
# 发起请求并返回结果
def txt2pic(text):
host = 'http://spark-api.cn-huabei-1.xf-yun.com/v2.1/tti'
url = assemble_ws_auth_url(host,method='POST',api_key=APIKEY,api_secret=APISecret)
content = getBody(APPID,text)
print(time.time())
response = requests.post(url,json=content,headers={'content-type': "application/json"}).text
print(time.time())
return response
def start(desc,folder):
res = txt2pic(desc)
return parser_Message(res,folder)
#将base64 的图片数据存在本地
def base64_to_image(base64_data, save_path):
# 解码base64数据
img_data = base64.b64decode(base64_data)
# 将解码后的数据转换为图片
img = Image.open(BytesIO(img_data))
# 保存图片到本地
img.save(save_path)
# 解析并保存到指定位置
def parser_Message(message,folder):
data = json.loads(message)
# print("data" + str(message))
code = data['header']['code']
if code != 0:
print(f'请求错误: {code}, {data}')
return ''
else:
text = data["payload"]["choices"]["text"]
imageContent = text[0]
# if('image' == imageContent["content_type"]):
imageBase = imageContent["content"]
imageName = data['header']['sid']
savePath = f"{folder}//{imageName}.jpg"
base64_to_image(imageBase,savePath)
print("图片保存路径:" + savePath)
return savePath
if __name__ == '__main__':
#运行前请配置以下鉴权三要素,获取途径:https://console.xfyun.cn/services/tti
desc = '''生成一张图:远处有着高山,山上覆盖着冰雪,近处有着一片湛蓝的湖泊'''
folder = r'D:'
res = start(desc,folder)
print(res)
注意:实际使用场景需要替换成自己的APPID、APISecret、APIKEY
步骤四:创建sourceai/controller/draw_view.py文件,代码如下:
from django.shortcuts import render
from django.http import HttpResponse
import json
from sourceai.model.draw.xunfei import word2picture
# Create your views here.
def sparkdraw_index(request):
return render(request, 'draw/sparkdraw.html')
def sparkdraw_pic(request):
product = request.POST.get('context')
print('内容为:', product)
result = ''
code = 0
try:
if product.strip() != '':
result = word2picture.start(product.strip(), 'static/images')
print(result)
except Exception as e:
result = 'static/img/nopic.jpg'
code = -1
return HttpResponse(json.dumps({"code": code, "res": result}))
步骤五:在soft863ai/urls.py中新增如下内容:
from sourceai.controller import draw_view
path('sparkdrawindex', draw_view.sparkdraw_index),
path('sparkdrawto', draw_view.sparkdraw_pic),
步骤六:创建templates/draw/sparkdraw.html,代码如下:
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="icon" href="/static/img/touxiang.png"> <!-- 网页图标 -->
<link rel="stylesheet" type="text/css" href="/static/css/index.css"/>
<link rel="stylesheet" href="/static/css/index.css">
<link rel="stylesheet" href="/static/css/animate.css">
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script src="/static/js/typeit.min.js"></script>
<script src="/static/js/rem.js" type="text/javascript" charset="utf-8"></script>
</head>
<body style="cursor: url(img/body2.png);">
<div class="canvas" style="opacity: .2">
<iframe frameborder="0" src="/static/js/index.html" style="width: 100%; height: 100%"></iframe>
</div>
<div id="app">
<div class="header_title">AI绘图系统</div>
<div class="header_time"></div>
<!-- //左侧显示信息 -->
<div class="side_left">
<textarea id="context1"
style="width: 4.2rem;min-height: 5rem;background: rgba(255, 255, 255, 0);color:#3cf7f1;border:0;outline: none;margin-top: 1rem;margin-left:0.23rem;display: inline-block;">
</textarea>
</div>
<div class="face-capture" id="face-capture">
<div class="faceCon">
<div class="videoFaceCon">
<div class="title">点击绘制</div>
<div class="imgCoverCon">
<div class="retiveCon">
<img src="/static/img/1233.png" class="retiveConImg allRoteAnmi">
<img src="/static/img/fanyi.png" class="videoFace">
</div>
</div>
</div>
</div>
</div>
<div class="side_right">
<div class="rightCon">
<div>
<img class="textConImg"
src=""
alt="" width="400px">
</div>
</div>
</div>
<script type="text/javascript">
window.onload = function () {
$('.side_left').show();
$('.side_right').show();
// 时间
var timer = '';
var taker = '';
timer && clearInterval(_this.timer);
timer = setInterval(function () {
var show_day = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
var nowtime = new Date();
var year = nowtime.getFullYear();
var month = nowtime.getMonth() + 1;
var date = nowtime.getDate();
var day = nowtime.getDay();
var hour = nowtime.getHours();
var minutes = nowtime.getMinutes();
var second = nowtime.getSeconds();
month < 10 ? month = '0' + month : month;
hour < 10 ? hour = '0' + hour : hour;
minutes < 10 ? minutes = '0' + minutes : minutes;
second < 10 ? second = '0' + second : second;
var show_day_s = day === 0 ? show_day[6] : show_day[day - 1];
// _this.mytime = year + "年" + month + "月" + date + "日 " + show_day_s+ nowtime.toLocaleTimeString('chinese', { hour12: false });
var mytimer = year + "年" + month + "月" + date + "日" + show_day_s + ' ' +
hour + ':' + minutes;
$('.header_time').html(mytimer)
}, 1000);
//时间
function goanimation() {
taker = '';
taker = new TypeIt('.rightCon', {
lifeLike: true,
cursorSpeed: 100,
waitUntilVisible: false,
speed: 100
}).go();
}
$('.videoFaceCon').click(() => {
log = $("#context1").val()
$('.title').html('正在绘制..');
//创建一个存储表单数据的对象
var fd = new FormData();
fd.append("context", log);
fd.append("csrfmiddlewaretoken", '{{ csrf_token }}')
$.ajax({
url: '/sparkdrawto',
type: 'post',
data: fd,
dataType: 'json',
cache: false,
processData: false,
contentType: false,
success: function (obj) {
console.log(obj)
console.log(obj.res)
if (obj.code == 0) {
$('.textConImg').attr("src", obj.res)
$('.title').html('恭喜您,绘制成功!')
$('.rightCon').show()
$('.rightCon').addClass('fadeInRightBig');
} else {
$('.textConImg').attr("src", obj.res)
$('.title').html('绘制失败!')
$('.rightCon').show()
$('.rightCon').addClass('fadeInRightBig');
}
{#goanimation();#}
{#taker.reset();#}
}
})
})
}
</script>
</body>
</html>
与智能问答前端对比
<div class="rightCon"> <div> <img class="textConImg" src="" width="400px"> </div>
$('.textConImg').attr("src",obj.res)
if (obj.code == 0) { $('.title').html('恭喜您,创作成功!') }else { $('.title').html('创作失败!') }
步骤七:运行并测试