Andrew
Andrew

Reputation: 137

Flask Api with HTML giving Runtime error: cannot open image_file: No such file or directory

I am using flask based api with html ui file. When i am trying to upload a pdf through html and it passes through my api.py file it gives run time error that no such file exist.

app.py File

app = Flask(__name__)

@app.route('/predict', methods=['GET', 'POST'])

def predict():
    if request.method == 'POST':
        image_file = request.files["file_image"]

    print(image_file)
    nlp_model = spacy.load("nlp_model")

    docs = fitz.open(image_file)
    text = ""
    for p in docs:
        text = text + str(p.getText())

    tx_2 = "".join(text.split('\n')) 

    doc = nlp_model(tx_2)
    s = []

    for ent in doc.ents:
        s.append(ent)

    return  s

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

HTML file

<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="/predict" method="POST" enctype=multipart/form-data>
    <input type=file name="file_image">
    <input type=submit value=Submit>
</form>

Upvotes: 0

Views: 151

Answers (1)

unione
unione

Reputation: 371

The form

<form action="/predict">

would send data to

@app.route('/predict')

You want one of those

<form action="/index">
<form action="{{ url_for('predict') }}">

Edit: I'm not sure what fitz.open() does but you are passing the string 'image_file' to it. Not the actual file name, which would be fitz.open(image_file.filename)

Also, you may need to save the file to disk before passing its filename to fitz, but again, I don't know if this module requires that.

Upvotes: 1

Related Questions