Apache或Blask需要特殊设置用于使用Pickle的Web应用程序



在Visual Studio中,我已经意识到使用Python的Web API,它使用Pickle加载SVM,然后将整个系统部署在Apache上。我的问题是,当我尝试从Visual Studio进行调试时,一切正常,但是当我将其加载到Apache Server上时,页面只需加载结果或错误。
Web应用程序的代码如下:

import PythonApplication2 as ml
import requests
import json
from flask_cors import CORS, cross_origin
from flask import Flask, jsonify, request, render_template
import pickle as pk
MLwebapp = Flask(__name__)
left_model=pk.load(open('C:\myapp\app\left_classifier.pickle','rb'))
right_model=pk.load(open('C:\myapp\app\right_classifier.pickle','rb'))
wsgi_app = MLwebapp.wsgi_app
# Make the WSGI interface available at the top level so wfastcgi can get it.

@MLwebapp.route('/')
def start():
    return render_template('WebPage1.html')
@MLwebapp.route('/test')
def hello():
    number='203'
    list_left=[]
    list_right=[]
    for i in range(0,19):
        data=requests.get('url here'+number+str(i))
        json_data=json.loads(data.json())
        number=json.loads(json_data['Dati'])
        left_temp=[x[1] for x in number]
        right_temp=[z[2] for z in number]
        list_left.append(left_temp)
        list_right.append(right_temp)
    patient_vector_right,patient_vector_left=ml.funzionetotale(list_left,list_right)
    patient_vector_left=patient_vector_left.reshape(1, -1)
    patient_vector_right=patient_vector_right.reshape(1, -1)
    prediction=ml.machine_learning(patient_vector_left,patient_vector_right,left_model,right_model)
    risultato="Risultato: " + prediction
    return jsonify(risultato)
if __name__ == '__main__':
    import os
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '8080'))
    except ValueError:
        PORT = 8080
    MLwebapp.run(HOST, PORT)

模板webpage1.html非常简单,只需一个用于测试系统的按钮:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Ciao</title>
</head>
<body>
    <h1>Test button2</h1>
    <form method="get" action="/test">
        <input type="submit" value="Press for test"/>
    </form>
</body>
</html>

关于Apache和WSGI的设置:
在httpd.conf中:

added  
LoadFile "c:/program files (x86)/microsoft visual studio/shared/python36_64/python36.dll"
LoadModule wsgi_module "c:/myapp/flask/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/myapp/flask"  
Removed the # from LoadModule cgi_module modules/mod_cgi.so  
added  
Listen 8080
Removed the # from conf/extra/httpd-vhosts.conf

在httpd-vhosts.conf中:

added  
<VirtualHost *:8080>
        ServerAdmin localhost
        ServerName  localhost:8080
        WSGIScriptAlias / "C:/myapp/app/web.wsgi"
        DocumentRoot "C:/myapp/app"
        <Directory "C:/myapp/app">
                Require all granted
        </Directory>
        ErrorLog "C:/myapp/app/logs/error.log"
        CustomLog "C:/myapp/app/logs/access.log" common
</VirtualHost>

在应用程序root中创建的Web.wsgi文件:

import sys
sys.path.insert(0, 'C:/myapp/app')
from app import MLwebapp as application

通过一些测试,我了解问题在于WSGI和Pickle之间的关系,但是我找不到指南或帖子,这可以帮助我解决我的问题。

前进的一种方式是排除泡菜是一个问题。一种简单的方法是捕获试图加载泡菜时发生的任何例外,并将其传递到起始模板中。像

try:
    left_model=pk.load(open('C:\myapp\app\left_classifier.pickle','rb'))
    left_model_ex = None
except Exception as ex:
    left_model = None
    left_model_ex = repr(ex)

(对于right_model而言相同(

left_model_ex传递到模板中,该模板做

之类的事情
{% if left_model_ex %}<div>{{ left_model_ex }}</div>{% endif %}

可以看到的地方。

相关内容

最新更新