Skip to content

Commit

Permalink
feat: Add download pickle button
Browse files Browse the repository at this point in the history
  • Loading branch information
dekwahdimas committed Sep 12, 2024
1 parent 4d0c08b commit b9771df
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
16 changes: 8 additions & 8 deletions web_app/scripts/modeling_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def svm_pipeline(X_train, X_test, y_train, y_test):
cr_svm = classification_report(y_test, y_pred_svm, target_names=['negative', 'neutral', 'positive'])
cm_svm = confusion_matrix(y_test, y_pred_svm)

return cr_svm, cm_svm
return svm_pipeline, cr_svm, cm_svm


# K-Nearest Neighbor pipeline
Expand All @@ -37,7 +37,7 @@ def knn_pipeline(X_train, X_test, y_train, y_test):
cr_knn = classification_report(y_test, y_pred_knn, target_names=['negative', 'neutral', 'positive'])
cm_knn = confusion_matrix(y_test, y_pred_knn)

return cr_knn, cm_knn
return knn_pipeline, cr_knn, cm_knn


# Naive Bayes pipeline
Expand All @@ -53,7 +53,7 @@ def nb_pipeline(X_train, X_test, y_train, y_test):
cr_nb = classification_report(y_test, y_pred_nb, target_names=['negative', 'neutral', 'positive'])
cm_nb = confusion_matrix(y_test, y_pred_nb)

return cr_nb, cm_nb
return nb_pipeline, cr_nb, cm_nb


def modeling_and_evaluation(filepath, chosen_model):
Expand All @@ -64,14 +64,14 @@ def modeling_and_evaluation(filepath, chosen_model):

X = df["content"]
y = df["hard_label"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

if chosen_model == 'svm':
cr, cm = svm_pipeline(X_train, X_test, y_train, y_test)
pipeline, cr, cm = svm_pipeline(X_train, X_test, y_train, y_test)
elif chosen_model == 'knn':
cr, cm = knn_pipeline(X_train, X_test, y_train, y_test)
pipeline, cr, cm = knn_pipeline(X_train, X_test, y_train, y_test)
elif chosen_model == 'nb':
cr, cm = nb_pipeline(X_train, X_test, y_train, y_test)
pipeline, cr, cm = nb_pipeline(X_train, X_test, y_train, y_test)

return cr, cm
return pipeline, cr, cm
3 changes: 2 additions & 1 deletion web_app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Play Store Sentiment Analysis</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/2.1.5/css/dataTables.bootstrap5.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
<!-- Start navbar -->
Expand Down
8 changes: 8 additions & 0 deletions web_app/templates/features/modeling-evaluation.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ <h5 class="m-0 font-weight-bold text-center">{{ context['feature_title'] }}</h5>
</form>

{% if context["classification_report"] %}
<!-- Start button download pickle -->
<div style="text-align: right;">
<a href="{{ url_for('views.download_model') }}" class="btn btn-primary">
<i class="fa fa-download"></i> Download Pickle</a>
</div>
<!-- End button download pickle -->
<br>

<div class="row">
<div class="col">
<div class="card shadow md-4">
Expand Down
10 changes: 9 additions & 1 deletion web_app/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from datetime import datetime
import joblib

from flask import Blueprint, request, current_app, render_template, redirect, send_from_directory, url_for
from werkzeug.utils import secure_filename
from werkzeug.security import safe_join
import pytz

from .scripts.scraping_reviews import scrape_reviews
Expand Down Expand Up @@ -103,7 +105,10 @@ def modeling_evaluation():
if request.method == 'POST':
filepath = upload_file()
chosen_model = request.form['chosen_model']
cr, cm = modeling_and_evaluation(filepath, chosen_model)
pipeline, cr, cm = modeling_and_evaluation(filepath, chosen_model)
joblib.dump(pipeline, os.path.join(current_app.config['UPLOAD_FOLDER'], 'model_pipeline.pkl'))
# safe_path = safe_join(os.path.join(current_app.config['UPLOAD_FOLDER'], 'pickle'))
# joblib.dump(pipeline, safe_path, 'model_pipeline.pkl')

context = {
'feature_title': feature_title,
Expand All @@ -115,6 +120,9 @@ def modeling_evaluation():

return render_template("features/modeling-evaluation.html", context={'feature_title': feature_title})

@views.route('/download_model')
def download_model():
return send_from_directory(current_app.config['UPLOAD_FOLDER'], 'model_pipeline.pkl')

@views.route('/prediction')
def prediction():
Expand Down

0 comments on commit b9771df

Please sign in to comment.