소스 검색

调整AI生成逻辑

zjs_project 1 년 전
부모
커밋
1fee66eed0
4개의 변경된 파일174개의 추가작업 그리고 93개의 파일을 삭제
  1. 18 3
      src/components/newBottomCom/viewMask/viewMask.html
  2. 146 90
      src/components/newBottomCom/viewMask/viewMask.vue
  3. 6 0
      src/services/request.js
  4. 4 0
      src/services/requestConfig.js

+ 18 - 3
src/components/newBottomCom/viewMask/viewMask.html

@@ -18,12 +18,12 @@
 	<div class="indicator-view rows" v-if="aiImagesList && aiImagesList.length>1 && showAIImage">
 		<div class="indicator-item" :class="{'active': currentIndex == idx}" v-for="(item, idx) in aiImagesList" :key="idx"></div>
 	</div>
-	<div class="loading_more" v-if="aiFlag && aiImagesList.length>0 && showAIImage">
+	<!-- <div class="loading_more" v-if="aiFlag && aiImagesList.length>0 && showAIImage">
 		<img class="loading_img"
 			src="https://dm.static2.elab-plus.com/aiBeauty/aiBeauty3/icon_loading_Img.png"
 			mode="aspectFit"></image>
 			自动生成更多图片中{{random+'%'}}...
-	</div>
+	</div> -->
 	<div class="btn-view columns" v-if="showAIImage==false">
 		<!-- 切换视图-退出当前模式 -->
 		<div class="compareBtn btn1" @click.stop="showOrHideWebGl" v-if="aiImagesList && aiImagesList.length>0">
@@ -118,7 +118,22 @@
 		<span>当前AI使用火爆,请继续尝试</span>
 		<span slot="footer" class="dialog-footer">
 			<el-button @click.stop="cancelHandle();">取消等待</el-button>
-			<el-button type="primary" @click.stop="confirmHandle();">继续尝试</el-button>
+			<el-button type="primary" @click.stop="confirmHandle(1);">继续尝试</el-button>
+		</span>
+	</el-dialog>
+	<el-dialog
+		title="提醒"
+		:visible.sync="dialogAIVisible"
+		width="80%"
+		top="35vh"
+		:modal="false"
+		style="pointer-events: auto;"
+		:before-close="cancelHandle"
+		>
+		<span>AI开了小差,是否重新生成</span>
+		<span slot="footer" class="dialog-footer">
+			<el-button @click.stop="cancelHandle();">放弃生成</el-button>
+			<el-button type="primary" @click.stop="confirmHandle(0);">继续生成</el-button>
 		</span>
 	</el-dialog>
 </div>

+ 146 - 90
src/components/newBottomCom/viewMask/viewMask.vue

@@ -21,8 +21,9 @@
 				aiFlag:false,
 				aiImage: "",
 				inputBase64Url: "",
-				taskId: "", // 图生图任务ID
-				img2imgTimer: null,
+				aiPicId: "", // 图生图任务ID
+				randomTimer:null,
+				timer: null,
 				outTimer: null,
 				imageWidth: 750,
 				imageHeight: 448,
@@ -221,17 +222,18 @@
 				reqListReset:[
 					{
 						model:'deployments/elabgroup/elab-sdxl-controlnet-lora',
-						level:'5',
+						level:'3',
 						aiPicId:'',
 						resultImg:'',
 					},{
 						model:'batouresearch/sdxl-controlnet-lora',
-						level:'3',
+						level:'5',
 						aiPicId:'',
 						resultImg:'',
 					}
 				],
 				dialogVisible: false,
+				dialogAIVisible: false,
 			}
 		},
 		props:{
@@ -276,7 +278,7 @@
         },
 		beforeDestroy:function(){
 			console.warn("***beforeDestroy***");//更新到页面上的数据
-			this.clearInterval();
+			this.stopInterval();
 		},
 		computed: {
 			aiData() {
@@ -503,14 +505,6 @@
 					// this.disableAble = false;//可以点击
 				}
 			},
-			clearInterval() {
-				if (this.img2imgTimer) {
-					clearInterval(this.img2imgTimer);
-					this.img2imgTimer = null;
-				}
-				this.random = 1;
-				this.myloading = false;
-			},
 			//风格选择
 			selectStyle(idx){
 				if(this.curStyleIndex == idx){
@@ -539,17 +533,11 @@
 					return
 				}
 				this.aiFlag = true;
-				if (this.aiImage == "" || !this.aiImage) {
-					this.aiImage = ""; // https://dm.static.elab-plus.com/CE4/backImg.png
-				}
-				this.aiImage = "";
-			
-				this.clearInterval();
+				this.aiImage = "";//清空AI结果图
+				this.stopInterval();
 				if(!this.showAIImage){
 					this.myloading = true;
 					this.loadingMsg = "设计中...";
-					// this.$store.state.loading = true;
-					// this.$store.state.loadingMsg="设计中...";
 				}
 				
 				// this.inputBase64Url = await this.shottingAction(2);//开始截图-返回的是base64的数据
@@ -630,25 +618,18 @@
 			},
 			//前置逻辑
 			async prevHandle(parmas){
-				return '';//暂不需要开启
 				//不是首次请求,则无需前置判断 用户上传的也不需要前置处理
-				if(this.subType!=0 || this.userInput){
+				if(this.subType!=0){
 					return '';
 				}
 				return new Promise(async (resolve, reject) => {
-					let imgRes = await requestConfig("generateTaskImgToImgForAliyun", parmas);
-					if (imgRes.success) {
-						let aiPicId = imgRes.single || '';
-						let res = await requestConfig('generateProcess', {
-							id: aiPicId
-						})
+					let _data = JSON.parse(JSON.stringify(parmas))
+					delete _data.webhook;
+					let res = await requestConfig("img2img_local", _data);
+					if (res.success) {
 						if (res.success && res.single) {
-							if(res.single.category == 'scheduler'){//命中了
-								let resultImg = res.single.urls[0];
-								resolve(resultImg);
-							}else{
-								resolve('');
-							}
+							let resultImg = res.single;
+							resolve(resultImg);
 						}else{
 							resolve('');
 						}
@@ -720,32 +701,51 @@
 					console.log("图生图结果:", res);
 					let that = this;
 					if (res.success && res.single) {
-						this.taskId = res.single;
+						this.aiPicId = res.single;
 						if(this.modelType==2){//精准设计
 							if(subType == 0){//首次
-								this.reqList[0].aiPicId = this.taskId;
+								this.reqList[0].aiPicId = this.aiPicId;
 							}else{//重试
-								this.reqListReset[0].aiPicId = this.taskId;
+								this.reqListReset[0].aiPicId = this.aiPicId;
 							}
 						}
-						if (!this.img2imgTimer) {
-							this.count = 0;
-							this.random = 1;
-							this.currentImg = false;//当次生成图还没有结果
-							this.img2imgTimer = setInterval(() => {
-								this.count ++;
-								this.getResultForImgToImg();
-							}, 1000);
-							this.setOutTimer();
+						if (this.aiPicId) {
+							this.startInterval();//开始轮询AI生成图的结果
+						}else{
+							this.stopInterval()
 						}
 					}else{
-						this.clearInterval()
-						this.aiFlag = false;
-						this.aiImage = "";
+						this.stopInterval()
 						this.showToast("渲染失败,请重试")
 					}
 				}
 			},
+			stopInterval() {
+				if (this.randomTimer) {
+					clearInterval(this.randomTimer);
+					this.randomTimer = null;
+				}
+				if (this.timer) {
+					// clearInterval(this.timer);
+					this.timer = null;
+				}
+				if (this.outTimer) {
+					clearTimeout(this.outTimer)
+					this.outTimer = null
+				}
+				this.subType = 0;
+				this.reqList.forEach(it=>{
+					it.aiPicId = '';
+					it.resultImg = '';
+				})
+				this.reqListReset.forEach(it=>{
+					it.aiPicId = '';
+					it.resultImg = '';
+				})
+				// this.random = 1;
+				this.myloading = false;
+				this.aiFlag = false;
+			},
 			//随机处理
 			randomHandle(resultImg){
 				if(this.timer || !resultImg){
@@ -755,14 +755,14 @@
 				var count = 1;
 				var process = 0;//进度
 				var randomNum = Math.floor(Math.random() * 4 + 5);//5-8随机数
-				this.img2imgTimer = setInterval(function() {
+				this.randomTimer = setInterval(function() {
 					process = parseInt(count * 100/(randomNum));
 					if(process>=100){
 						process = 99;
 					}
 					if (count < randomNum) {//没有到上限
 						if(!self.showAIImage){
-							// this.$store.state.loadingMsg='生成中…' + this.random + '%';
+							self.myloading = true;
 							self.loadingMsg = '生成中…' + process + '%';
 						}
 					}else{
@@ -792,6 +792,20 @@
 					}
 				})
 			},
+			//开始生成AI图的轮询,每隔1s轮询一次
+			startInterval() {
+				if(this.timer){
+					return false;
+				}
+				let self = this;
+				this.count = 0;//轮询次数
+				var random = 0;
+				this.currentImg = false;//当次生成图还没有结果
+				this.timer = 1;
+				this.getOutPicture();//不在轮询,而是等结果
+				this.count = this.count + 1;
+				this.setOutTimer();//设置超时逻辑
+			},
 			//设置一个超时逻辑,到底指定时间后停止轮询,当前是90s
 			setOutTimer() {
 				if (this.outTimer) {
@@ -800,7 +814,7 @@
 				}
 				var self = this;
 				this.outTimer = setTimeout(function() {
-					if (self.img2imgTimer) {
+					if (self.timer) {
 						let hasResult = false;
 						if(self.modelType==2){//精准设计
 							let reqList = [];
@@ -818,31 +832,35 @@
 						}
 						console.warn("***hasResult***",hasResult)
 						if(!hasResult){//没有结果
-							self.clearInterval();//停止轮询
+							self.stopInterval();//停止轮询
 							// self.$message.warning("AI开了个小差,请稍后再试");
 							self.dialogVisible = true;//弹出超时提示
-							self.aiFlag = false;
-							self.aiImage = "";
 						}
 					}
 					clearTimeout(self.outTimer);
 					self.outTimer = null
 				}, this.timeOut);
 			},
-			confirmHandle(){
+			confirmHandle(type){
 				console.log('用户点击确定')
 				this.dialogVisible = false;
-				this.subType = 1;
+				this.dialogAIVisible = false;
+				this.subType = type || 0;
 				this.startServer();
 			},
 			cancelHandle(){
 				console.log('用户点击取消')
 				this.dialogVisible = false;
+				this.dialogAIVisible = false;
 				this.subType = 0;
 			},
-			// 轮询获取图片结果
-			async getResultForImgToImg() {
-				if(this.modelType==2){//精准设计且当次新开始
+			//获取生成图结果
+			getOutPicture() {
+				if(this.timer==null){
+					console.warn("***当前轮询已经结束了1***")
+					return false;
+				}
+				if(this.modelType==2){//精准设计
 					let reqList = [];
 					if(this.subType == 0){//首次
 						reqList = this.reqList;
@@ -854,9 +872,10 @@
 					});
 				}
 				else{
-					this.singleHandle({aiPicId:this.taskId})
+					this.singleHandle({aiPicId:this.aiPicId})
 				}
 			},
+			//发出获取结果请求获取AI生成结果
 			async singleHandle(model){
 				if(!model || !model.aiPicId){
 					return false;
@@ -864,54 +883,90 @@
 				var parmas = {
 					id: model.aiPicId,
 				};
-				let res = await requestConfig("generateProcess", parmas);
+				let res = await requestConfig("getPredictions", parmas);
 				if (res.success && res.single) {
 					if(this.currentImg){//当前已经有生成图了
 						console.warn("***当前已经有最高级生成图了***")
 						return false;
 					}
-					if (res.single.urls) {
-						console.warn("图生图轮询结束: ", res);
+					if(this.timer==null){
+						console.warn("***当前轮询已经结束了***")
+						return false;
+					}
+					if (res.single.status == 'succeeded' && res.single.output) {
 						if(this.modelType==2){//精准设计
-							model.resultImg = res.single.urls[0];
+							model.resultImg = res.single.output;
 							if(model.level=='5'){//最高级了
 								this.currentImg = true;
-								this.resultHandle(res.single.urls[0])
+								setTimeout(()=>{
+									this.resultHandle(res.single.output)
+								},2000)
 							}
 						}else{
-							this.resultHandle(res.single.urls[0])
+							this.currentImg = true;
+							setTimeout(()=>{
+								this.resultHandle(res.single.output)
+							},2000)
 						}
 						console.warn("***有生成图了***",model)
-						
-					} else {
-						if (!res.success) {
-							this.showToast("渲染失败,请重试")
-							this.aiFlag = false;
-							this.aiImage = "";
-							this.clearInterval();
-						}else {
-							// this.random = parseInt(this.count * 2);
-							this.random = parseInt(this.count * 100/(this.timeOut/1000));
-							if(this.random>=100){
-								this.random = 99;
-							}
-							if(!this.showAIImage){
-								// this.$store.state.loadingMsg='生成中…' + this.random + '%';
-								this.loadingMsg = '生成中…' + this.random + '%';
-							}else{
-							}
+					} 
+					if(this.modelType==2){//最高优先级返回没有图片,则处理进度条事宜
+						if(model.level=='5'){
+							this.processHandle(res.single);
 						}
+					}else{//普通设计
+						this.processHandle(res.single);
+					}
+				} else if (!res.success) {
+					this.stopInterval();//停止轮询
+					this.showToast("渲染失败,请重试")
+				}
+			},
+			//进度处理
+			processHandle(single){
+				console.warn("***single***",single.status,single.progress,single);
+				let self = this;
+				if(single.status=='starting'){//启动中的逻辑
+					if(this.count>=20){
+						this.stopInterval();//停止轮询
+						this.dialogAIVisible = true;
+					}else{
+						this.showToast("启动中");
+					}
+					this.getOutPicture();
+				}else if(single.status=='processing'){
+					let random = single.progress || 0;
+					if(random >= 100){
+						random = 99;
+					}
+					// uni.showLoading({
+					// 	icon: 'loading',
+					// 	title: '生成中…' + parseInt(random) + '%'
+					// })
+					if(!this.showAIImage){
+						this.myloading = true;
+						this.loadingMsg = '生成中…' + parseInt(random) + '%';
+					}
+					this.getOutPicture();
+				}else if(single.status=='succeeded'){
+					// uni.showLoading({
+					// 	icon: 'loading',
+					// 	title: '生成中…100%',
+					// 	duration:2000,
+					// })
+					if(!this.showAIImage){
+						this.myloading = true;
+						this.loadingMsg = '生成中…100%';
 					}
 				}
 			},
 			//返回结果处理
 			resultHandle(resultImg){
 				this.currentImg = true;
-				this.aiFlag = false;
-				this.random = 100;
+				// this.random = 100;
 				this.aiImage = resultImg;
 				this.showAIFlag = true;
-				let newImage = this.aiImage;
+				let newImage = resultImg;
 				let aiStyleName = this.styleList[this.curStyleIndex].styleName;
 				let _data = {
 					image:newImage,
@@ -951,7 +1006,7 @@
 					}
 					this.$store.dispatch('setAiData', cpAiData)
 				}
-				this.clearInterval();
+				this.stopInterval();
 				if(this.aiImagesList.length>1){
 					setTimeout(()=>{
 						// this.$refs.carousel.next();
@@ -960,6 +1015,7 @@
 				}
 			},
 			showToast(title,time=3000){
+				this.myloading = false;
 				this.$store.state.loading = true;
 				this.$store.state.loadingMsg = title || "";
 				setTimeout(()=>{

+ 6 - 0
src/services/request.js

@@ -14,6 +14,8 @@ axios.interceptors.request.use(function(config) {
 	}
 	if(!config.url.includes('aiGenerateImg/getResult') &&
 	!config.url.includes('aiGenerateImg/uploadImgControlNet') &&
+	!config.url.includes('aiGenerateImg/getPredictions') &&
+	!config.url.includes('aiGenerateImg/img2img_local') &&
 	!config.url.includes('aiGenerateImg/img2Img')){
 		requestCount++;//请求数+1
 	}
@@ -44,6 +46,8 @@ axios.interceptors.request.use(function(config) {
 axios.interceptors.response.use(function(response) {
 	if(!response.config.url.includes('aiGenerateImg/getResult') &&
 	!response.config.url.includes('aiGenerateImg/uploadImgControlNet') &&
+	!response.config.url.includes('aiGenerateImg/img2img_local') &&
+	!response.config.url.includes('aiGenerateImg/getPredictions') &&
 	!response.config.url.includes('aiGenerateImg/img2Img')){
 		requestCount = requestCount - 1;
 	}
@@ -56,6 +60,8 @@ axios.interceptors.response.use(function(response) {
 	console.log("***error***", error, requestCount)
 	if(	!error.config.url.includes('aiGenerateImg/getResult') &&
 		!error.config.url.includes('aiGenerateImg/uploadImgControlNet') &&
+		!error.config.url.includes('aiGenerateImg/img2img_local') &&
+		!error.config.url.includes('aiGenerateImg/getPredictions') &&
 		!error.config.url.includes('aiGenerateImg/img2Img')){
 		requestCount = requestCount - 1;
 	}

+ 4 - 0
src/services/requestConfig.js

@@ -34,6 +34,8 @@ const endpoints = {
 	generateTaskImgToImgForAliyun: 'elab-marketing-content/aiGenerateImg/img2Img', // 生成识别任务-阿里云(图生图)
 	generateTaskWordToImgForAliyun: 'elab-marketing-content/aiGenerateImg/addTaskWithWord2Img', // 生成识别任务-阿里云(文生图)
 	generateProcess: 'elab-marketing-content/aiGenerateImg/getResult', // 获取任务进度-阿里云
+	img2img_local: 'elab-marketing-content/aiGenerateImg/img2img_local', // 生成图第一次请求
+	getPredictions: 'elab-marketing-content/aiGenerateImg/getPredictions', // 获取任务进度-新版
 	shareDetail: '/elab-marketing-content/aiDreamHouse/V3.0/shareDetail', // 分享查看
 	// predictions: "https://api.replicate.com/v1/deployments/feathers-wing/spacely-realistic-style-softedge-a100/predictions", // 分享查看
 };
@@ -72,6 +74,8 @@ window.requestConfig = async(endpoint, options, isHideLoading = false, preventDo
         !requestOptions.url.includes('elab-marketing-analyse/heavenlyEye/getCompareMapTotalPopulation') &&
         !requestOptions.url.includes('aiGenerateImg/uploadImgControlNet') &&
         !requestOptions.url.includes('aiGenerateImg/img2Img') &&
+        !requestOptions.url.includes('aiGenerateImg/img2img_local') &&
+        !requestOptions.url.includes('aiGenerateImg/getPredictions') &&
         !requestOptions.url.includes('aiGenerateImg/getResult')
     ) {
         store.state.loading = true; //显示loading态