0

我正在使用 Flask Reuploaded 上传多个文件作为输入。for 循环已经有 return 语句。

错误在 POST 方法中:

TypeError:“upload_image”的视图函数未返回有效响应。该函数要么返回 None ,要么在没有 return 语句的情况下结束。

我不确定我在这里缺少什么以及缺少什么。

主文件

@app.route('/', methods=['POST'])
def upload_image():
    if request.method == 'POST':
        # checks whether or not the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)

        file = request.files['file']

        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No file selected for uploading')
            return redirect(request.url)
        files = request.files.getlist('files[]')
        for file in files:
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(os.getcwd() +
                                   UPLOAD_INPUT_IMAGES_FOLDER, file.filename))

            flash('File successfully uploaded')


            # calls the ocr_processing function to perform text extraction
            extracted_text = ocr_processing(file)
            print(extracted_text)
            match = extracted_text.lower()
            df = pd.read_csv("/Users/main/tl.csv")

            for row in df.Pattern_String:
                # result = ratio(row.lower(), match)
                result = ratio(row, match)
                print(result)
                if result >= 10:
                    loaded_vec = CountVectorizer(
                        vocabulary=pickle.load(open("model/tfidf_vector.pkl", "rb")))
                    loaded_tfidf = pickle.load(open("model/tfidf_transformer.pkl", "rb"))
                   #load and match other patterns
                    X_new_counts = loaded_vec.transform(
                        match)
                    X_new_tfidf = loaded_tfidf.transform(X_new_counts)

                    predicted_pattern_type = model_pattern_type.predict(X_new_tfidf)
                    your_predicted_pattern_type = predicted_pattern_type[0]

                    predicted_pattern_category = model_pattern_category.predict(
                        X_new_tfidf)
                    your_predicted_pattern_category = predicted_pattern_category[0]

                    return render_template('uploads/results.html',
                                           msg='Processed successfully!',
                                           match=match,
                                           your_predicted_pattern_category=your_predicted_pattern_category,
                                           your_predicted_pattern_type=your_predicted_pattern_type,
                                           img_src=UPLOAD_INPUT_IMAGES_FOLDER + file.filename)
                else:
                    return render_template('uploads/results.html',
                                           msg='Processed successfully!',
                                           match=match,
                                           img_src=UPLOAD_INPUT_IMAGES_FOLDER + file.filename)


    else:
        flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
        return redirect(request.url)
4

0 回答 0