generateSketch 功能

This commit is contained in:
徐佩
2023-08-17 11:59:19 +08:00
parent 11e9ff4e2c
commit 5b41b51859
13 changed files with 485 additions and 1 deletions

View File

@@ -1390,4 +1390,91 @@ public class PythonService {
originRatioList.set(1, originRatioList.get(1).multiply(BigDecimal.valueOf(high)));
return originRatioList;
}
public String generateSketchCaption(String url) {
//限流校验
AccessLimitUtils.validate("generateSketchCaption",5);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, url);
Request request = new Request.Builder()
.url(accessPythonIp+":2828/aida/interrogator")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("generateSketchCaption请求入参content###{}", url);
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("generateSketchCaption异常###{}", ExceptionUtil.getThrowableList(ioException));
}
//去除限流
AccessLimitUtils.validateOut("generateSketchCaption");
if (Objects.isNull(response)) {
log.error("generateSketchCaption异常###{}", "response or body is empty!");
throw new BusinessException("system error!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
return bodyStr;
}
log.info("attribute_retrieval失败###{}", bodyStr);
//生成失败
throw new BusinessException("system error!");
}
public String generateSketch(String url,String text) {
//限流校验
AccessLimitUtils.validate("generateSketch",5);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, String> content = Maps.newHashMap();
content.put("img_url", url);
content.put("input", text);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":2828/aida/diffusion")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyString = null;
try {
log.info("generateSketch请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyString = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##generateSketch异常###{}", ExceptionUtil.getThrowableList(ioException));
}
//去除限流
AccessLimitUtils.validateOut("generateSketch");
if (Objects.isNull(response)) {
log.error("PythonService##generateSketch异常###{}", "response or body is empty!");
throw new BusinessException("generate sketch exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
return bodyString;
}
log.info("generate sketch失败###{}", jsonObject);
//生成失败
throw new BusinessException("generate sketch exception!");
}
}