def script(): client1 = request.form['client1'] client2 = request.form['client2'] legal1 = request.form['legal1'] legal2 = request.form['legal2'] from_d = request.form['from'] #to = request.form['to']; #df1=[] #df2=[] df1 = model_call(str(client1), str(legal1)) df2 = model_call(str(client2), str(legal2)) #dataframe1= pd.DataFrame(df1) #dataframe2 = pd.DataFrame(df2) #temp_df={'A':df1 , 'B':df2} #combined_df = pd.DataFrame(temp_df) #return render_template('test.html', df1=dataframe1, df2=dataframe2) #return myfile.getAns(from_d) start = from_d #end = to #fig1 = px.line(x = df1[start:end].index, y = df1[start:end], title = 'Past trend') #fig2 = px.line( x = df2[start:end].index,y = df2[start:end] , title='past trend') fig = make_subplots(rows=1, cols=2) fig.add_trace(go.Scatter(x=df1[start:].index, y=df1[start:], mode="lines"), row=1, col=1) fig.add_trace(go.Scatter(x=df2[start:].index, y=df2[start:], mode="lines"), row=1, col=2) pio.write_html(fig, file='F:/project3_frontend/templates/output.html', auto_open=False) return render_template('output.html')
def script(): client1 = request.form['client1']; client2 = request.form['client2']; legal1 = request.form['legal1']; legal2 = request.form['legal2']; from_d = request.form['from']; start = from_d df1 = model_call(str(client1),str(legal1)) model = pickle.load(open('model.pkl', 'rb')) lastdate_1=give_last_date(client1, legal1) pred1=model.forecast(6) pred1_mean=round(pred1.mean(), 3) show_predict1=np.array(df1[str(datetime.datetime.strptime(str(lastdate_1),'%Y%m%d').date())]) show_predict1=np.append(show_predict1, pred1) df2 = model_call(str(client2),str(legal2)) model = pickle.load(open('model.pkl', 'rb')) lastdate_2=give_last_date(client2, legal2) pred2=model.forecast(6) pred2_mean=round(pred2.mean(), 3) print(lastdate_2) print('.............................................................') show_predict2=np.array(df2[str(datetime.datetime.strptime(str(lastdate_2),'%Y%m%d').date())]) show_predict2=np.append(show_predict2, pred2) print(type(show_predict2)) #fig = make_subplots(rows=1, cols=2) table_col=['Clients', 'Legal Entity', 'Mean of Predicted Paid Amount (USD)'] table_title="" if pred1_mean > pred2_mean : table_row=[[client1, client2], [legal1, legal2], [pred1_mean, pred2_mean]] table_title="Client 1 and Legal Entity 1 are expected to do better business based on predicted mean amount" else : table_title="Client 2 and Legal Entity 2 are expected to do better business based on predicted mean amount " table_row=[[client2, client1], [legal2, legal1], [pred2_mean, pred1_mean]] fig = make_subplots(rows=2, cols=1, vertical_spacing=0.03,specs=[ [{"type": "table"}],[{"type": "scatter"}] ] ) fig.add_trace(go.Table(header=dict(values=table_col,font=dict(size=10),align="left"), cells=dict(values=table_row, height=40,align="left")), row=1, col=1) fig.add_trace(go.Scatter(x =df1[start:].index,y=df1[start:],mode='lines',name='Recorded trend 1'), row=2, col=1) fig.add_trace(go.Scatter(x=give_dates(lastdate_1),y=show_predict1,mode='lines',name='Predicted trend 1',line=dict(width=4, dash='dot')), row=2, col=1) fig.add_trace(go.Scatter(x = df2[start:].index, y=df2[start:], mode='lines', name='Recorded Trend 2'), row=2, col=1) fig.add_trace(go.Scatter(x=give_dates(lastdate_2), y=show_predict2, mode='lines', name='Predicted trend 2', line=dict(width=4, dash='dot')), row=2, col=1) fig.update_yaxes(title_text="Paid Amount", row=2, col=1) fig.update_xaxes(title_text='Dates', row=2, col=1) fig.update_layout(title_text=table_title) pio.write_html(fig, file='templates/output.html', auto_open=False) return render_template('output.html')
def plot_com(client1, client2, legal1, legal2, from_d): start = from_d df1 = model_call(str(client1), str(legal1)) model = pickle.load(open('model.pkl', 'rb')) lastdate_1 = give_last_date(client1, legal1) pred1 = model.forecast(6) show_predict1 = np.array(df1[str( datetime.datetime.strptime(str(lastdate_1), '%Y%m%d').date())]) show_predict1 = np.append(show_predict1, pred1) df2 = model_call(str(client2), str(legal2)) model = pickle.load(open('model.pkl', 'rb')) lastdate_2 = give_last_date(client2, legal2) pred2 = model.forecast(6) print(lastdate_2) print('.............................................................') show_predict2 = np.array(df2[str( datetime.datetime.strptime(str(lastdate_2), '%Y%m%d').date())]) show_predict2 = np.append(show_predict2, pred2) print(type(show_predict2)) #fig = make_subplots(rows=1, cols=2) fig = go.Figure() fig.add_trace( go.Scatter(x=df1[start:].index, y=df1[start:], mode='lines', name='Recorded trend 1')) fig.add_trace( go.Scatter(x=give_dates(lastdate_1), y=show_predict1, mode='lines', name='Predicted trend 1', line=dict(width=4, dash='dot'))) fig.add_trace( go.Scatter(x=df2[start:].index, y=df2[start:], mode='lines', name='Recorded Trend 2')) fig.add_trace( go.Scatter(x=give_dates(lastdate_2), y=show_predict2, mode='lines', name='Predicted trend 2', line=dict(width=4, dash='dot'))) fig.update_yaxes(title_text="Paid Amount") fig.update_xaxes(title_text='Dates') pio.write_html(fig, file='templates/output.html', auto_open=False)
def plot_pred(cname, lename, from_d): df = model_call(str(cname), str(lename)) model = pickle.load(open('model.pkl', 'rb')) lastdate_ = give_last_date(cname, lename) pred = model.forecast(6) pred_mean = round(pred.mean(), 3) start = from_d show_predict = np.array(df[str( datetime.datetime.strptime(str(lastdate_), '%Y%m%d').date())]) show_predict = np.append(show_predict, pred) fig = go.Figure() fig.add_trace( go.Scatter(x=df[start:].index, y=df[start:], mode='lines', name='Recorded')) fig.add_trace( go.Scatter(x=give_dates(lastdate_), y=show_predict, mode='lines', name='Predicted', line=dict(width=4, dash='dot'))) final_verdict = '' if pred_mean > 0: final_verdict = "Mean paid amount of next 6 months is {m}.<br>So, considering this it is beneficial to work with this client.".format( m=pred_mean) else: final_verdict = "Mean paid amount of next 6 months is {m}.<br>So, considering this it is not beneficial to work with this client.".format( m=pred_mean) fig.update_layout(title_text=final_verdict) fig.update_yaxes(title_text="Paid Amount") fig.update_xaxes(title_text='Dates') pio.write_html(fig, file='templates/predict.html', auto_open=False)
def plot_pred(cname, lename, from_d): df = model_call(str(cname), str(lename)) model = pickle.load(open('model.pkl', 'rb')) lastdate_ = give_last_date(cname, lename) pred = model.forecast(6) start = from_d show_predict = np.array(df[str( datetime.datetime.strptime(str(lastdate_), '%Y%m%d').date())]) show_predict = np.append(show_predict, pred) fig = go.Figure() fig.add_trace( go.Scatter(x=df[start:].index, y=df[start:], mode='lines', name='Recorded')) fig.add_trace( go.Scatter(x=give_dates(lastdate_), y=show_predict, mode='lines', name='Predicted', line=dict(width=4, dash='dot'))) slope = pred[-1] - df.iloc[-1] final_verdict = '' if slope > 0: final_verdict = "Beneficial to work with this client" else: final_verdict = "Not beneficial to work with this client" fig.update_layout(title_text=final_verdict) fig.update_yaxes(title_text="Paid Amount") fig.update_xaxes(title_text='Dates') pio.write_html(fig, file='templates/predict.html', auto_open=False)
def output(): value_list = request.form.values() df = model_call(str(value_list[0]), str(value_list[1])) start = datetime.date(value_list[2]) end = datetime.date(value_list[3]) fig = px.line(x=df[start:end].index, y=df[start:end], title='Past trend') pio.write_html(fig, file='G:/Deploying Model/templates/output.html', auto_open=False) return render_template( 'output.html', output_text= 'This is the graph for predicted values of given time interval : ')
def predict(): fig = go.Figure() cname = request.form['cname'] lename = request.form['lename'] from_d = request.form['from'] df = model_call(str(cname), str(lename)) start = from_d fig.add_trace( go.Scatter(x=df[start:].index, y=df[start:], mode='lines', name='Recorded Trend')) model = pickle.load(open('model.pkl', 'rb')) with open("F:/project3_frontend/dataset.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) last_date = "" for row in csvreader: if row[1] == cname and row[2] == lename: last_date = row[0] lastdate_ = "" for w in last_date: if w != '-': lastdate_ += w pred = model.predict(start=datetime.datetime.strptime( str(lastdate_), '%Y%m%d').date(), end=date.today() + relativedelta(months=+6)) #pred = model.predict(start=date.today(), end=date.today() + relativedelta(months=+6)) start = date.today() fig.add_trace( go.Scatter(x=pred[start:].index, y=pred[start:], mode='lines', name='Predicted trend')) pio.write_html(fig, file='F:/project3_frontend/templates/predict.html', auto_open=False) slope = pred[-1] - pred[0] return render_template('predict.html') output = "" if slope > 0 else "not " return render_template( 'predict.html', prediction_text= 'This is the graph for predicted values of 6 months from now. It is {}beneficial to work with this client' .format(output))