Home > Net >  How do I solve page not found error in flask rest api
How do I solve page not found error in flask rest api

Time:11-24

I have developed a flask application that returns some text from OPEN-AI by giving some inputs.

But unfortunately the rest API in my application returns 404 error.

Here is the code:

from crypt import methods
from warnings import catch_warnings
from flask import Flask,request
from flask_cors import CORS
import flask
import openai
from flask_restful import Api,Resource
import base64
import json


#Init
app =   Flask(__name__)
CORS(app)
api =   Api(app)
app.run(host='0.0.0.0',port=8080)


#OPENAI CREDENTIALS
openai.api_key = ""


#Functions

class advert(Resource):
    def post(self):
        try:
            request_body=request.json
            A=request_body["data"]
            adprompt = "Write an advertisement for "   A


            
            response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=adprompt,
            temperature=0.7,
            max_tokens=70,
            top_p=1.0,
            n=1
            )
            json_advert = json.loads(str(response))
            advert_output = json_advert['choices'][0]['text']
            to_return= json_advert = json.loads(str(advert_output))

            return to_return,200
        except:
            return ({"ERROR":"Error Occured"}),500




#Mapping
api.add_resource(advert,'/data',methods=['POST'])




if __name__=='__main__':
    app.run(debug=True)
    


This is the response i get:

192.168.1.21 - - [24/Nov/2022 11:52:59] "POST /data HTTP/1.1" 404 -

I've tried changing the port and endpoints, nothing helped .

How to solve this.

CodePudding user response:

Your problem is at this line,

app.run(host='0.0.0.0',port=8080)

take it out, then add the parameters into the last line,

if __name__=='__main__':
    app.run(debug=True, host='0.0.0.0',port=8080)
  • Related