Created
December 19, 2019 16:25
-
-
Save muratxs/8c6736939be84af66205f5085099b3c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Dec 19 15:13:01 2019 | |
@author: murat | |
""" | |
import numpy as np | |
from flask import Flask, request, jsonify, render_template | |
import pickle | |
#%% | |
app = Flask(__name__) | |
model = pickle.load(open("nb_model.pkl", "rb")) | |
#%% | |
@app.route('/') | |
def home(): | |
return render_template('index.html') | |
@app.route('/predict',methods=['POST']) | |
def predict(): | |
''' | |
For rendering results on HTML GUI | |
''' | |
int_features = [int(x) for x in request.form.values()] | |
final_features = [np.array(int_features)] | |
prediction = model.predict(final_features) | |
output = round(prediction[0], 2) | |
if output == 0: | |
return render_template('index.html', prediction_text = 'Üzgünüm! Kredi başvurunuz olumsuz sonuçlanmıştır.') | |
else: | |
return render_template('index.html', prediction_text = 'Harika! Kredi başvurunuz olumlu sonuçlanmıştır.') | |
@app.route('/predict_api',methods=['POST']) | |
def predict_api(): | |
''' | |
For direct API calls trought request | |
''' | |
data = request.get_json(force = True) | |
prediction = model.predict([np.array(list(data.values()))]) | |
output = prediction[0] | |
return jsonify(output) | |
if __name__ == "__main__": | |
app.run(debug=True) | |
#%% | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Dec 19 14:43:34 2019 | |
@author: murat | |
""" | |
#%% | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import pickle | |
#%% | |
df = pd.read_csv("kredi.csv", sep=";") | |
#%% | |
from pandas.api.types import CategoricalDtype | |
df["evDurumu"].replace("evsahibi",1, inplace = True) | |
df["evDurumu"].replace("kiraci",0, inplace= True) | |
df["telefonDurumu"].replace("var",1, inplace = True) | |
df["telefonDurumu"].replace("yok",0, inplace= True) | |
df["KrediDurumu"].replace("krediver",1, inplace = True) | |
df["KrediDurumu"].replace("verme",0, inplace= True) | |
#%% | |
y = df["KrediDurumu"] | |
X = df.drop(["KrediDurumu"], axis = 1) | |
#%% | |
from sklearn.naive_bayes import GaussianNB | |
nb = GaussianNB() | |
nb_model = nb.fit(X, y) | |
pickle.dump(nb_model, open("nb_model.pkl", "wb")) | |
#%% | |
model = pickle.load(open("nb_model.pkl", "rb")) | |
#print(model.predict([[12000, 70, 0,2,1]])) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Dec 19 15:21:23 2019 | |
@author: murat | |
""" | |
import requests | |
url = "http://localhost:5000/predict_api" | |
r = request.post(url, json = {"krediMiktari" : 1200, "yas" : 70, "evDurumu" : 0, "aldigi_kredi_sayi" : 2, "telefonDurumu" : 1}) | |
print(r.json()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Dec 19 14:43:34 2019 | |
@author: murat | |
""" | |
#%% | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import pickle | |
#%% | |
df = pd.read_csv("kredi.csv", sep=";") | |
#%% | |
from pandas.api.types import CategoricalDtype | |
df["evDurumu"].replace("evsahibi",1, inplace = True) | |
df["evDurumu"].replace("kiraci",0, inplace= True) | |
df["telefonDurumu"].replace("var",1, inplace = True) | |
df["telefonDurumu"].replace("yok",0, inplace= True) | |
df["KrediDurumu"].replace("krediver",1, inplace = True) | |
df["KrediDurumu"].replace("verme",0, inplace= True) | |
#%% | |
y = df["KrediDurumu"] | |
X = df.drop(["KrediDurumu"], axis = 1) | |
#%% | |
from sklearn.naive_bayes import GaussianNB | |
nb = GaussianNB() | |
nb_model = nb.fit(X, y) | |
pickle.dump(nb_model, open("nb_model.pkl", "wb")) | |
#%% | |
model = pickle.load(open("nb_model.pkl", "rb")) | |
#print(model.predict([[12000, 70, 0,2,1]])) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Dec 19 15:21:23 2019 | |
@author: murat | |
""" | |
import requests | |
url = "http://localhost:5000/predict_api" | |
r = request.post(url, json = {"krediMiktari" : 1200, "yas" : 70, "evDurumu" : 0, "aldigi_kredi_sayi" : 2, "telefonDurumu" : 1}) | |
print(r.json()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment