无法使用 Python 让语义内核在 Azure Web 应用程序中工作

问题描述 投票:0回答:1

我正在尝试使用 Python 让语义内核在 Azure Web 应用程序中工作。我正在使用 VS 代码。每次我“部署到 Web 应用程序”时,其中有一个包含以下内容的 Python 脚本: from Semantic_kernel import Kernel,当我尝试访问我的 Azure Web 应用程序时,我会收到一条错误消息,显示“:( 应用程序错误” .

我已经确认我的 requiremnets.txt 文件中有正确的包(见下文),我尝试使用安装的本地依赖项进行部署,我使用的是 Python 3.10,这就是 Azure 环境所使用的。语义内核在本地工作得很好,但在 Azure 中却不行。如果我删除与语义内核相关的所有行并重新部署,我可以再次访问我的 Azure Web 应用程序,当然无需语义内核功能。

需求.txt:

Flask==3.0.3
PyMuPDF==1.24.13
Werkzeug==3.0.6
openai==1.55.0
azure-core==1.31.0
azure-ai-formrecognizer==3.3.3
pytesseract==0.3.13
Pillow==11.0.0
beautifulsoup4==4.12.3
requests==2.32.3
urllib3==2.2.3
pandas==2.2.3
openpyxl==3.1.5
semantic-kernel==1.12.1
azure-storage-blob==12.24.0
nest-asyncio==1.6.0

以下是我的app.py代码。其中唯一相关的部分是 pdf_comparison.py 代码。其他一切都工作正常,直到我将“导入语义_内核”添加到我的 pdf_comparison.py 文件中(也在下面)。

应用程序.py

from flask import Flask, request, render_template, send_from_directory, jsonify
from werkzeug.utils import secure_filename
import os
from wage_and_apprenticeship import extract_pages
from statement_of_values import sov_main
from payment_summary import payment_summary_main
from address_search import process_file
from pdf_comparison import main_pdf_comparison
import base64
import sys

sys.path.insert(0, "./package")

app = Flask(__name__)

UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER



#HOME PAGE
@app.route('/')
def home_page():
    return render_template('index.html')



#ADDRESS SEARCH
@app.route('/address_search')
def address_search():
    return render_template('address_search.html')

@app.route('/address_search_upload', methods=['POST'])
def address_search_upload():
    uploaded_file = request.files['file']
    if uploaded_file:
        filename = uploaded_file.filename
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        uploaded_file.save(file_path)

        updated_file_path = process_file(file_path, filename)

        # Read the processed CSV file content
        with open(updated_file_path, 'r') as csv_file:
            csv_content = csv_file.read()

        # Send the CSV content to the frontend as JSON
        return jsonify({'filename': os.path.basename(updated_file_path), 'csv_data': csv_content})

    return 'No file uploaded', 400



#STATEMENT OF VALUES
@app.route('/statement_of_values')
def statement_of_values_page():
    return render_template('statement_of_values.html')

@app.route('/sov_upload', methods=['POST'])
def sov_upload():
    if 'file' not in request.files:
        return 'No file part'
    file = request.files['file']
    if file.filename == '':
        return 'No selected file'
    if file:
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        image_reply_pairs = sov_main(file_path)
        image_paths = [f'/uploads/{os.path.basename(path)}' for path in image_reply_pairs.keys()]
        csv_data = [reply for reply in image_reply_pairs.values()]
        return jsonify({'image_paths': image_paths, 'csv_data': csv_data})



#WAGE & APPRENTICESHIP
@app.route('/wage_and_apprenticeship')
def wage_and_apprenticeship_page():
    return render_template('wage_and_apprenticeship.html')

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return 'No file part'
    file = request.files['file']
    if file.filename == '':
        return 'No selected file'
    if file:
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        image_reply_pairs = extract_pages(file_path)
        image_paths = [f'/uploads/{os.path.basename(path)}' for path in image_reply_pairs.keys()]
        csv_data = [reply for reply in image_reply_pairs.values()]
        return jsonify({'image_paths': image_paths, 'csv_data': csv_data})



#PAYMENT SUMMARY
@app.route('/payment_summary')
def payment_summary():
    return render_template('payment_summary.html')

@app.route('/payment_summary_upload', methods=['POST'])
def payment_summary_upload():
    if 'file' not in request.files:
        return 'No file part'
    file = request.files['file']
    if file.filename == '':
        return 'No selected file'
    if file:
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(file_path)
        image_reply_pairs = payment_summary_main(file_path)
        image_paths = [f'/uploads/{os.path.basename(path)}' for path in image_reply_pairs.keys()]
        csv_data = [reply for reply in image_reply_pairs.values()]
        return jsonify({'image_paths': image_paths, 'csv_data': csv_data})



#PDF COMPARISON
@app.route('/pdf_comparison')
def pdf_comparison():
    return render_template('pdf_comparison.html')

@app.route('/pdf_comparison_upload', methods=['POST'])
def pdf_comparison_upload():
    prior_year_file = request.files['priorYearPDF']
    current_year_file = request.files['currentYearPDF']

    prior_year_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(prior_year_file.filename))
    current_year_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(current_year_file.filename))
    
    prior_year_file.save(prior_year_path)
    current_year_file.save(current_year_path)

    try:
        annotated_pdf_path = main_pdf_comparison(prior_year_path, current_year_path)

        # Read the processed PDF file content
        with open(annotated_pdf_path, 'rb') as pdf_file:
            pdf_content = pdf_file.read()

        # Encode the PDF content to Base64
        encoded_pdf = base64.b64encode(pdf_content).decode('utf-8')

        # Send the Base64-encoded PDF content to the frontend as JSON
        return jsonify({'filename': os.path.basename(annotated_pdf_path), 'pdf_data': encoded_pdf})
    except FileNotFoundError as e:
        return jsonify({"error": str(e)}), 500
    except Exception as e:
        return jsonify({"error": f"Processing failed: {str(e)}"}), 500



#FILE UPLOAD
@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

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

pdf_comparison.py

import fitz 
import asyncio
import ast
import semantic_kernel #THIS IS THE PROBLEM. 
import os

import nest_asyncio
nest_asyncio.apply()

def main_pdf_comparison():
    print("Hello World")
python azure azure-web-app-service semantic-kernel
1个回答
0
投票

我已经尝试了您的代码并成功将其部署到 Azure Web App,没有任何问题。

。版本不兼容 (pydantic):当前版本 (2.9.2) 与语义内核要求冲突。

  • 您遇到的问题是因为版本不兼容,我使用时遇到了同样的错误
    semantic-kernel==1.12.1
  • 当您使用
    pydantic==2.9.2
    时,您需要使用
    semantic-kernel==1.16.0
    来实现兼容性。
  • 内核可能在本地计算机上运行良好,因为设置不同,但是当您将其部署到 Azure 时,它可能需要一些调整才能顺利运行,特别是对于在后台运行的任务。
  • 所以,我将语义内核更新为
    1.16.0

我的requirements.txt

Flask==3.0.3
PyMuPDF==1.24.13
Werkzeug==3.0.6
openai==1.55.0
azure-core==1.31.0
azure-ai-formrecognizer==3.3.3
pytesseract==0.3.13
Pillow==11.0.0
beautifulsoup4==4.12.3
requests==2.32.3
urllib3==2.2.3
pandas==2.2.3
openpyxl==3.1.5
semantic-kernel==1.16.0
azure-storage-blob==12.24.0
nest-asyncio==1.6.0
gunicorn==21.2.0
pydantic==2.9.2

本地输出:

enter image description here

更新包后,我可以成功将应用程序部署到Azure Web App。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.