Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

<script setup lang="ts"> import { onMounted, ref, reactive, computed, watch, Ref,…

  1. User
            <!-- 模型类型选择 -->
            <van-row class="section" justify="center">
              <van-col :span="12">
                <div class="model-type">
                  <van-radio-group
                    v-model="checked_type"
                    direction="horizontal"
                  >
                    <van-radio name="1">SD-XL</van-radio>
                    <van-radio name="2">FLUX</van-radio>
                  </van-radio-group>
                </div>
              </van-col>
            </van-row>
    
            <!-- 模型ID输入 -->
            <van-row class="section" justify="center">
              <van-col :span="8"> </van-col>
              <van-col :span="12">
                <div class="model-id">
                  <van-cell-group inset>
                    <van-field
                      v-model="model_id"
                      label="模型ID:"
                      left-icon="smile-o"
                      right-icon="warning-o"
                      placeholder="输入模型ID"
                      clearable
                    />
                  </van-cell-group>
                </div>
              </van-col>
              <van-col :span="4">
                <v-btn block @click="clearSelection">清空选择的模型</v-btn>
              </van-col>
            </van-row>
    
            <!-- 模型列表和代码输入区域 -->
            <van-row class="section align-stretch" justify="center">
              <!-- 模型列表 -->
              <van-col :span="12" class="flex-item">
                <div class="model-list">
                  <v-container fluid>
                    <v-list lines="one" density="compact" max-height="300">
                      <v-list-item
                        v-for="item in paginatedModels"
                        :key="item.model_id"
                        :title="'   模型ID: ' + item.model_id"
                        ><v-checkbox
                          class="my-custom-checkbox"
                          color="red"
                          v-model="selected"
                          :label="
                            item.model_name + ' 调用次数: ' + item.api_calls
                          "
                          :value="item.model_id"
                        >
                        </v-checkbox>
                      </v-list-item>
                    </v-list>
                  </v-container>
                  <v-pagination
                    v-model="currentPage"
                    :length="totalPages"
                    class="pagination"
                  ></v-pagination>
                </div>
              </van-col>
    
              <!-- 代码输入 -->
              <van-col :span="12" class="flex-item">
                <div class="code_input">
                  <v-container>
                    <v-textarea
                      row-height="15"
                      bg-color="amber-lighten-4"
                      color="orange orange-darken-4"
                      label="代码输入"
                      rows="10"
                      v-model="code_input_"
                      placeholder='{
    

    "key": "", "model_id": "your_model_id", "prompt": "ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner)), blue eyes, shaved side haircut, hyper detail, cinematic lighting, magic neon, dark red city, Canon EOS R3, nikon, f/1.4, ISO 200, 1/160s, 8K, RAW, unedited, symmetrical balance, in-frame, 8K", "negative_prompt": "", "width": "512", "height": "512", "samples": "1", "num_inference_steps": "31", "safety_checker": "no", "enhance_prompt": "yes", "seed": null, "guidance_scale": 7.5, "panorama": "no", "self_attention": "no", "upscale": "no", "embeddings_model": null, "lora_model": null, "tomesd": "yes", "clip_skip": "2", "use_karras_sigmas": "yes", "vae": null, "lora_strength": null, "scheduler": "UniPCMultistepScheduler", "webhook": null, "track_id": null }' auto-grow > <v-btn block @click="ondraw">开始绘图

            <!-- 新增:画廊部分 -->
            <van-row class="draw" justify="center">
              <van-col :span="24">
                <div class="gallery">
                  <h2 class="gallery-title">画廊</h2>
                  <van-row justify="start" wrap>
                    <van-col
                      v-for="(image, index) in paginatedGalleryImages"
                      :key="index"
                      :span="4"
                      class="gallery-item"
                    >
                      <img
                        :src="image.url"
                        alt="Gallery Image"
                        class="gallery-image"
                      />
                      <div>{{ image.modelId }}</div>
                    </van-col>
                  </van-row>
                  <v-pagination
                    v-model="galleryCurrentPage"
                    :length="galleryTotalPages"
                    class="pagination"
                  ></v-pagination>
                </div>
              </van-col>
            </van-row>
          </div>
        </van-col>
      </van-row>
    </div>
    

    能否在请求图片的进度在msgtext.value上也追加一下

  2. Assistant

    可以的,您可以在请求图片的过程中更新 msgtext.value,以显示请求的进度。为此,您可以修改 pollForImage 函数,在每次轮询时更新 msgtext.value,显示当前的进度。

    以下是如何修改代码的步骤:

    1. 检查 API 是否提供进度信息:

      首先,检查您的 Pixihttp.get_output_image() 接口在状态为 'processing' 时,是否返回了任何与进度相关的数据,例如 progress 字段。如果有,您可以直接使用该字段来更新进度。

    2. 修改 pollForImage 函数:

      在 pollForImage 函数中,添加对进度信息的处理。在每次轮询时,根据返回的进度信息更新 msgtext.value。

    以下是修改后的代码示例:

    async function pollForImage(rs: any, pollingAttempt = 1) {
      try {
        let modelId = rs.data.meta.model_id
        const id = rs.data.id
        const key = '您的API_KEY' // 请替换为您的实际 API Key
        const response = await Pixihttp.get_output_image({
          request_id: id,
          key: key,
        })
        if (response.data.status === 'success') {
          console.log(response.data)
          if (response.data.output && response.data.output.length > 0) {
            msgtext.value = modelId + '的图片生成成功~'
            response.data.output.forEach((url: string) => {
              images.push({ modelId, url })
            })
          }
        } else if (response.data.status === 'failed') {
          console.error('模型生成失败:', modelId)
          msgtext.value = `模型 ${modelId} 的图像生成失败`
        } else if (response.data.status === 'processing') {
          // 如果 API 提供了进度数据,可以在这里更新 msgtext.value
          let progress = response.data.progress || null
          if (progress !== null) {
            msgtext.value = `模型 ${modelId} 的图像生成中...进度: ${progress}%`
          } else {
            // 如果没有进度信息,可以根据轮询次数估计进度,例如每次轮询增加10%
            let estimatedProgress = Math.min(pollingAttempt * 10, 99)
            msgtext.value = `模型 ${modelId} 的图像生成中...进度: ${estimatedProgress}%`
          }
          // 等待一段时间后再次轮询
          setTimeout(() => {
            pollForImage(rs, pollingAttempt + 1)
          }, 3000) // 每隔 3 秒轮询一次
        }
      } catch (error) {
        console.error('轮询图像时出错:', error)
        msgtext.value = '轮询图像时出错,请稍后重试'
      }
    }
    

    解释:

    • 在上述代码中,pollForImage 函数新增了一个 pollingAttempt 参数,表示轮询的次数。
    • 如果 API 返回了 progress 字段(例如进度百分比),那么直接使用它来更新 msgtext.value。
    • 如果没有进度信息,可以根据轮询次数来估算进度,例如每次轮询增加10%,但不会超过99%(因为100%表示完成)。
    • 在发生错误时,也更新 msgtext.value,以提示用户。
    1. 在 ondraw 函数中初始化进度消息:

      当开始请求时,您可以在 ondraw 函数中初始化 msgtext.value,告知用户正在开始请求。

    const ondraw = async () => {
      // 之前的代码...
    
      mergedList.forEach(async (modelId: string) => {
        msgtext.value = '正在请求' + modelId + '的图片'
        arg.model_id = modelId
        arg.key = '您的API_KEY' // 请替换为您的实际 API Key
    
        try {
          const rs = await Pixihttp.get_image(arg)
          // 初始化进度消息
          msgtext.value = `模型 ${modelId} 的图像生成已开始...`
          pollForImage(rs)
        } catch (error) {
          console.error(`请求失败 for model_id: ${modelId}`, error)
          msgtext.value = `请求模型 ${modelId} 的图像时出错`
        }
      })
    
      // 之后的代码...
    }
    
    1. 更新 watch 钩子:

      确保在 watch 钩子中,当 msgtext 发生变化时,text 对象的内容也会相应更新。

    watch(msgtext, (newVal) => {
      if (text) {
        text.text = newVal
      }
    })
    

    注意事项:

    • 请确保您的 API 确实返回了进度信息。如果没有,您可能需要根据自己的业务逻辑来估计进度。
    • 如果同时请求多个模型的图像,您可能需要区分每个模型的进度。可以考虑使用一个对象来保存每个模型的进度,并相应地更新显示。

    例如,使用一个 progressMap 来跟踪每个模型的进度:

    const progressMap = reactive<{ [modelId: string]: number }>({})
    
    async function pollForImage(rs: any, pollingAttempt = 1) {
      // ...之前的代码...
    
      } else if (response.data.status === 'processing') {
        let modelId = rs.data.meta.model_id
        let progress = response.data.progress || null
        if (progress !== null) {
          progressMap[modelId] = progress
        } else {
          let estimatedProgress = Math.min(pollingAttempt * 10, 99)
          progressMap[modelId] = estimatedProgress
        }
        // 更新消息,显示所有模型的进度
        msgtext.value = Object.entries(progressMap)
          .map(([id, prog]) => `模型 ${id} 进度: ${prog}%`)
          .join('\n')
    
        // 继续轮询
        setTimeout(() => {
          pollForImage(rs, pollingAttempt + 1)
        }, 3000)
      }
    
      // ...之后的代码...
    }
    

    这样,msgtext.value 将显示多个模型的进度,每个模型一行。

    希望这些修改能满足您的需求!