Files
sora_back/src/main/java/com/mixi/service/PythonService.java

629 lines
31 KiB
Java
Raw Normal View History

2024-04-02 15:46:31 +08:00
package com.mixi.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
2024-04-25 02:57:05 +08:00
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
2024-04-02 15:46:31 +08:00
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mixi.common.config.FileProperties;
import com.mixi.common.config.exception.BusinessException;
import com.mixi.common.utils.AccessLimitUtils;
2024-04-25 02:57:05 +08:00
import com.mixi.mapper.TProductMapper;
2024-08-28 16:47:33 +08:00
import com.mixi.mapper.entity.QeFilling;
2024-04-02 15:46:31 +08:00
import com.mixi.mapper.entity.TProduct;
2024-08-01 13:21:09 +08:00
import com.mixi.model.dto.AIRecommendDTO;
2024-04-02 15:46:31 +08:00
import com.mixi.model.dto.GenerateCollocationDataBaseDTO;
import com.mixi.model.dto.GenerateCollocationQueryDTO;
import com.mixi.model.dto.GenerateCollocationQueryNewDTO;
import com.mixi.model.vo.AppProductColorExtractVO;
import com.mixi.model.vo.GenerateCollocationOutVO;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Slf4j
@Service
public class PythonService {
@Resource
private FileProperties fileProperties;
@Value("${access.python.ip:''}")
private String accessPythonIp;
/**
* 识别对应的属性标签值
*
* @param pictureUrls
* @param ids
* @return
*/
2024-04-08 17:02:53 +08:00
public JSONObject attributeRecognition(List<String> pictureUrls,List<String> ids, List<String> category) {
2024-04-02 15:46:31 +08:00
//限流校验
AccessLimitUtils.validate("attributeRecognition", 20);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, List<String>> content = Maps.newHashMap();
//识别图片路径数组
content.put("upload_img_path", pictureUrls);
//识别图片id数组
content.put("upload_img_id", ids);
2024-04-08 17:02:53 +08:00
content.put("upload_img_category", category);
2024-04-02 15:46:31 +08:00
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":9993/api/attribute")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("识别python对应的属性标签值请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService###attributeRecognition异常##{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识别python对应的属性标签值结果###{}",bodyStr.trim());
//去除限流
AccessLimitUtils.validateOut("attributeRecognition");
if (Objects.isNull(response)) {
log.error("PythonService##attributeRecognition异常###{}", "response or body is empty!");
throw new BusinessException("attribute recognition exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
JSONObject attributeJSONObject = JSON.parseObject(bodyStr.trim());
return attributeJSONObject;
}
log.info("识别python对应的属性标签值异常###{}", jsonObject);
//生成失败
throw new BusinessException("Atribute recognition exception!");
}
/**
* 商品推荐搭配
*
* @param query
* @param dataBaseMap
* @return
*/
public List<GenerateCollocationOutVO> generateCollocation(
List<GenerateCollocationQueryDTO> query, Map<String, GenerateCollocationDataBaseDTO> dataBaseMap) {
//限流校验
AccessLimitUtils.validate("generateCollocation", 20);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
//范围范围越大速度越慢。现在调试阶段可以设为10也就是从10套中选最好的5套。
content.put("max_outfits", 5);
content.put("is_best", Boolean.TRUE);
//输出得分最高的时装的套数topk < range
content.put("topk", 1);
//需要搭配的衣服,数组
//去重
List<GenerateCollocationQueryDTO> queryAfter = CollectionUtil.emptyIfNull(query).stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(GenerateCollocationQueryDTO::getItem_id))),ArrayList::new));
content.put("query", queryAfter);
//java数据库中存在的商品
content.put("database", dataBaseMap);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":10201/mixi/api/v1.0/generate")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("获取python推荐搭配请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##attributeRecognition异常###{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识获取python推荐搭配请求结果###{}", bodyStr.trim());
//去除限流
AccessLimitUtils.validateOut("generateCollocation");
if (Objects.isNull(response)) {
log.error("PythonService##generateCollocation异常###{}", "response or body is empty!");
throw new BusinessException("Generate Collocation exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
List<GenerateCollocationOutVO> collocationOutList = JSON.parseArray(bodyStr.trim(),GenerateCollocationOutVO.class);
return collocationOutList;
}
log.info("获取python推荐搭配请求异常###{}", jsonObject);
//生成失败
throw new BusinessException("Generate Collocation exception!");
}
public List<GenerateCollocationOutVO> generateCollocationNew(
List<GenerateCollocationQueryNewDTO> query, List<GenerateCollocationQueryNewDTO> dataBaseMap) {
//限流校验
AccessLimitUtils.validate("generateCollocation", 20);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
//范围范围越大速度越慢。现在调试阶段可以设为10也就是从10套中选最好的5套。
2024-04-03 11:36:52 +08:00
content.put("max_outfits", 10);
2024-04-02 15:46:31 +08:00
content.put("is_best", Boolean.TRUE);
//输出得分最高的时装的套数topk < range
2024-04-03 11:36:52 +08:00
content.put("topk", 5);
2024-04-02 15:46:31 +08:00
//需要搭配的衣服,数组
//去重
// List<GenerateCollocationQueryDTO> queryAfter = CollectionUtil.emptyIfNull(query).stream().collect(Collectors.collectingAndThen(
// Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(GenerateCollocationQueryDTO::getItem_id))),ArrayList::new));
content.put("query", query);
//java数据库中存在的商品
content.put("database", dataBaseMap);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":9993/api/outfit_matcheroutfit_matcher")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("获取python推荐搭配请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##attributeRecognition异常###{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识获取python推荐搭配请求结果###{}", bodyStr.trim());
//去除限流
AccessLimitUtils.validateOut("generateCollocation");
if (Objects.isNull(response)) {
log.error("PythonService##generateCollocation异常###{}", "response or body is empty!");
throw new BusinessException("Generate Collocation exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
System.out.println(bodyStr.trim());
// JSONObject jsonObject1 = JSONObject.parseObject(bodyStr.trim());
List<GenerateCollocationOutVO> collocationOutList = processJson(bodyStr.trim());
return collocationOutList;
}
log.info("获取python推荐搭配请求异常###{}", jsonObject);
//生成失败
throw new BusinessException("Generate Collocation exception!");
}
public static List<GenerateCollocationOutVO> processJson(String json) {
List<GenerateCollocationOutVO> resultList = new ArrayList<>();
JSONObject jsonObject = JSON.parseObject(json);
if (jsonObject.containsKey("data")) {
JSONArray dataArray = jsonObject.getJSONArray("data");
for (int i = 0; i < dataArray.size(); i++) {
JSONObject dataObject = dataArray.getJSONObject(i);
if (dataObject.containsKey("outfits") && dataObject.containsKey("scores")) {
JSONArray outfitsArray = dataObject.getJSONArray("outfits");
JSONArray scoresArray = dataObject.getJSONArray("scores");
List<List<GenerateCollocationQueryNewDTO>> outfitsList = new ArrayList<>();
for (int j = 0; j < outfitsArray.size(); j++) {
JSONArray innerArray = outfitsArray.getJSONArray(j);
List<GenerateCollocationQueryNewDTO> innerList = new ArrayList<>();
for (int k = 0; k < innerArray.size(); k++) {
JSONObject itemObject = innerArray.getJSONObject(k);
GenerateCollocationQueryNewDTO item = new GenerateCollocationQueryNewDTO();
item.setItem_name(itemObject.getString("item_name"));
item.setSemantic_category(itemObject.getString("semantic_category"));
item.setImage_path(itemObject.getString("image_path"));
innerList.add(item);
}
outfitsList.add(innerList);
}
List<Double> scoresList = new ArrayList<>();
for (int j = 0; j < scoresArray.size(); j++) {
scoresList.add(scoresArray.getDouble(j));
}
GenerateCollocationOutVO vo = new GenerateCollocationOutVO();
vo.setOutfits(outfitsList);
vo.setScores(scoresList);
resultList.add(vo);
}
}
}
return resultList;
}
/**
* 颜色或者相似度搭配,或者主色提取(同一个接口)
*
* @param query
* @param dataBaseMap
* @return
*/
public Map<String,List<String>> similarityRecognition(String mode, List<GenerateCollocationQueryDTO> query
, Map<String, GenerateCollocationDataBaseDTO> dataBaseMap,List<AppProductColorExtractVO> productRgbList) {
//限流校验
AccessLimitUtils.validate("similarityRecognition", 20);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
//color_feature和hash_feature。分别表示进行颜色匹配和相似度匹配。
content.put("mode", mode);
//颜色识别
if("color_feature".equals(mode)){
List<List<Integer>> rgbList = productRgbList.stream()
.map(rgb-> Arrays.asList(Integer.valueOf(rgb.getR()),
Integer.valueOf(rgb.getG()),Integer.valueOf(rgb.getB()))).collect(Collectors.toList());
List<Map<String,List<List<Integer>>>> rgbArgList = Lists.newArrayList();
Map<String,List<List<Integer>>> map = Maps.newHashMap();
map.put("RGB",rgbList);
rgbArgList.add(map);
//相似度识别
content.put("query", rgbArgList);
}else{
//去重需要搭配的衣服,数组
List<GenerateCollocationQueryDTO> queryAfter = CollectionUtil.emptyIfNull(query).stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(GenerateCollocationQueryDTO::getItem_id))),ArrayList::new));
content.put("query", queryAfter);
}
//输出匹配得分最高的图片topk < database,暂定5
content.put("topk", 5);
//java数据库中存在的商品
content.put("database", dataBaseMap);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":10202/mixi/api/v1.0/retrieval")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("获取python颜色或者相似度搭配请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##similarityRecognition异常###{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识获取python颜色或者相似度搭配请求结果###{}", bodyStr.trim());
//去除限流
AccessLimitUtils.validateOut("similarityRecognition");
if (Objects.isNull(response)) {
log.error("PythonService##similarityRecognition异常###{}", "response or body is empty!");
throw new BusinessException("Generate Collocation exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
JSONArray jsonArray = JSON.parseArray(bodyStr.trim());
return resolveMap(jsonArray,mode);
}
log.info("获取python颜色或者相似度搭配请求异常###{}", jsonObject);
//生成失败
throw new BusinessException("Similarity Recognition Exception!");
}
private static Map<String,List<String>> resolveMap(JSONArray jsonArray,String mode ){
Map<String,List<String>> returnMap = Maps.newHashMap();
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String queryId ="RGB";
if("hash_feature".equals(mode)){
queryId = jsonObject.getJSONObject("query").getString("item_id");
}
String retrievalJSONString = jsonObject.getJSONObject("retrieval").toJSONString();
Map<String,Map<String,Object>> resultValue = JSON.parseObject(retrievalJSONString,Map.class);
List<String> collationProductIdList = Lists.newArrayList(resultValue.keySet());
returnMap.put(queryId,collationProductIdList);
}
return returnMap;
}
/**
* 主色提取(同一个接口)
*
* @param mode
* @param path
* @param isHsv 1返回hsv值 0返回rgb值
* @return
*/
public List<List<String>> colorExtract(String mode,String path,Integer isHsv) {
//限流校验
AccessLimitUtils.validate("colorExtract", 20);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
//color_feature和hash_feature。分别表示进行颜色匹配和相似度匹配。
content.put("mode", mode);
//表示提取主色的数量最小2最大10暂定5
content.put("color_num", 5);
content.put("return_hsv", isHsv);
//衣服的绝对路径或urlhttp开头
content.put("path",path);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":10202/mixi/api/v1.0/retrieval")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("获取python主色提取请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##colorExtract异常###{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识获取python主色提取请求结果###{}", bodyStr.trim());
//去除限流
AccessLimitUtils.validateOut("colorExtract");
if (Objects.isNull(response)) {
log.error("PythonService##colorExtract异常###{}", "response or body is empty!");
throw new BusinessException("Color Extract exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
JSONArray jsonArray = JSON.parseArray(bodyStr.trim());
return resolveColorExtractList(jsonArray);
}
log.info("获取python主色提取请求异常###{}", jsonObject);
//生成失败
throw new BusinessException("Color Extract Exception!");
}
private static List<List<String> > resolveColorExtractList(JSONArray jsonArray ){
List<List<String>> returnList = Lists.newArrayList();
List<String> innerList= Lists.newArrayList();
for (int i = 0; i < jsonArray.size(); i++) {
String rgb = jsonArray.getString(i);
innerList.add(rgb);
}
returnList.add(innerList);
return returnList;
}
/**
*
* rgb转hsv
*
* @param r
* @param g
* @param b
* @return
*/
public List<Integer> rgb2hsv(Integer r ,Integer g,Integer b) {
//限流校验
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
//color_feature和hash_feature。分别表示进行颜色匹配和相似度匹配。
content.put("mode", "rgb2hsv");
//表示提取主色的数量最小2最大10暂定5
content.put("rgb", Arrays.asList(r,g,b));
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":10202/mixi/api/v1.0/retrieval")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("获取python rgb转hsv请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##rgb2hsv###{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识获取python主色 rgb转hsv请求结果###{}", bodyStr.trim());
if (Objects.isNull(response)) {
log.error("PythonService##rgb2hsv异常###{}", "response or body is empty!");
throw new BusinessException("rgb2hsv exception!");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
List<Integer> hsvList = JSON.parseArray(bodyStr.trim(),Integer.class);
return hsvList;
}
log.info("获取python rgb转hsv请求异常###{}", jsonObject);
//生成失败
throw new BusinessException("rgb2hsv Exception!");
}
public static void main(String[] args) {
// String res = "{\"1\":{\"Item\":\"top\",\"Top_length\":[\"Regular\"],\"Top_type\":[\"Sweater\"]},\"2\":{\"Item\":\"top\",\"Top_length\":[\"Regular\"],\"Top_type\":[\"Sweater\"]}}";
// Map<String,Map<String,Object>> resultValue = JSON.parseObject(res,Map.class);
// System.out.println(resultValue);
// String bodyStr = "{\n" +
// " \"mode\": \"color_extract\",\n" +
// " \"color_num\": 5,\n" +
// " \"path\": \"http://18.167.251.121:5568/download/202303/userFile/product/3/61b20df8-dabe-4eca-90a1-8dc804b1eddbrendering_113581965_source_99619122.jpg\"\n" +
// "}";
//
// List<GenerateCollocationOutVO> collocationOutList = JSON.parseArray(bodyStr.trim(),GenerateCollocationOutVO.class);
// System.out.println(collocationOutList);
//
// JSONArray jsonArray = JSON.parseArray(bodyStr.trim());
// System.out.println(resolveColorExtractList(jsonArray));
}
2024-04-25 02:57:05 +08:00
public List<TProduct> similarityMatch(String pictureUrl) {
//限流校验
AccessLimitUtils.validate("similarityMatch", 20);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
content.put("image_path", pictureUrl);
content.put("result_number", 5);
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":9993/api/similar_matchsimilar_match")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
log.info("获取python获取相似商品请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##similarityMatch异常###{}", ExceptionUtil.getThrowableList(ioException));
}
log.info("识获取python获取相似商品请求结果###{}", bodyStr.trim());
//去除限流
AccessLimitUtils.validateOut("similarityMatch");
if (Objects.isNull(response)) {
log.error("PythonService##similarityMatch异常###{}", "response or body is empty!");
throw new BusinessException("SimilarityMatch exception.");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
JSONObject parseObject = JSON.parseObject(bodyStr.trim());
return similarityMatchToList(parseObject);
}
log.info("获取python获取相似商品请求异常###{}", jsonObject);
//生成失败
throw new BusinessException("SimilarityMatch exception.");
}
@Resource
private TProductMapper tProductMapper;
private List<TProduct> similarityMatchToList(JSONObject parseObject) {
List<TProduct> productList = new ArrayList<>();
JSONArray data = parseObject.getJSONArray("data");
for (int i = 0; i < data.size(); i++) {
JSONObject jsonObject = data.getJSONObject(i);
String imagePath = jsonObject.getString("image_path");
String itemName = jsonObject.getString("item_name");
QueryWrapper<TProduct> qw = new QueryWrapper<>();
qw.lambda().eq(TProduct::getPictureUrl, imagePath);
qw.lambda().eq(TProduct::getPictureName, itemName);
List<TProduct> productList1 = tProductMapper.selectList(qw);
if (CollectionUtil.isNotEmpty(productList1)) {
productList.add(productList1.get(0));
}
}
return productList;
}
2024-08-01 13:21:09 +08:00
2024-08-30 16:44:19 +08:00
public JSONArray getAIRecommend(QeFilling qeFilling) {
2024-08-01 13:21:09 +08:00
//限流校验
2024-08-26 16:36:44 +08:00
// AccessLimitUtils.validate("similarityMatch", 20);
2024-08-01 13:21:09 +08:00
OkHttpClient client = new OkHttpClient().newBuilder()
2024-08-26 16:36:44 +08:00
.connectTimeout(300, TimeUnit.SECONDS)
2024-08-01 13:21:09 +08:00
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(300, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(300, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
Map<String, Object> content = Maps.newHashMap();
2024-08-28 16:47:33 +08:00
content.put("user_id", qeFilling.getPhoneNum());
2024-08-30 16:44:19 +08:00
// content.put("type", qeFilling.getType());
// content.put("q1", qeFilling.getQ1());
// content.put("q2", qeFilling.getQ2());
// content.put("q3", qeFilling.getQ3());
// content.put("q4", qeFilling.getQ4());
// content.put("q5", qeFilling.getQ5());
// content.put("q6", qeFilling.getQ6());
// content.put("q7", qeFilling.getQ7());
// content.put("q8", qeFilling.getQ8());
2024-08-01 13:21:09 +08:00
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
// .url(accessPythonIp + ":9993/api/similar_matchsimilar_match")
2024-08-30 17:23:58 +08:00
.url("http://127.0.0.1:5001/chat")
2024-08-01 13:21:09 +08:00
.method("POST", body)
// .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyStr = null;
try {
2024-08-26 16:36:44 +08:00
log.info("获取python获取AI推荐请求入参content###{}", JSON.toJSONString(content));
2024-08-01 13:21:09 +08:00
response = client.newCall(request).execute();
bodyStr = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##similarityMatch异常###{}", ExceptionUtil.getThrowableList(ioException));
}
2024-08-26 16:36:44 +08:00
log.info("识获取python获取AI推荐请求结果###{}", bodyStr.trim());
2024-08-01 13:21:09 +08:00
//去除限流
AccessLimitUtils.validateOut("similarityMatch");
if (Objects.isNull(response)) {
log.error("PythonService##similarityMatch异常###{}", "response or body is empty!");
throw new BusinessException("SimilarityMatch exception.");
}
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
2024-08-30 16:44:19 +08:00
JSONArray parseArray = JSON.parseArray(bodyStr.trim());
return parseArray;
2024-08-01 13:21:09 +08:00
}
2024-08-26 16:36:44 +08:00
log.info("获取python获取AI推荐请求异常###{}", jsonObject);
2024-08-01 13:21:09 +08:00
//生成失败
2024-08-26 16:36:44 +08:00
throw new BusinessException("AI recommendation exception.");
2024-08-01 13:21:09 +08:00
}
2024-04-02 15:46:31 +08:00
}