erictik / midjourney-api

MidJourney client. Unofficial Node.js client
Apache License 2.0
1.65k stars 271 forks source link

经常不出图 #208

Closed webpon closed 11 months ago

webpon commented 11 months ago

the code that reproduces this issue or a replay of the bug

不出图·

Describe the bug

不出图·

error log

不出图·

zcpua commented 11 months ago

复现代码 bug日志

webpon commented 11 months ago

报UND_ERR_CONNECT_TIMEOUT

webpon commented 11 months ago

香港服务器

webpon commented 11 months ago

router.post('/add-paint', async (req, res) => { try { const { prompt, config_str, type, need_translate, task_id, index, is_show } = req.body const userList = await sql('SELECT * FROM fox_chatgpt_user WHERE id = ? LIMIT 1', [req.auth.userid]) || [] const userInfo = userList[0] const paint_balance = userInfo.paint_balance if (!userInfo.length <= 0) { res.status(500).send({ status: 'Fail', data: null, message: '非法用户' || 'Fail', }) return }

if (!paint_balance || paint_balance <= 0) {
  res.status(500).send({
    status: 'Fail',
    data: null,
    message: '剩余次数不足,请充值' || 'Fail',
  })
  return
}
if (type === 'generate') {
  const message = [
    {
      role: 'system',
      content: '你是翻译助手,只翻译描述为英文,不要有任何其他回答内容',
    },
    {
      role: 'user',
      content: prompt,
    },
  ]
  let prompt_en = ''
  if (need_translate) {
    await fetchStreamedChat({
      apiKey: chatgptConfig.OPENAI_API_KEY,
      apiUrl: chatgptConfig.OPENAI_BASE_URL,
      model: chatgptConfig.OPENAI_MODEL,
      messageInput: message,
    }, (responseChunk) => {
      const content = JSON.parse(responseChunk).choices[0].delta.content
      if (content)
        prompt_en += content
    })
  }
  else {
    prompt_en = prompt
  }

  if (config_str)
    prompt_en += ` ${config_str}`
  const uuid = Math.floor(Math.random() * 9000000000) + 1000000000
  const data = await sql('INSERT INTO fox_chatgpt_paint_log (user_id, model, task_id, status, type, public, prompt, prompt_en) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', [req.auth.userid, 'mj', uuid, 0, type, is_show ? 1 : 0, prompt, prompt_en])

  const client = new Midjourney({
    ...mjConfig,
    Ws: true,
  })
  try {
    await client.Connect()
  }
  catch (e) {
    res.status(500).send({
      status: 'Fail',
      data: e,
      message: e.message || '未知错误',
    })
  }
  res.send(
    {
      data: {
        id: uuid,
      },
      status: 'Success',
      message: 'ok',
    },
  )
  try {
    const msg = await client.Imagine(
      prompt_en,
      (url, progress) => {
        sql('UPDATE fox_chatgpt_paint_log SET status = ?, progress = ?, image_url = ? WHERE task_id = ? AND user_id = ?', [1, parseInt(progress), url, uuid, req.auth.userid])
      },
    )
    sql('UPDATE fox_chatgpt_paint_log SET status = ?, progress = ?, image_url = ?, image_info = ? WHERE task_id = ? AND user_id = ?', [2, 100, msg.uri, JSON.stringify(msg), uuid, req.auth.userid])
    await sql('UPDATE fox_chatgpt_user SET paint_balance = ? use_paint_num = ? WHERE id = ? LIMIT 1', [userInfo.paint_balance > 0 ? userInfo.paint_balance - 1 : 0, userInfo.use_paint_num + 1, req.auth.userid])
  }
  catch (error) {
    res.status(500).send({
      status: 'Fail',
      data: error,
      message: error.message || '未知错误',
    })
  }
  finally {
    client.Close()
  }
}
else if (type === 'upscale') {
  const uuid = Math.floor(Math.random() * 9000000000) + 1000000000
  const mainTask = await sql('SELECT * FROM fox_chatgpt_paint_log WHERE user_id = ? AND task_id = ? AND is_delete = 0 LIMIT 1 ', [req.auth.userid, task_id])
  if (mainTask[0]) {
    const imgInfo = JSON.parse(mainTask[0].image_info)
    await sql('INSERT INTO fox_chatgpt_paint_log (user_id, model, task_id, status, type, prompt, prompt_en, public) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', [req.auth.userid, 'mj', uuid, 0, type, mainTask[0].prompt, mainTask[0].prompt_en, is_show ? 1 : 0])
    imgInfo.index = index

    const client = new Midjourney({
      ...mjConfig,
      Ws: true,
    })
    await client.Connect()
    res.send(
      {
        data: {
          id: uuid,
        },
        status: 'Success',
        message: 'ok',
      },
    )
    try {
      const msg = await client.Upscale({
        index: imgInfo.index,
        msgId: imgInfo.id,
        hash: imgInfo.hash,
        flags: imgInfo.flags,
        loading: (url, progress) => {
          sql('UPDATE fox_chatgpt_paint_log SET status = ?, progress = ?, image_url = ? WHERE task_id = ? AND user_id = ?', [1, parseInt(progress), url, uuid, req.auth.userid])
        },
      })
      sql('UPDATE fox_chatgpt_paint_log SET status = ?, progress = ?, image_url = ?, image_info = ? WHERE task_id = ? AND user_id = ?', [2, 100, msg.uri, JSON.stringify(msg), uuid, req.auth.userid])
      await sql('UPDATE fox_chatgpt_user SET paint_balance = ? WHERE id = ? LIMIT 1', [userInfo.paint_balance > 0 ? userInfo.paint_balance - 1 : 0, req.auth.userid])
    }
    catch (error) {
      res.status(500).send({
        status: 'Fail',
        data: null,
        message: error.message || '未知错误',
      })
    }
    finally {
      client.Close()
    }
  }
  else {
    res.status(500).send({
      status: 'Fail',
      data: null,
      message: '任务不存在',
    })
  }
}

} catch (error) { res.status(500).send({ status: 'Fail', data: null, message: error.message || 'Fail', }) } })