adrianhajdin/project_ai_mern_image_generation

Running Project with Working API

SETHEAD opened this issue · 9 comments

if anyone has done the same project where in the api actually works, please help me out...

if anyone has done the same project where in the api actually works, please help me out...

Replace dalleRoutes.js with this code, and make sure you have the open ai API credits

import express from 'express';
import * as dotenv from 'dotenv';
import OpenAI from 'openai';

dotenv.config();

const router = express.Router();

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY 
});
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.images.generate({
            prompt,
            n: 1,
            size: '1024x1024',
            response_format: 'b64_json',
        });
        
        // console.log("aiResponse :  ",aiResponse)
        const image = aiResponse.data[0].b64_json;
        res.status(200).json({photo: image});
        
    } catch (error) {
        console.log(error);
        res.status(500).send(error?.response.data.error.message)
    }
})

export default router;

if anyone has done the same project where in the api actually works, please help me out...

Replace dalleRoutes.js with this code, and make sure you have the open ai API credits

import express from 'express';
import * as dotenv from 'dotenv';
import OpenAI from 'openai';

dotenv.config();

const router = express.Router();

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY 
});
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.images.generate({
            prompt,
            n: 1,
            size: '1024x1024',
            response_format: 'b64_json',
        });
        
        // console.log("aiResponse :  ",aiResponse)
        const image = aiResponse.data[0].b64_json;
        res.status(200).json({photo: image});
        
    } catch (error) {
        console.log(error);
        res.status(500).send(error?.response.data.error.message)
    }
})

export default router;

hello, can i connect with you on discord or some other platform if its fine by you?

if anyone has done the same project where in the api actually works, please help me out...

Replace dalleRoutes.js with this code, and make sure you have the open ai API credits

import express from 'express';
import * as dotenv from 'dotenv';
import OpenAI from 'openai';

dotenv.config();

const router = express.Router();

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY 
});
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.images.generate({
            prompt,
            n: 1,
            size: '1024x1024',
            response_format: 'b64_json',
        });
        
        // console.log("aiResponse :  ",aiResponse)
        const image = aiResponse.data[0].b64_json;
        res.status(200).json({photo: image});
        
    } catch (error) {
        console.log(error);
        res.status(500).send(error?.response.data.error.message)
    }
})

export default router;

hello, can i connect with you on discord or some other platform if its fine by you?

sure

@SarangMarkandey thanks for helping, took some time to debug, but I can finally run the api. For anyone facing the issue, please first update your openai in package.json to "openai": "^4.0.0". Then npm install, and then paste the above code in dalleRoutes.js
Please refer this link to update from v3 to v4 openai/openai-node#217

I've done all of that and I'm still getting TypeError: Failed to fetch. I have 5$ credits, do you have some idea why I'm getting this ?

I've done all of that and I'm still getting TypeError: Failed to fetch. I have 5$ credits, do you have some idea why I'm getting this ?

can you share your code?

I've done all of that and I'm still getting TypeError: Failed to fetch. I have 5$ credits, do you have some idea why I'm getting this ?

same :((

did you find any solution for that??

Hey @SETHEAD , i am interested in work on this, could you assign this Issue to me?

@SETHEAD @SarangMarkandey i am also facing same issue Typeerror;5173 says Failed in fetching.