Apache或Flask是否需要特殊设置用于使用pickle的网站应用?

2024-07-01 07:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在visualstudio中,我用python和flask实现了一个webapi,它使用pickle加载一个SVM,整个系统已经部署在apache上。 我的问题是,当我尝试从visualstudio进行系统调试时,一切正常,但当我将其加载到apache服务器上时,页面只会加载,不会给出结果或错误。
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>

创建网站.wsgi应用程序根目录中的文件:

import sys

sys.path.insert(0, 'C:/myapp/app')

from app import MLwebapp as application

通过一些测试,我了解到问题在于wsgi和pickle之间的关系,但是我找不到一个指南或帖子来帮助我解决问题。你知道吗


Tags: fromimportrightjsonappflaskwsgiget
1条回答
网友
1楼 · 发布于 2024-07-01 07:12:14

一个前进的方向是排除泡菜是一个问题。一种简单的方法是捕获在尝试加载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 %}

可以看到的地方。你知道吗

相关问题 更多 >

    热门问题