adrianhajdin / project_ai_mern_image_generation

Build and Deploy a Full Stack MERN AI Image Generation App MidJourney & DALL E Clone
https://jsmastery.pro
1.06k stars 300 forks source link

Running Project with Working API #68

Open SETHEAD opened 11 months ago

SETHEAD commented 11 months ago

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

SarangMarkandey commented 11 months ago

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;
SETHEAD commented 11 months ago

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?

SarangMarkandey commented 11 months ago

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

HimanshuRahul commented 10 months ago

@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 https://github.com/openai/openai-node/discussions/217

euortiz commented 10 months ago

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 ?

SarangMarkandey commented 10 months ago

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?

SETHEAD commented 10 months ago

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??

ManashDholey commented 3 months ago

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

alpha7375 commented 1 month ago

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