先说结果:不好说
不敢细看,过于骇人
首先还是先去申请,基础版即可
AI作画_文心AI作画-百度AI开放平台百度AI开放平台-文心AI作画,基于百度领先的中文跨模态生成模型,准确理解用户输入的自然语言,一键自动生成不限定风格的图像.https://ai.baidu.com/tech/creativity/ernie_Vilg拿到API_KEY 、SECRET_KEY ,打开Vscode 输入下方代码
Python :
import requests
import json
API_KEY = "" #你的API Key
SECRET_KEY = "" #你的Secret Key
def main():
url = "https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token=" + get_access_token()
payload = json.dumps({
"text": "高清",
"resolution": "1024*1024",
"style": "二次元",
"num": 1
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
main()
C#
using System;
using System.IO;
using RestSharp;//依赖版本106.15.0 https://www.nuget.org/packages/RestSharp/106.15.0
using Newtonsoft.Json; //https://www.nuget.org/packages/Newtonsoft.Json
namespace SampleApplication {
public class Sample {
const string API_KEY = "";
const string SECRET_KEY = "";
public static void Main(string[] args) {
var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token={GetAccessToken()}");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
var body = @"{""text"":""高清"",""resolution"":""1024*1024"",""style"":""二次元"",""num"":1}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
/**
* 使用 AK,SK 生成鉴权签名(Access Token)
* @return 鉴权签名信息(Access Token)
*/
static string GetAccessToken() {
var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", API_KEY);
request.AddParameter("client_secret", SECRET_KEY);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var result = JsonConvert.DeserializeObject(response.Content);
return result.access_token.ToString();
}
}
}
JAVA:
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
class Sample {
public static final String API_KEY = "";
public static final String SECRET_KEY = "";
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"text\":\"高清\",\"resolution\":\"1024*1024\",\"style\":\"二次元\",\"num\":1}");
Request request = new Request.Builder()
.url("https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token=" + getAccessToken())
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
System.out.println(response.body().string());
}
/**
* 从用户的AK,SK生成鉴权签名(Access Token)
*
* @return 鉴权签名(Access Token)
* @throws IOException IO异常
*/
static String getAccessToken() throws IOException {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
+ "&client_secret=" + SECRET_KEY);
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token")
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
return new JSONObject(response.body().string()).getString("access_token");
}
}
Run code, 即可(得到二刺猿小姐姐)