def admin_upload(filename=None): if request.method == 'POST': if 'file' not in request.files: flash("No file part") return redirect(request.url) file = request.files['file'] if file.filename == '': flash("No selected file") return redirect(request.url) if file and allowed_file(file.filename): message = None filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) missing_values = [ "no info", ".", "n/a", "na", "--", "N/A", "NA", "??", "?" ] df = pd.read_csv(file_path, encoding='latin', sep=',', delimiter=None, header=0, index_col=None, squeeze=False, engine='python', usecols=range(0, 3), na_values=missing_values) df['Post'].fillna(method="ffill", inplace=True) df['Comment'].fillna(method="ffill", inplace=True) df['Sentiment'].fillna(method="ffill", inplace=True) y = df['Sentiment'] # print(y) df = df.drop('Sentiment', axis=1) # print(df) X_train = final_transformer.transform(df) log_model = clf.fit(X_train, y, sample_weight=None) # Save the model filename = 'finall_modell.sav' pi.dump(log_model, open(filename, 'wb')) if filename: message = 'model updated successfully!!' return render_template('adminupdate.html', message=message) else: message = 'model not updated!!' return render_template('adminupdate.html', message=message) else: message = 'file not allowed,choose a csv file. model not updated' return render_template('adminupdate.html', message=message) return render_template('adminupdate.html', filename=filename)
def update_model(db_path, model, batch_size=100): conn = sqlite3.connect("sample_db.sqlite") query = "SELECT * FROM sent;" results = pd.read_sql_query(query, conn) df = results.drop('Sentiment', axis=1) y = results['Sentiment'] X_train = final_transformer.transform(df) clf.fit(X_train, y, sample_weight=None) conn.close() return None
def main(): if request.method == 'GET': return render_template('main.html') if request.method == 'POST': Post = request.form['Post'] Comment = request.form['Comment'] df = pd.DataFrame([[Post, Comment]], columns=['Post', 'Comment'], dtype=str, index=['input']) vec = final_transformer.transform(df) my_prediction = clf.predict(vec) return render_template('main.html', original_input={'Post': Post, 'Comment': Comment}, prediction=my_prediction, post=Post, comment=Comment)
def upload_file1(filename=None, column=None, data=None, my_prediction=None, df=None): UPLOAD_FOLDER = 'C:/Users/hamza/PycharmProjects/exercise/try' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER if request.method == 'POST': if 'file' not in request.files: flash("No file part") return redirect(request.url) file = request.files['file'] if file.filename == '': flash("No selected file") return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) missing_values = ["no info", ".", "n/a", "na", "--", "N/A", "NA", "??", "?"] df = pd.read_csv(file_path, encoding='ISO-8859-1', sep=',', delimiter=None, header=0, index_col=None, squeeze=False, engine='python', usecols=range(0, 2),na_values=missing_values) df.columns = ['Post', 'Comment'] df['Post'].fillna(method="ffill", inplace=True) df['Comment'].fillna(method="ffill", inplace=True) #df['Sentiment'].fillna(method="ffill", inplace=True) vec = final_transformer.transform(df) my_prediction = clf.predict(vec) df['Sentiment'] = my_prediction df['Sentiment'].value_counts().plot(kind='bar', title='Sentiment of classification') plt.savefig("image.png") line_labels, line_values = creategraph(df['Sentiment']) desc = df.describe(include='all') return render_template('bar_chart.html', title='Bar graph showing Sentiment Analysis', max=1000, labels=line_labels, values=line_values, df=df.to_html(), stat=desc.to_html(), csv=df.to_csv()) return render_template('upload1.html', filename=filename, data=data, column=column, predict=my_prediction, df=df)
def train(df, y): X = final_transformer.transform(df) clf.fit(X, y, sample_weight=None)