nginx Ubuntu Flask应用程序看起来不工作,因为试图在被删除的代码中找到文件

iyfjxgzm  于 2023-03-29  发布在  Nginx
关注(0)|答案(1)|浏览(114)

你好,我试图运行我的 flask 应用程序在ubuntu服务器(数字海洋水滴),但我遇到一些奇怪的错误。
错误消息说,文件的路径是不正确的,但我已经改变了这个文件,现在它不包含任何路径,所以我知道为什么我看到这个错误。
下面是错误消息:

File "/var/www/html/cancel-the-cancer/venv/lib/python3.10/site-packages/flask/views.py", line 107, in view
    return current_app.ensure_sync(self.dispatch_request)(**kwargs)
  File "/var/www/html/cancel-the-cancer/venv/lib/python3.10/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/var/www/html/cancel-the-cancer/Resources/predict.py", line 10, in post
    img = img.resize((256, 256))
  File "/var/www/html/cancel-the-cancer/venv/lib/python3.10/site-packages/werkzeug/datastructures.py", line 3002, in save
    dst = open(dst, "wb")
FileNotFoundError: [Errno 2] No such file or directory: './images/ISIC_0029612.jpg.jpg'

下面是predic.py文件:

import numpy as np
from flask_restful import Resource, request
import tensorflow as tf
from PIL import Image

class Predict(Resource):
    def post(self):
        file = request.files['image']
        img = Image.open(file.stream)
        img = img.resize((256, 256))
        img = np.array(img)
        img = img / 255.0
        img = np.expand_dims(img, axis=0)
        loaded_model = tf.keras.models.load_model('../model/vgg19_92%_93%')
        prediction = loaded_model.predict(img)[0][0]
        return {"result": float(prediction)}

下面是app.py文件:

import flask_restful
from flask_cors import CORS

from flask_cors import CORS
from flask import Flask
from flask_restful import Api

from Resources.predict import Predict

app = Flask(__name__)
app.secret_key = 'zwolnienizteorii44893'
api = Api(app)
cors = CORS(app)

api.add_resource(Predict, '/predict')

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

我也尝试了同样的应用程序在我的个人电脑上与windows 11和win它工作得很好,并返回预测,但在ubuntu作为API响应,我收到内部服务器错误。
我正在努力
sudo systemctl restart uwsgi
sudo systemctl restart nginx
sudo systemctl reload uwsgi
sudo systemctl reload nginx

ruarlubt

ruarlubt1#

你好,我终于解决了我的代码问题。首先,我创建了一个全新的droplet,并遵循了这个tutorial
我按照代码,所以我得到了相同的名字,因为在教程也路径像这样../model/vgg_19_92%_93%造成错误,所以我必须改变它为home/kremobil/cancelthecancer/model/vgg_19_92%_93%
这里是我改变或装箱的文件
cancelthecancer.py以前的app.py

import flask_restful
from flask_cors import CORS
from flask import Flask
from flask_restful import Api

from Resources.predict import Predict

app = Flask(__name__)
app.secret_key = 'zwolnienizteorii44893'
api = Api(app)
cors = CORS(app)

api.add_resource(Predict, '/predict')

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

下面是predict.py

import numpy as np
from flask_restful import Resource, request
import tensorflow as tf
from PIL import Image

class Predict(Resource):
    def post(self):
        file = request.files['image']
        img = Image.open(file.stream)
        img = img.resize((256, 256))
        img = np.array(img)
        img = img / 255.0
        img = np.expand_dims(img, axis=0)
        loaded_model = tf.keras.models.load_model('/home/kremobil/cancelthecancer/model/vgg19_92%_93%')
        prediction = loaded_model.predict(img)[0][0]
        return {"result": float(prediction)}

这里是新的crated wsgi.py像在教程:

from cancelthecancer import app

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

相关问题