wuyongjin/javaAI
0
1package com.bitservice.ai_txt2image;2import java.io.IOException;3import java.util.HashMap;4import java.util.Map;5 6import com.alibaba.fastjson.JSON;7import lombok.extern.slf4j.Slf4j;8import org.apache.http.Header;9import org.apache.http.HttpEntity;10import org.apache.http.HttpResponse;11import org.apache.http.client.ClientProtocolException;12import org.apache.http.client.ResponseHandler;13import org.apache.http.client.methods.CloseableHttpResponse;14import org.apache.http.client.methods.HttpGet;15import org.apache.http.client.methods.HttpPost;16import org.apache.http.entity.StringEntity;17import org.apache.http.impl.client.CloseableHttpClient;18import org.apache.http.impl.client.HttpClientBuilder;19import org.apache.http.util.EntityUtils;20import org.springframework.web.bind.annotation.PostMapping;21import org.springframework.web.bind.annotation.RequestBody;22import org.springframework.web.bind.annotation.RestController;23 24import com.alibaba.fastjson.JSONArray;25import com.alibaba.fastjson.JSONObject;26@RestController27@Slf4j28public class AI_txt2ImageController {29 @PostMapping("/txt2pic")30 Map txt2pic(@RequestBody Map<String, Object> map) {31 String result = "";32 String api_key = System.getenv("OPENAI_API_KEY");33 String api_base = System.getenv("OPENAI_API_BASE");34 String api_version = "2023-06-01-preview";35 String url = api_base + "openai/images/generations:submit?api-version=" + api_version;36 CloseableHttpClient httpclient = HttpClientBuilder.create().build();37 HttpPost httpPost = new HttpPost(url);38 ResponseHandler<String> handler = new ResponseHandler<String>() {39 @Override40 public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {41 // TODO Auto-generated method stub42 Header[] headers = response.getHeaders("operation-location");43 return headers[0].getValue();44 }45 };46 // 设置请求格式47 String jsonData = "{\"prompt\":\"" + map.get("prompt") + "\",\"size\":\"" + map.get("size") + "\",\"n\":1}";48 log.info("查询请求:"+jsonData);49 StringEntity entity = new StringEntity(jsonData, "utf-8");// 解决中文乱码问题50 entity.setContentEncoding("UTF-8");51 entity.setContentType("application/json");52 httpPost.setEntity(entity);53 httpPost.addHeader("api-key", api_key);54 httpPost.addHeader("Content-Type", "application/json");55 CloseableHttpResponse response = null;56 try {57 result = httpclient.execute(httpPost, handler);58 HttpGet httpget = new HttpGet(result);59 httpget.addHeader("api-key", api_key);60 httpget.addHeader("Content-Type", "application/json");61 String status = "";62 while (!status.equals( "succeeded")) {63 Thread.sleep(1000);64 response = httpclient.execute(httpget);65 HttpEntity he = response.getEntity();66 String ret=EntityUtils.toString(response.getEntity());67 JSONObject obj = JSONObject.parseObject(ret);68 status=obj.getString("status");69 log.info("当前状态:"+status);70 if (status .equals("succeeded")) {71 JSONArray js=obj.getJSONObject("result").getJSONArray("data");72 String images_url=js.getJSONObject(0).getString("url");73 log.info("获取到图片url:"+images_url);74 Map<String, Object> resultmap =new HashMap<>();75 resultmap.put("url",images_url);76 return resultmap;77// return resultmap;78 }79 }80 } catch (Exception e) {81 // TODO Auto-generated catch block82 e.printStackTrace();83 }84 return null;85 }86 87 @PostMapping("/test")88 String txt2pic(String prompt,String size) {89 String result = "";90 91 String api_key = System.getenv("OPENAI_API_KEY");92 String api_base = System.getenv("OPENAI_API_BASE");93 String api_version = "2023-06-01-preview";94 String url = api_base + "openai/images/generations:submit?api-version=" + api_version;95 CloseableHttpClient httpclient = HttpClientBuilder.create().build();96 HttpPost httpPost = new HttpPost(url);97 ResponseHandler<String> handler = new ResponseHandler<String>() {98 @Override99 public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {100 // TODO Auto-generated method stub101 Header[] headers = response.getHeaders("operation-location");102 return headers[0].getValue();103 }104 };105 // 设置请求格式106 String jsonData = "{\"prompt\":\"" + prompt + "\",\"size\":\"" +size + "\",\"n\":1}";107 log.info("查询请求:"+jsonData);108 StringEntity entity = new StringEntity(jsonData, "utf-8");// 解决中文乱码问题109 entity.setContentEncoding("UTF-8");110 entity.setContentType("application/json");111 httpPost.setEntity(entity);112 httpPost.addHeader("api-key", api_key);113 httpPost.addHeader("Content-Type", "application/json");114 CloseableHttpResponse response = null;115 try {116 result = httpclient.execute(httpPost, handler);117 HttpGet httpget = new HttpGet(result);118 httpget.addHeader("api-key", api_key);119 httpget.addHeader("Content-Type", "application/json");120 String status = "";121 while (!status.equals( "succeeded")) {122 Thread.sleep(1000);123 response = httpclient.execute(httpget);124 HttpEntity he = response.getEntity();125 String ret=EntityUtils.toString(response.getEntity());126 JSONObject obj = JSONObject.parseObject(ret);127 status=obj.getString("status");128 log.info("当前状态:"+status);129 if (status .equals("succeeded")) {130 JSONArray js=obj.getJSONObject("result").getJSONArray("data");131 String images_url=js.getJSONObject(0).getString("url");132 log.info("获取到图片url:"+images_url);133 return images_url;134 }135 }136 } catch (Exception e) {137 // TODO Auto-generated catch block138 e.printStackTrace();139 }140 return null;141 }142}