MrPutzi / design-ai-toolkit

Design + AI Toolkit
https://design-plus-ai-toolkit.vercel.app/
1 stars 0 forks source link

Error during deployment #19

Open MrPutzi opened 3 months ago

MrPutzi commented 3 months ago

Check the code based on the error upon deployment.

[14:32:08] Running build in Washington, D.C., USA (East) – iad1 [14:32:08] Cloning github.com/MrPutzi/design-ai-toolkit (Branch: autopilot/17-20240408123157565, Commit: 6a2d2fa) [14:32:08] Cloning completed: 413.783ms [14:32:13] Restored build cache [14:32:13] Running "vercel build" [14:32:13] Vercel CLI 33.6.3 [14:32:14] Installing dependencies... [14:32:16] [14:32:16] changed 1 package in 2s [14:32:16] [14:32:16] 36 packages are looking for funding [14:32:16] run npm fund for details [14:32:16] Detected Next.js version: 13.1.6 [14:32:16] Detected package-lock.json generated by npm 7+ [14:32:16] Running "npm run build" [14:32:17] [14:32:17] > build [14:32:17] > next build [14:32:17] [14:32:17] info - Linting and checking validity of types... [14:32:21] Failed to compile. [14:32:21] [14:32:21] ./pages/api/realvision.ts:45:9 [14:32:21] Type error: Type 'string | string[] | undefined' is not assignable to type 'string | undefined'. [14:32:21] Type 'string[]' is not assignable to type 'string'. [14:32:21] [14:32:21]   43 |  [14:32:21]   44 |  const replicate = new Replicate({ [14:32:21] > 45 |  auth: req.headers['x-api-key'], // Replace with the API key provided by the user [14:32:21]   |  ^ [14:32:21]   46 |  userAgent: 'https://www.npmjs.com/package/create-replicate' [14:32:21]   47 |  }) [14:32:21]   48 |  const model = 'lucataco/realistic-vision-v5:8aeee50b868f06a1893e3b95a8bb639a8342e846836f3e0211d6a13c158505b1' [14:32:21] Error: Command "npm run build" exited with 1 [14:32:21]

Also try to get the right url from the response with real vision generation. The response look like this: { "completed_at": "2023-07-28T12:54:35.866109Z", "created_at": "2023-07-28T12:54:33.533109Z", "error": null, "id": "exol3grbmex2jvqhsmzm7h5htq", "input": { "steps": 20, "width": 512, "height": 728, "prompt": "RAW photo, a portrait photo of a latina woman in casual clothes, natural skin, 8k uhd, high quality, film grain, Fujifilm XT3", "guidance": 5, "negative_prompt": "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck" }, "logs": "0%| | 0/20 [00:00<?, ?it/s]\n 10%|█ | 2/20 [00:00<00:01, 14.66it/s]\n 20%|██ | 4/20 [00:00<00:01, 14.70it/s]\n 30%|███ | 6/20 [00:00<00:00, 14.85it/s]\n 40%|████ | 8/20 [00:00<00:00, 14.65it/s]\n 50%|█████ | 10/20 [00:00<00:00, 14.86it/s]\n 60%|██████ | 12/20 [00:00<00:00, 15.01it/s]\n 70%|███████ | 14/20 [00:00<00:00, 15.10it/s]\n 80%|████████ | 16/20 [00:01<00:00, 15.19it/s]\n 90%|█████████ | 18/20 [00:01<00:00, 15.23it/s]\n100%|██████████| 20/20 [00:01<00:00, 15.19it/s]\n100%|██████████| 20/20 [00:01<00:00, 15.03it/s]", "metrics": { "predict_time": 2.363836, "total_time": 2.333 }, "output": "https://replicate.delivery/pbxt/6GzgXMQEnfRiGirhuRhZjsMQypDQQ7AcfQasxbewWLQWktoiA/ai.png", "started_at": "2023-07-28T12:54:33.502273Z", "status": "succeeded", "urls": { "get": "https://api.replicate.com/v1/predictions/exol3grbmex2jvqhsmzm7h5htq", "cancel": "https://api.replicate.com/v1/predictions/exol3grbmex2jvqhsmzm7h5htq/cancel" }, "version": "c0259010b93e7a4102a4ba946d70e06d7d0c7dc007201af443cfc8f943ab1d3c" }

The output url is the one I need from the reply to be loaded as the generated image.

codeautopilot[bot] commented 3 months ago

Potential solution

To solve the bug, we need to address the type mismatch issue in the auth field assignment and ensure that the correct URL is extracted from the response of the replicate.run method.

What is causing this bug?

The bug is caused by the fact that req.headers['x-api-key'] can be of type string | string[] | undefined, but the auth field in the Replicate constructor expects a value of type string | undefined. The type mismatch leads to a compilation error. Additionally, the code responsible for extracting the URL from the response is not correctly handling the response structure.

Code

To fix the type mismatch, we can modify the assignment of the auth field to handle the case where req.headers['x-api-key'] is an array:

const replicate = new Replicate({
    auth: Array.isArray(req.headers['x-api-key']) ? req.headers['x-api-key'][0] : req.headers['x-api-key'],
    userAgent: 'https://www.npmjs.com/package/create-replicate'
});

To correctly extract the URL from the response, we should directly access the output field if it's a string:

const response = await replicate.run(model, { input });
const outputUrl = response.output; // Assuming `response` has a field `output` which is a string URL
console.log({ outputUrl });
res.status(200).json({ success: true, output: outputUrl });

How to replicate the bug

To replicate the bug, attempt to build and deploy the application with the original code. The type mismatch error will occur during the compilation step, and the incorrect handling of the response will prevent the correct URL from being sent back to the client.

Task

The task involves fixing the type error during deployment and ensuring that the correct URL is extracted from the response and sent back to the client. The solution provided above addresses both parts of the task.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on pages/api/realvision.ts Based on the error message provided in the ticket description, the issue seems to be with the assignment of the `auth` field in the `Replicate` constructor. The error message indicates that the type `'string | string[] | undefined'` is not assignable to type `'string | undefined'`. This suggests that `req.headers['x-api-key']` could potentially be an array of strings, whereas the `auth` field expects a single string or undefined. To resolve this issue, we need to ensure that the `auth` field is assigned a single string value even if `req.headers['x-api-key']` is an array. We can do this by checking if the value is an array and, if so, taking the first element, or by simply converting any value to a string. Here's the modified code snippet that addresses the type error: ```typescript const replicate = new Replicate({ auth: Array.isArray(req.headers['x-api-key']) ? req.headers['x-api-key'][0] : req.headers['x-api-key'], // Ensure auth is a string userAgent: 'https://www.npmjs.com/package/create-replicate' }) ``` Regarding the second part of the task, which involves parsing the JSON response to extract the 'output' URL, the current code seems to be attempting to handle the output with the `handleOutput` function and then again with `setGeneratedPhoto`. However, the `handleOutput` function is sending a response back to the client, which means that any code after `res.status(200).json(...)` will not execute because the response has already been sent. To fix this, we should remove the `handleOutput` function and its invocation, and instead use the `setGeneratedPhoto` logic to extract the URL and send the correct response. Here's the modified code snippet for handling the output: ```typescript const output = await replicate.run(model, { input }) as string[]; const setGeneratedPhoto = output.find((output) => output.includes('http')); console.log({setGeneratedPhoto}) res.status(200).json({ success: true, output: setGeneratedPhoto }); ``` This code assumes that the `output` from `replicate.run` is an array of strings and that the URL we're interested in is the one that includes 'http'. However, based on the response structure provided in the ticket, it seems that the `output` field is directly a string URL, not an array. If that's the case, the casting to `string[]` is incorrect, and the `find` method is unnecessary. Instead, the code should directly use the `output` as the URL: ```typescript const output = await replicate.run(model, { input }) as string; console.log({output}) res.status(200).json({ success: true, output: output }); ``` Please note that the above code assumes that the `output` from `replicate.run` is a string containing the URL. If the actual structure of the response from `replicate.run` is different, you will need to adjust the code accordingly to extract the URL correctly.