Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error during deployment #19

Open
MrPutzi opened this issue Apr 8, 2024 · 1 comment
Open

Error during deployment #19

MrPutzi opened this issue Apr 8, 2024 · 1 comment

Comments

@MrPutzi
Copy link
Owner

MrPutzi commented Apr 8, 2024

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] �[0m �[90m 43 | �[39m�[0m
[14:32:21] �[0m �[90m 44 | �[39m �[36mconst�[39m replicate �[33m=�[39m �[36mnew�[39m �[33mReplicate�[39m({�[0m
[14:32:21] �[0m�[31m�[1m>�[22m�[39m�[90m 45 | �[39m auth�[33m:�[39m req�[33m.�[39mheaders[�[32m'x-api-key'�[39m]�[33m,�[39m �[90m// Replace with the API key provided by the user�[39m�[0m
[14:32:21] �[0m �[90m | �[39m �[31m�[1m^�[22m�[39m�[0m
[14:32:21] �[0m �[90m 46 | �[39m userAgent�[33m:�[39m �[32m'https://www.npmjs.com/package/create-replicate'�[39m�[0m
[14:32:21] �[0m �[90m 47 | �[39m })�[0m
[14:32:21] �[0m �[90m 48 | �[39m �[36mconst�[39m model �[33m=�[39m �[32m'lucataco/realistic-vision-v5:8aeee50b868f06a1893e3b95a8bb639a8342e846836f3e0211d6a13c158505b1'�[39m�[0m
[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.

Copy link
Contributor

codeautopilot bot commented Apr 8, 2024

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:

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:

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:

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.

Disclaimer: This comment was entirely generated using AI. Be aware that the information provided may be incorrect.

Current plan usage: 101.16%

Have feedback or need help?
Discord
Documentation
[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant