Spaces:
Runtime error
Runtime error
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() | |