前提・実現したいこと
初心者です。Python機械学習プログラミング[第2版]で勉強中です。
第9章 9.4.1で映画レビュー分類アプリケーションを作成しました。
ローカルサーバーで起動したらうまくいったのですが、
PythonAnywhereにファイルをアップロードしてWebを見ようとすると、
以下のエラーログが出ます。
発生している問題・エラーメッセージ
2018-09-23 12:36:22,227: Error running WSGI application
2018-09-23 12:36:22,232: FileNotFoundError: [Errno 2] No such file or directory: '/home/1976kaca/pkl_objects/stopwords.pkl'
2018-09-23 12:36:22,233: File "/var/www/1976kaca_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-09-23 12:36:22,233: from movieclassifier import app as application # noqa
2018-09-23 12:36:22,233:
2018-09-23 12:36:22,233: File "/home/1976kaca/movieclassifier/app.py", line 8, in <module>
2018-09-23 12:36:22,233: from vectorizer import vect
2018-09-23 12:36:22,233:
2018-09-23 12:36:22,234: File "/home/1976kaca/vectorizer.py", line 10, in <module>
2018-09-23 12:36:22,234: 'stopwords.pkl'), 'rb'))
app.pyのソースコードは以下です。
from flask import Flask, render_template, request
from wtforms import Form, TextAreaField, validators
import pickle
import sqlite3
import os
import numpy as np
from vectorizer import vect
app = Flask(name)
cur_dir = os.path.dirname(file)
clf = pickle.load(open(os.path.join(cur_dir, 'pkl_objects', 'classifier.pkl'),
'rb'))
db = os.path.join(cur_dir, 'reviews.sqlite')
def classify(document):
label = {0: 'negative', 1: 'positive'}
X = vect.transform([document])
y = clf.predict(X)[0]
proba = clf.predict_proba(X).max()
return label[y], proba
def train(document, y):
X = vect.transform([document])
clf.partial_fit(X, [y])
def sqlite_entry(path, document, y):
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute("INSERT INTO review_db (review, sentiment, date)"
" VALUES (?, ?, DATETIME('now'))", (document, y))
conn.commit()
conn.close()
class ReviewForm(Form):
moviereview = TextAreaField('',
[validators.DataRequired(),
validators.length(min=15)])
@app.route('/')
def index():
form = ReviewForm(request.form)
return render_template('reviewform.html', form=form)
@app.route('/results', methods=['POST'])
def results():
form = ReviewForm(request.form)
if request.method == 'POST' and form.validate():
review = request.form['moviereview']
y, proba = classify(review)
return render_template('results.html',
content=review,
prediction=y,
probability=round(proba*100, 2))
return render_template('reviewform.html', form=form)
@app.route('/thanks', methods=['POST'])
def feedback():
feedback = request.form['feedback_button']
review = request.form['review']
prediction = request.form['prediction']
inv_label = {'negative': 0, 'positive': 1} y = inv_label[prediction] if feedback == 'Incorrect': y = int(not(y)) train(review, y) sqlite_entry(db, review, y) return render_template('thanks.html')
if name == 'main':
app.run()
試したこと
movieclassifierディレクトリをベースとして、動くアプリケーションのはずが、
1976kacaディレクトリからpkl_objectsや、vectorizer.pyを読もうとして失敗していると
読み解いたのですが。
WEb上でも
movieclassifier
|_app.py
|_pkl_objects
| |_classifier.pkl
| |_stopwords.pkl
|_reviews.sqlite
|_static
| |_style.css
|_templates
........
とフォルダーの配置などは同じにしてあるのですが、何が間違っているのでしょうか?
教えていただければ幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/26 13:18
2018/09/26 13:25
2018/09/26 13:31
2018/09/26 13:36 編集
2018/09/26 14:03 編集
2018/09/26 14:01
2018/09/27 12:57 編集
2018/09/26 14:27
2018/09/27 12:03 編集