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: Failed to fetch when generating an image #16

Open
jhaakshita27 opened this issue Jan 29, 2023 · 11 comments
Open

Error: Failed to fetch when generating an image #16

jhaakshita27 opened this issue Jan 29, 2023 · 11 comments

Comments

@jhaakshita27
Copy link

I can't move onto further steps because of this issue, someone please resolve this..

dalleRoutes.js

import express from 'express';
import * as dotenv from 'dotenv';
import { Configuration, OpenAIApi } from 'openai';

dotenv.config();

const router = express.Router();

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);

router.route('/').get((req, res) => {
res.send('Hello from DALL-E!' );
});

router.route('/').post(async (req, res) => {
try {
const { prompt } = req.body;

const aiResponse = await openai.createImage({
  prompt,
  n: 1,
  size: '1024x1024',
  response_format: 'b64_json'
});

const image = aiResponse.data.data[0].b64_json;
res.status(200).json({ photo: image });

} catch (error) {
console.error(error);
res.status(500).send(error?.response.data.error.message);
}
});

export default router;

CreatePost.jsx
const generateImage = async () => {
if (form.prompt) {
try {
setGeneratingImg(true);
const response = await fetch('http://localhost:8080/api/v1/dalle', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: form.prompt,
}),
})

    const data = await response.json();
    setForm({ ...form, photo: `data:image/jpeg;base64,${data.photo}` })
  } catch (error) {
    alert(error);
  } finally {
    setGeneratingImg(false);
  }
} else {
  alert('Please provide proper prompt')
}

}

image

@jhaakshita27
Copy link
Author

Sometimes it's working and sometimes not
Why is this happening? How to resolve?

@justaskulkarni
Copy link

Even I'm facing the same issue. Were you able to resolve it?

@Antonio-Ramon
Copy link

Same here, and I thought was just me, still looking for some way to resolve that

@Azzam1503
Copy link

Instead of alert use console.log(error) in the createPost page then see in detail what the issue is then solve or ask.

@lalitkumawat1m
Copy link

I have fixed this issue by
create new api key from openai and use it in .env file.

@muskan-Bansal
Copy link

I got the same error and created new api key from openai too but this didnt solve my problem

@Komal-J04
Copy link

Same issue...tried using 4 different api keys but doesn't work

@jhaakshita27
Copy link
Author

Keep trying with new API keys, my issue was resolved like this only.

@abdul3909
Copy link

Try reinstalling the nodemon:

For Linux:
sudo npm install -g nodemon

For Windows:
npm install -g nodemon

@Komal-J04
Copy link

Komal-J04 commented Mar 29, 2023 via email

@abdul3909
Copy link

But there's no url in the Home. jsx file..

On Tue, 28 Mar, 2023, 10:01 pm Abdul Basit, @.> wrote: My issue was resolved by changing the localhost URL as follows, Change the localhost URL in the client/src/pages/Home.jsx to: " http://localhost:8080/api/v1/post". Change the localhost URL in the client/src/pages/CreatePost.jsx to: " http://localhost:8080/api/v1/dalle" — Reply to this email directly, view it on GitHub <#16 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXZJXR234RASMNCFP4H7V73W6MG7VANCNFSM6AAAAAAUKMMAVY . You are receiving this because you commented.Message ID: @. .com>

There is in the line 29.

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

8 participants