File size: 5,113 Bytes
1ee5c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pandas as pd
import streamlit as st

from Functionalities.Streamlit_helpers import filter_dataframe
from Functionalities.KeywordAdgroupPredict import KeywordAdgroupPredict


class NewKeywordAdGrouper:

    def __init__(self):
        self.new_kw_df = None
        self.cur_kw_df = None
        self.selected_features = None
        self.features = None
        self.cur_kw_file_camp_col = None
        self.cur_kw_file_adg_col = None
        self.new_kw_file = None
        self.cur_kw_file = None
        self.new_kw_file_kw_col = None
        self.cur_kw_file_kw_col = None
        st.session_state.predict_df = pd.DataFrame() \
            if 'predict_df' not in st.session_state else st.session_state.predict_df

        st.set_page_config(page_title='Google Ads Keyword Ad grouper', layout="wide")
        st.header("Google Ads Keyword Ad grouper")
        st.write(f"This page tries to predict the suitable ad group for new keywords "
                 f"based on the keywords already existing in the campaign.")
        st.write(f"Please make sure you DON'T have the following column names in the New Keyword CSV\n"
                 f"1. Recommended Ad group\n2. relevance score")

    def input_file_features(self) -> None:
        """
        Takes 2 file inputs, 1 feature input using checkboxes, 1 button for running prediction
        :return:
        """
        self.new_kw_file = st.file_uploader(label="Upload the CSV file containing the new keywords")

        if self.new_kw_file:
            self.new_kw_df = pd.read_csv(self.new_kw_file)
            if 'Recommended Ad group' in self.new_kw_df.columns or 'relevance score' in self.new_kw_df.columns:
                st.error(f"Please make sure you DON'T have the following column names in the New Keyword CSV\n"
                         f"1. Recommended Ad group\n2. relevance score")
            self.new_kw_file_kw_col = st.selectbox(
                label=f"Select the Keyword Column in **{self.new_kw_file.name}**", options=self.new_kw_df.columns
            )

        self.cur_kw_file = st.file_uploader(label="Upload the CSV file containing the current keywords, "
                                                  "their ad group, and campaign (Search keyword report)")

        if self.cur_kw_file:
            self.cur_kw_df = pd.read_csv(self.cur_kw_file)
            self.cur_kw_file_kw_col = st.selectbox(label=f"Select the Keyword Column in **{self.cur_kw_file.name}**",
                                                   options=self.cur_kw_df.columns, index=0)
            self.cur_kw_file_adg_col = st.selectbox(label=f"Select Ad Group Column in **{self.cur_kw_file.name}**",
                                                    options=self.cur_kw_df.columns, index=3)
            self.cur_kw_file_camp_col = st.selectbox(label=f"Select Campaign Column in **{self.cur_kw_file.name}**",
                                                     options=self.cur_kw_df.columns, index=2)

        if self.new_kw_file and self.cur_kw_file:
            self.features = {self.cur_kw_file_kw_col: False, self.cur_kw_file_adg_col: False}
            st.write('Select the features you want to use for finding relevant ad group for new keywords:')
            self.features[self.cur_kw_file_kw_col] = st.checkbox('Ad group Keywords', value=True)
            self.features[self.cur_kw_file_adg_col] = st.checkbox('Ad group name')

            self.selected_features = [key for key, value in self.features.items() if value == True]

            st.button("Predict", on_click=self.run_prediction)
        else:
            st.info("Please upload necessary files")

    def run_prediction(self) -> None:
        """
        runs ad group prediction for given inputs on button press
        :return:
        """
        if not (self.features[self.cur_kw_file_kw_col] or self.features[self.cur_kw_file_adg_col]):
            st.error("Please select at least one feature")
        else:
            kw_adg_predict = KeywordAdgroupPredict()
            st.session_state.predict_df = kw_adg_predict.predict_ad_group_for_keywords(
                candidate_kw_df=self.new_kw_df, cur_kw_df=self.cur_kw_df, features=self.selected_features,
                new_kw_col=self.new_kw_file_kw_col, cur_kw_col=self.cur_kw_file_kw_col,
                cur_adg_col=self.cur_kw_file_adg_col, cur_camp_col=self.cur_kw_file_camp_col)

    def output(self) -> None:
        """
        Outputs the dataframe containing ad group prediction and relevance score
        :return:
        """
        if not st.session_state.predict_df.empty:
            filtered_df = filter_dataframe(st.session_state.predict_df)
            st.dataframe(filtered_df)
            st.download_button(
                "Press to Download",
                filtered_df.to_csv(index=False).encode('utf-8'),
                "Keyword Ad group Prediction.csv",
                "text/csv",
                key='download-csv'
            )


if __name__ == '__main__':
    new_kw_ad_grouper = NewKeywordAdGrouper()
    new_kw_ad_grouper.input_file_features()
    new_kw_ad_grouper.output()