Commit 5f5c5e2f by 苗权威

09.21

parent 1b75903b
...@@ -515,9 +515,11 @@ public class ApachHttpUtils { ...@@ -515,9 +515,11 @@ public class ApachHttpUtils {
if (status == 200) { if (status == 200) {
HttpEntity httpEntity = response.getEntity(); HttpEntity httpEntity = response.getEntity();
if(httpEntity != null){ if(httpEntity != null){
String result = EntityUtils.toString(httpEntity, "UTF-8"); byte[] bytes = EntityUtils.toByteArray(httpEntity);
// String result = EntityUtils.toString(httpEntity, "UTF-8");
EntityUtils.consume(httpEntity);//关闭资源 EntityUtils.consume(httpEntity);//关闭资源
return result; // return result;
return String.valueOf(bytes.length);
} }
} }
} }
......
...@@ -29,14 +29,7 @@ import org.apache.commons.lang3.StringUtils; ...@@ -29,14 +29,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
...@@ -59,6 +52,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -59,6 +52,7 @@ import lombok.extern.slf4j.Slf4j;
* painter * painter
* 20-6-11 下午7:57 * 20-6-11 下午7:57
*/ */
@CrossOrigin
@RestController @RestController
@RequestMapping("/upload") @RequestMapping("/upload")
@Slf4j @Slf4j
......
package ai.qiwu.com.tts_service.controller; package ai.qiwu.com.tts_service.controller;
import ai.qiwu.com.tts_service.common.ApachHttpUtils;
import ai.qiwu.com.tts_service.common.FileUtil; import ai.qiwu.com.tts_service.common.FileUtil;
import ai.qiwu.com.tts_service.common.ServerResponse; import ai.qiwu.com.tts_service.common.ServerResponse;
import ai.qiwu.com.tts_service.common.TTSUtils; import ai.qiwu.com.tts_service.common.TTSUtils;
import ai.qiwu.com.tts_service.pojo.*; import ai.qiwu.com.tts_service.pojo.*;
import ai.qiwu.com.tts_service.pojo.upload.UploadOneSenData; import ai.qiwu.com.tts_service.pojo.upload.UploadOneSenData;
import ai.qiwu.com.tts_service.service.*; import ai.qiwu.com.tts_service.service.*;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter; import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -25,6 +29,7 @@ import java.io.*; ...@@ -25,6 +29,7 @@ import java.io.*;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -413,16 +418,109 @@ public class TTSController { ...@@ -413,16 +418,109 @@ public class TTSController {
// return cdnService.getDirPugeState(botAccount); // return cdnService.getDirPugeState(botAccount);
// } // }
private static void buildTTSTime(String sentence, String noCdnBytes, Long noCdnTime, String cdnOnceBytes,
Long cdnOnceTime, String cdnBytes, Long cdnTime) {
synchronized(TTServiceEdition2.class) {
CSVFormat format;
if (Files.exists(Paths.get("/home/yf01/下载/audioCut/timeAccount/cdnTest.csv"))) format = CSVFormat.DEFAULT;
else format = CSVFormat.DEFAULT.withHeader("sentence", "noCdnBytes", "noCdnTime", "cdnOnceBytes", "cdnOnceTime", "cdnBytes", "cdnTime");
try (CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(
new FileOutputStream("/home/yf01/下载/audioCut/timeAccount/cdnTest.csv", true), Charset.forName("UTF-8")),format)){
csvPrinter.printRecord(sentence, noCdnBytes, noCdnTime, cdnOnceBytes, cdnOnceTime, cdnBytes, cdnTime);
} catch (IOException e) {
log.error("buildTTSTime:"+e);
}
}
}
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String text = "今天是第"; // String[] arr = {"为咱们人类的共同命运。", "我们应该要展现大国的担当。","还有我们中国同各国一道重温使命。","当然要共同构建人类命运共同体。",
String text2 = "6"; // "这仅仅是他们的交流方式。","当然这些对他们的自由权起到限制。", "除此之外美国宣扬所谓国家安全。","这就会意味着案件会呈递联邦法庭。", "还有这项裁决很可能会继续有效。",
String text3 = "天,知道吗?,"; // "当然这些在美国什么不能发生。", "推测很可能美国又出台新禁令。", "美国的最高法院的大法官是谁。", "特朗普总统又开始挑剔施压。", "尊重市场经济和公平竞争的原则。", "外国企业在美国投资经营。"};
String text4 = "知道"; // String[] seq = {"所以你们要买什么\uD83C\uDD70蔬菜", "所以你们要买什么\uD83C\uDD70水果", "所以你们要买什么\uD83C\uDD70肉类",
// "所以你们要买什么\uD83C\uDD70干货", "所以你们要买什么\uD83C\uDD70鱼类", "所以你们要买什么\uD83C\uDD70水产品",
System.out.println(TTSUtils.buildMP3Filename(text)); // "所以你们要买什么\uD83C\uDD70瓜果蔬菜", "所以你们要买什么\uD83C\uDD70东西"};
System.out.println(TTSUtils.buildMP3Filename(text2)); // String[] seqPost = {"呢\uD83C\uDD71","哦\uD83C\uDD71","吧\uD83C\uDD71","嘿\uD83C\uDD71","呵\uD83C\uDD71",
System.out.println(TTSUtils.buildMP3Filename(text3)); // "吒\uD83C\uDD71","哼\uD83C\uDD71","哈\uD83C\uDD71","吼\uD83C\uDD71","嘀\uD83C\uDD71","兹\uD83C\uDD71"};
System.out.println(TTSUtils.buildMP3Filename(text4)); // List<String> list = new ArrayList<>();
// for (int i = 0; i < 11; i++) {
// for (int j = 0; j < 8; j++) {
// list.add(arr[i]+seq[j]+seqPost[i]);
// }
// }
// list = Collections.singletonList("在美国句子重复会更快吗,你说呢有可能吗谁知道呢");
// Map<String, String> header = Collections.singletonMap("Content-Type", "application/json");
// for (String str: list) {
// TTSLSRequest request = new TTSLSRequest();
// request.setBotname("miaoTestSeqTime");
// request.set_type("url");
//
// TextInfo textInfo = new TextInfo();
// textInfo.setBotAccount("miaoTestSeqTime");
// textInfo.setNpcName("默笙");
// textInfo.setPerson("Liehu1");
// textInfo.setTone("正常");
// textInfo.setPitch("0.5");
// textInfo.setSpeed("1.36");
// textInfo.setVolume("12.5");
// textInfo.setText(str);
// request.setText_info_ls(Collections.singletonList(textInfo));
//
// ApachHttpUtils.post("http://hw-gz25.heyqiwu.cn:9663/api/tts_ls", JSON.toJSONString(request), header);
// try {
// Thread.sleep(3000);
// } catch (Exception e) {
//
// }
// }
String preCsvPath = "/home/yf01/下载/audioCut/timeAccount/A13.csv";
try (CSVParser csvRecords = CSVFormat.EXCEL.parse(new BufferedReader(
new InputStreamReader(new FileInputStream(new File(preCsvPath)), StandardCharsets.UTF_8)))) {
boolean first = true;
String prefix ="http://hw-gz25.heyqiwu.cn:9663/api/audio/cache/miaoTestTime/?filename=/miaoTestTime/A13/正常/";
String cdnPrefix = "http://cdntest.heyqiwu.cn/api/audio/cache/miaoTestTime/?filename=/miaoTestTime/A13/正常/";
for (CSVRecord line: csvRecords) {
if (first) {
first = false;
continue;
}
// "person", "mood", "md5", "id", "upUrl"
String text = line.get(0);
String url = prefix+TTSUtils.buildMP3Filename(text);
long t1 = System.currentTimeMillis();
String noCdnSize = ApachHttpUtils.get(url, null, null);
long noCdnTime = System.currentTimeMillis()-t1;
try {
Thread.sleep(1000);
} catch (Exception e) {
}
url = cdnPrefix+TTSUtils.buildMP3Filename(text);
t1 = System.currentTimeMillis();
String cdnOnceSize = ApachHttpUtils.get(url, null, null);
long cdnOnceTime = System.currentTimeMillis()-t1;
try {
Thread.sleep(1000);
} catch (Exception e) {
}
t1 = System.currentTimeMillis();
String cdnSize = ApachHttpUtils.get(url, null, null);
long cdnTime = System.currentTimeMillis()-t1;
buildTTSTime(text, noCdnSize, noCdnTime, cdnOnceSize, cdnOnceTime, cdnSize, cdnTime);
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
} }
} }
...@@ -13,25 +13,17 @@ import javax.servlet.http.HttpServletResponse; ...@@ -13,25 +13,17 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component //@Component
public class CorsFilter implements Filter { public class CorsFilter {
final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class); // public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
// throws IOException, ServletException {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) // HttpServletResponse response = (HttpServletResponse) res;
throws IOException, ServletException { // response.setHeader("Access-Control-Allow-Origin", "*");
HttpServletResponse response = (HttpServletResponse) res; // response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", "*"); // response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Allow-Credentials", "true"); // response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Methods", "*"); // response.setHeader("Access-Control-Allow-Headers", "*");
response.setHeader("Access-Control-Max-Age", "3600"); // chain.doFilter(req, res);
response.setHeader("Access-Control-Allow-Headers", "*"); // }
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
} }
\ No newline at end of file
...@@ -15,8 +15,7 @@ import org.springframework.stereotype.Service; ...@@ -15,8 +15,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
...@@ -261,6 +260,8 @@ public class TTServiceEdition2 { ...@@ -261,6 +260,8 @@ public class TTServiceEdition2 {
} }
public void tts_ls(TTSLSRequest request, HttpServletResponse response) { public void tts_ls(TTSLSRequest request, HttpServletResponse response) {
// long start = System.currentTimeMillis();
String _type = request.get_type(); String _type = request.get_type();
String platform = request.getPlatform(); String platform = request.getPlatform();
List<TextInfo> infoList = request.getText_info_ls(); List<TextInfo> infoList = request.getText_info_ls();
...@@ -364,6 +365,7 @@ public class TTServiceEdition2 { ...@@ -364,6 +365,7 @@ public class TTServiceEdition2 {
if (!TTSUtils.doesFileExist(path)) { if (!TTSUtils.doesFileExist(path)) {
ResponseCodeWithData response1 = generalMP3(moodUrl, data, path, "GET",true); ResponseCodeWithData response1 = generalMP3(moodUrl, data, path, "GET",true);
if (response1 == null) response1 = new ResponseCodeWithData(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "tts_ls:generalMP3 return null"); if (response1 == null) response1 = new ResponseCodeWithData(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "tts_ls:generalMP3 return null");
// buildTTSTime(data.getText(), data.getPerson(), data.getText().length(), System.currentTimeMillis()-start);
return response1; return response1;
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -403,6 +405,20 @@ public class TTServiceEdition2 { ...@@ -403,6 +405,20 @@ public class TTServiceEdition2 {
} }
} }
// private void buildTTSTime(String sentence, String person, Integer length, Long time) {
// synchronized(TTServiceEdition2.class) {
// CSVFormat format;
// if (Files.exists(Paths.get("/root/TTS-service/accountTime.csv"))) format = CSVFormat.DEFAULT;
// else format = CSVFormat.DEFAULT.withHeader("sentence", "person", "length", "time");
// try (CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(
// new FileOutputStream("/root/TTS-service/accountTime.csv", true), Charset.forName("UTF-8")),format)){
// csvPrinter.printRecord(sentence, person, length, time);
// } catch (IOException e) {
// log.error("buildTTSTime:"+e);
// }
// }
// }
private ResponseCodeWithData generalMP3(String url, GeneralMP3Data data, String filePath, String requestMethod, private ResponseCodeWithData generalMP3(String url, GeneralMP3Data data, String filePath, String requestMethod,
boolean getOnline) { boolean getOnline) {
ResponseCodeWithData res = null; ResponseCodeWithData res = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment