一、白嫖平台
Cloudflare 很早就支持了 Worker 服务,使用这个 Worker 服务配合 CloudFlare 的 AI大模型 我们已经白嫖很久了,今天我们来聊一聊如何白嫖一个。
Cloudflare Worker 官方文档:developers.cloudflare.com/workers/
二、注册账号并创建服务
注册账号就没必要说了吧。
2.1 创建一个 Worker
首先在菜单找到 Workers 和 Pages
,然后点击创建一个 Worker,输入一个标识名称后直接部署。
2.2 编写代码
我们使用的是 Cloudflare 提供的 @cf/stabilityai/stable-diffusion-xl-base-1.0
这个模型,然后编写下面的代码来提供API服务:
export default {
error(err) {
return new Response(err);
},
async fetch(request, env) {
const model = '@cf/stabilityai/stable-diffusion-xl-base-1.0'
let json = ''
if (request.method === 'GET') {
return this.error('POST only!')
}
const reader = request.body.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
json += new TextDecoder().decode(value)
}
} finally {
reader.releaseLock()
}
// 解析请求的JSON
let requestBody = null
try {
requestBody = JSON.parse(json || '{}')
} catch (e) {
return this.error('Bad Json!')
}
const inputs = {
prompt: requestBody.prompt || 'a dog',
negative_prompt: requestBody.negative_prompt || '',
height: requestBody.height || 1024,
width: requestBody.width || 1024,
num_steps: requestBody.num_steps || 20,
strength: requestBody.strength || 1,
guidance: requestBody.guidance || 7.5,
seed: requestBody.seed || parseInt((Math.random() * 1024 * 1024).toString(), 10),
};
console.log("model", model)
console.log("inputs", inputs)
const response = await env.AI.run(
model,
inputs,
);
return new Response(response, {
headers: {
'content-type': 'image/png',
},
});
},
};
然后点击 部署 即可。
2.3 部署反向代理
因为我没有空闲的域名托管到 Cloudflare,然后默认提供的域名又很容易被拦截,所以我选择了用自己的香港服务器来反向代理这个服务。
Nginx 配置如下:
server
{
server_name tti.hamm.cn;
include force-https.conf;
include enable-https.conf;
autoindex on;
location / {
include cross-domain.conf;
include enable-php.conf;
index index.php index.html;
root /home/www/tti.hamm.cn/;
}
location /tti {
include enable-proxy.conf;
# 下面是 cloudflare 提供的免费子域名
proxy_pass http://test.hamm.workers.dev/;
}
}
2.4 简单写个前端页面
我们直接用 HTML 简单写了个页面,部署了起来,访问速度什么的都还挺不错。
当然,你脑子里想的那些图,这个模型肯定是不支持的。(我也不知道你脑子里想的什么图)。
接下来体验一下吧:
三. 更多玩法
你也可以基于 Cloudflare
提供的其他模型来搞更多好玩的东西,比如
- 部署一个
qwen1.5
来聊天 - 部署一个
distilbert-sst-2-int8
来做情感分析 - 部署一个
hermes-2-pro-mistral-7b
来做函数调用 - 等等等等
四、总结
白嫖的算力和服务,可以用来为自己的日常工作添砖加瓦,或者做点什么。
比如我们基于 qwen1.5
搭了一个 AI角色扮演
的站点:
相比本地搭建一些大模型,使用 cloudflare
提供的服务就能省下很多成本啦~
That's all.