createImgHandle.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import {
  2. MessageBox
  3. } from 'mint-ui';
  4. import {
  5. Toast
  6. } from 'mint-ui';
  7. export default {
  8. data() {
  9. return {
  10. timeOut: 50000, //超时时间
  11. subType: 0, //0 首次 1 再次
  12. aiPicId: "", // 图生图任务ID
  13. count: 0, //轮询次数
  14. randomTimer: null,
  15. timer: null,
  16. currentImg: null, //当次生成图的结果
  17. startCreate:false, //是否开始生成AI图了
  18. // myloading: false, //加载中
  19. loadingMsg: '', //加载中的话术
  20. reqList: [{
  21. model: 'batouresearch/sdxl-controlnet-lora',
  22. level: '5',
  23. aiPicId: '',
  24. resultImg: '',
  25. }],
  26. reqListReset: [{
  27. model: 'deployments/elabgroup/elab-sdxl-controlnet-lora',
  28. level: '3',
  29. aiPicId: '',
  30. resultImg: '',
  31. }, {
  32. model: 'batouresearch/sdxl-controlnet-lora',
  33. level: '5',
  34. aiPicId: '',
  35. resultImg: '',
  36. }],
  37. createTabIndex:0, //生成AI图时记录下当前AI图的类型序号
  38. createOptionIndex:0,//生成AI图时记录下当前AI图的选项序号
  39. paramObj:null, //启动参数-外界传进来的
  40. }
  41. },
  42. methods: {
  43. //前置逻辑
  44. async prevHandle(parmas) {
  45. //不是首次请求,则无需前置判断 用户上传的也不需要前置处理
  46. if (this.subType != 0) {
  47. return '';
  48. }
  49. return new Promise(async (resolve, reject) => {
  50. let _data = JSON.parse(JSON.stringify(parmas))
  51. delete _data.webhook;
  52. let res = await requestConfig("img2img_local", _data);
  53. if (res.success) {
  54. if (res.success && res.single) {
  55. let resultImg = res.single;
  56. resolve(resultImg);
  57. } else {
  58. resolve('');
  59. }
  60. } else {
  61. resolve('');
  62. }
  63. })
  64. },
  65. //随机处理
  66. randomHandle(resultImg) {
  67. if (this.timer || !resultImg) {
  68. return false;
  69. }
  70. let self = this;
  71. var count = 1;
  72. var process = 0; //进度
  73. var randomNum = Math.floor(Math.random() * 4 + 5); //5-8随机数
  74. this.randomTimer = setInterval(function() {
  75. process = parseInt(count * 100 / (randomNum));
  76. if (process >= 100) {
  77. process = 99;
  78. }
  79. if (count < randomNum) { //没有到上限
  80. // self.myloading = true;
  81. self.loadingMsg = '生成中…' + process + '%';
  82. } else {
  83. // self.myloading = false;
  84. this.loadingMsg = '生成中…100%';
  85. self.resultHandle(resultImg);
  86. }
  87. count = count + 1;
  88. }, 1000);
  89. },
  90. //开始图生图流程
  91. async startServer(obj) {
  92. if (!obj || !obj.imgUrl || !obj.prompt || !obj.negativePrompt) {
  93. Toast({
  94. message: '数据不全',
  95. });
  96. return false
  97. }
  98. // if(this.$parent.pvCurPageName!="room_show"){//说明用户切换页面了
  99. // console.warn("***用户已经退出页面***")
  100. // return false;
  101. // }
  102. let imgUrl = obj.imgUrl;
  103. let subType = this.subType;
  104. let session_hash = Date.now();
  105. let prompt = obj.prompt;
  106. let noPromot = obj.negativePrompt;
  107. // let unit = 768 / this.screenWidth;
  108. // this.imageWidth = parseInt((this.screenWidth * unit).toFixed());
  109. // this.imageHeight = parseInt((this.$parent.canvasHeight * unit).toFixed());
  110. if(this.startCreate){
  111. return false
  112. }
  113. this.startCreate = true;
  114. this.paramObj = obj;
  115. this.loadingMsg = '启动中';
  116. this.createTabIndex = obj.tabIndex;
  117. this.createOptionIndex = obj.optionIndex;
  118. var parmas = {
  119. negativePrompt: noPromot,
  120. prompt: prompt,
  121. "batchSize": 1,
  122. brandId: $config.brandId,
  123. height: 1,
  124. width: 1,
  125. "moduleType": "AI_Biography",
  126. "steps": 20,
  127. "sampler": "DDIM",
  128. "controlNetSessionHash": session_hash,
  129. "cfgScale": 12,
  130. "denoising": 0.9,
  131. image: imgUrl,
  132. // styleImage:imgUrl,
  133. keyword: "zhiqite",
  134. model: 'controlnet',
  135. };
  136. let result = await this.prevHandle(parmas);
  137. console.warn("***prevHandle***", result)
  138. if (result && result.length > 0) { //匹配到了,则随机处理
  139. this.randomHandle(result)
  140. return false;
  141. } else {
  142. this.otherHandle(parmas); //发送其他AI请求
  143. let res = await requestConfig("generateTaskImgToImgForAliyun", parmas);
  144. console.log("图生图结果:", res);
  145. let that = this;
  146. if (res.success && res.single) {
  147. this.aiPicId = res.single;
  148. if (subType == 0) { //首次
  149. this.reqList[0].aiPicId = this.aiPicId;
  150. } else { //重试
  151. this.reqListReset[0].aiPicId = this.aiPicId;
  152. }
  153. if (this.aiPicId) {
  154. this.startInterval(); //开始轮询AI生成图的结果
  155. } else {
  156. this.stopInterval()
  157. }
  158. } else {
  159. this.stopInterval()
  160. // this.showToast("渲染失败,请重试")
  161. }
  162. }
  163. },
  164. stopInterval() {
  165. if (this.randomTimer) {
  166. clearInterval(this.randomTimer);
  167. this.randomTimer = null;
  168. }
  169. if (this.timer) {
  170. // clearInterval(this.timer);
  171. this.timer = null;
  172. }
  173. if (this.outTimer) {
  174. clearTimeout(this.outTimer)
  175. this.outTimer = null
  176. }
  177. this.subType = 0;
  178. this.reqList.forEach(it => {
  179. it.aiPicId = '';
  180. it.resultImg = '';
  181. })
  182. this.reqListReset.forEach(it => {
  183. it.aiPicId = '';
  184. it.resultImg = '';
  185. })
  186. // this.myloading = false;
  187. // this.aiFlag = false;
  188. this.startCreate = false;//释放标志
  189. },
  190. //从请求接口队列里面挨个发出请求
  191. otherHandle(parmas) {
  192. let reqList = [];
  193. if (this.subType == 0) { //首次
  194. reqList = this.reqList;
  195. } else { //重试
  196. reqList = this.reqListReset;
  197. }
  198. reqList.forEach(async (it, index) => {
  199. let _data = JSON.parse(JSON.stringify(parmas));
  200. if (index > 0) {
  201. _data.model = it.model;
  202. let res = await requestConfig("generateTaskImgToImgForAliyun", _data);
  203. if (res.success) {
  204. console.log('生成结果123:', res);
  205. it.aiPicId = res.single || '';
  206. }
  207. }
  208. })
  209. },
  210. //开始生成AI图的轮询,每隔1s轮询一次
  211. startInterval() {
  212. if (this.timer) {
  213. return false;
  214. }
  215. let self = this;
  216. this.count = 1; //轮询次数
  217. var random = 0;
  218. this.currentImg = false; //当次生成图还没有结果
  219. this.timer = 1; //标志进入了轮询流程
  220. this.getOutPicture(); //不在轮询,而是等结果
  221. this.setOutTimer(); //设置超时逻辑
  222. },
  223. //设置一个超时逻辑,到底指定时间后停止轮询,当前是90s
  224. setOutTimer() {
  225. if (this.outTimer) {
  226. clearTimeout(this.outTimer)
  227. this.outTimer = null
  228. }
  229. var self = this;
  230. this.outTimer = setTimeout(function() {
  231. if (self.timer) {
  232. let hasResult = false;
  233. let reqList = [];
  234. if (self.subType == 0) { //首次
  235. reqList = self.reqList;
  236. } else { //重试
  237. reqList = self.reqListReset;
  238. }
  239. reqList.some((item, index) => {
  240. if (item.resultImg) {
  241. hasResult = true;
  242. self.resultHandle(item.resultImg)
  243. }
  244. });
  245. console.warn("***hasResult***", hasResult)
  246. if (!hasResult) { //没有结果
  247. self.stopInterval(); //停止轮询
  248. MessageBox.confirm('', {
  249. title: '提示',
  250. message: '当前AI使用火爆,请继续尝试?',
  251. showCancelButton: true,
  252. confirmButtonText: '继续尝试',
  253. cancelButtonText: '取消等待',
  254. }).then(action => {
  255. console.warn("***MessageBox-action***", action)
  256. if (action == 'confirm') {
  257. self.confirmHandle(1);
  258. }
  259. }).catch(err => {
  260. console.warn("***MessageBox-err***", err)
  261. if (err == 'cancel') {
  262. self.cancelHandle();
  263. }
  264. });
  265. }
  266. }
  267. clearTimeout(self.outTimer);
  268. self.outTimer = null
  269. }, this.timeOut);
  270. },
  271. confirmHandle(type) {
  272. this.subType = type || 0;
  273. this.startServer(this.paramObj);
  274. },
  275. cancelHandle() {
  276. this.subType = 0;
  277. },
  278. //获取生成图结果
  279. getOutPicture() {
  280. if (this.timer == null) {
  281. console.warn("***当前轮询已经结束了1***")
  282. return false;
  283. }
  284. let reqList = [];
  285. if (this.subType == 0) { //首次
  286. reqList = this.reqList;
  287. } else { //重试
  288. reqList = this.reqListReset;
  289. }
  290. reqList.forEach((item, index) => {
  291. this.singleHandle(item)
  292. });
  293. },
  294. //发出获取结果请求获取AI生成结果
  295. async singleHandle(model) {
  296. if (!model || !model.aiPicId) {
  297. return false;
  298. }
  299. var parmas = {
  300. id: model.aiPicId,
  301. };
  302. let res = await requestConfig("getPredictions", parmas);
  303. if (res.success && res.single) {
  304. if (this.currentImg) { //当前已经有生成图了
  305. console.warn("***当前已经有最高级生成图了***")
  306. return false;
  307. }
  308. if (this.timer == null) {
  309. console.warn("***当前轮询已经结束了***")
  310. return false;
  311. }
  312. if (res.single.status == 'succeeded' && res.single.output) {
  313. model.resultImg = res.single.output; //把生成图结果记录到请求接口对象里面
  314. if (model.level == '5') { //最高级的生成图,可以直接用
  315. this.currentImg = true;
  316. setTimeout(() => {
  317. this.resultHandle(res.single.output)
  318. }, 1500)
  319. }
  320. console.warn("***有生成图了***", model)
  321. }
  322. if (model.level == '5') { //最高优先级的接口返回的结果
  323. this.processHandle(res.single);
  324. }
  325. } else if (!res.success && model.level == '5') { //最高优先级的接口返回失败
  326. this.stopInterval(); //停止轮询
  327. // this.showToast("渲染失败,请重试")
  328. }
  329. },
  330. //进度处理
  331. processHandle(single) {
  332. console.warn("***single***", single.status, single.progress, this.count, single);
  333. let self = this;
  334. if (single.status == 'starting') { //启动中的逻辑
  335. if (this.count >= 20) {
  336. this.stopInterval(); //停止轮询
  337. MessageBox.confirm('', {
  338. title: '提示',
  339. message: 'AI开了小差,是否重新生成?',
  340. showCancelButton: true,
  341. confirmButtonText: '继续生成',
  342. cancelButtonText: '放弃生成',
  343. }).then(action => {
  344. console.warn("***MessageBox-action***", action)
  345. if (action == 'confirm') { //继续生成
  346. this.confirmHandle(0);
  347. }
  348. }).catch(err => {
  349. if (err == 'cancel') {
  350. this.cancelHandle();
  351. }
  352. });
  353. } else {
  354. // this.myloading = true;
  355. this.loadingMsg = '启动中';
  356. }
  357. this.count = this.count + 1;
  358. setTimeout(()=>{
  359. this.getOutPicture();
  360. },10000)
  361. } else if (single.status == 'processing') {
  362. let random = single.progress || 0;
  363. if (random >= 100) {
  364. random = 99;
  365. }
  366. // this.myloading = true;
  367. this.loadingMsg = '生成中…' + parseInt(random) + '%';
  368. setTimeout(()=>{
  369. this.getOutPicture();
  370. },10000)
  371. } else if (single.status == 'succeeded') {
  372. // this.myloading = true;
  373. this.loadingMsg = '生成中…100%';
  374. }
  375. },
  376. //返回结果处理
  377. resultHandle(resultImg) {
  378. this.currentImg = true;
  379. this.aiImage = resultImg;
  380. this.stopInterval();
  381. let newImage = resultImg;
  382. // let aiStyleName = this.styleList[this.curStyleIndex].styleName;
  383. let _data = {
  384. imageUrl: newImage,
  385. checked: false,
  386. type:'AI',//表示AI生成的
  387. }
  388. // this.aiImagesList.push(_data);
  389. this.paramObj = null;//生成成功后,清楚外界传入参数
  390. // this.tabData[this.createTabIndex].options[this.createOptionIndex].hardboundEffect.push(_data);
  391. if (this.createTabIndex == this.tabIndex) {
  392. let len = this.aiImagesList.length;
  393. this.aiImagesList.push(_data);
  394. setTimeout(() => {
  395. this.$refs.carousel.setActiveItem(len); //切换到最后一张
  396. }, 200);
  397. }else{
  398. this.tabData[this.createTabIndex].options[this.createOptionIndex].hardboundEffect.push(_data);
  399. }
  400. // this.subDataList[this.tabIndex].hardboundEffect.push(_data);//把数据同步到对应的待提交对象中
  401. },
  402. }
  403. }