【问题标题】:Classify Multiple Documents and store them in different folders Via Flask (python)通过 Flask (python) 对多个文档进行分类并将它们存储在不同的文件夹中
【发布时间】:2019-02-07 18:38:17
【问题描述】:

我想做的是我希望我的网络应用程序将多个文档作为输入,并使用我的模型对它们进行分类,并将这些分类的文档存储到不同的文件夹中。

我开发了一个对文档进行分类的模型。模型已准备就绪,准确度约为 0.96 f-score。我想在烧瓶中实现它。我已经在显示准确结果的文本输入上实现了。

((#LIBRARIES
app = Flask(__name__)

app.config.from_object(__name__) # load config from th

app.config['UPLOAD_FOLDER'] = 'CV_upload/'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 
    'jpeg', 'gif'])

# Route for handling the login page logic
@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != 'admin' or 
    request.form['password'] != 'admin':
            error = 'Invalid Credentials. Please try again.'
        else:
            return redirect(url_for('home'))
    return render_template('index.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('home'))


@app.route('/home')
def home():
    return render_template('home.html')

@app.route('/predict',methods=['POST'])
def predict():
    <<<<<<<<<<<<<<#MY_NAIVE_BAYES MODEL 
   HERE>>>>>>>>>>>>>>>>>>>>>>>>

<<<<<<<<<<<<<<<<<<THis is where i take input text but i want this to 
   change as input multiple pdf files and then classify them>>>>>>>>
                if request.method == 'POST':
            message = request.form['message']
            data = [message]
            vect = vectorizer.transform(data).toarray()
            my_prediction = model.predict(vect)
            return render_template('result.html',prediction = my_prediction)



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

我想做的是我希望我的网络应用程序将多个文档作为输入,并使用我的模型对它们进行分类,并将这些分类的文档存储到不同的文件夹中。放置查询时,它将生成结果。

【问题讨论】:

  • 您发布的请求到底是什么样的?它只是 pdf 名称(您将实际文件存储在服务器上),还是 pdf 文件的完整 blob?
  • 我希望在我的服务器上加载 500 到 1000 个文件,然后我的模型将使用它来分类,然后存储在不同的类中
  • 那么您的问题是关于案例 1 还是案例 2?你已经有这些文件了吗?或者您希望用户上传它?案例 1:如何创建 POST 烧瓶路由接受/上传多个文件?案例 2:如何在 pdf 文件上运行模型?
  • 好的,仔细阅读......
  • 在那之后我想要我已经开发的模型工作正常(直接输入文本/上传文本并告诉正确的类)..我希望它也适用于多个文件并将它们分类为不同的类。(模型已经在不同的类上训练过。我想知道的是,当文件上传时(您将告诉如何上传,然后将它们转换为文本文件)之后我将如何链接这些文件到我的模型中,它将每个文件分类到一个类并将其存储在将在类名的帮助下创建的文件夹中。

标签: python flask document text-classification


【解决方案1】:

使用FileUploads 的最小解决方案

import os

from flask import Flask, request
from werkzeug.utils import secure_filename

app = Flask(__name__)

def classify(bindata):
    #this is a pdf file, extract text and run your model
    return "abc"

def ensure_dirs(path):
    if not os.path.exists(path):
        os.makedirs(path)

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        for fileobj in request.files.getlist('file'):
            print "Found {}".format(fileobj.filename)
            binary_data = fileobj.stream.read()
            file_class = classify(binary_data)
            path = "uploads/{}".format(file_class)
            ensure_dirs(path)
            fd = open(os.path.join(path, fileobj.filename), "wb")
            fd.write(binary_data)
            fd.close()
            fileobj.close()
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data>
      <input type=file name=file multiple>
      <input type=submit value=Upload>
    </form>
    '''

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

【讨论】:

  • 问题是如何将它们作为输入。 ive 模块进行转换。但问题是..我将如何让我的网络应用程序将它们作为输入,并使用我设计的模型将它们分类到不同的类中,然后将它们存储在不同的文件夹中
  • 所以你想让网页“上传多个文件”?
猜你喜欢
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
  • 2021-10-15
  • 2018-01-26
  • 1970-01-01
相关资源
最近更新 更多