INNER CODE UNIT · Python
id_response_parser
google-marketing-solutions/feedgen · enrichment/src/main.py:106
def id_response_parser(request):
for contents in request['contents']:
for parts in contents['parts']:
if parts["text"].startswith("Id: "):
current_id = parts["text"].replace("Id: ", "")
return current_id
return "N/A" # for good practice, but Id is always present in request
df_category[target_column_name] = df_category['response'].apply(category_response_parser)
df_category[id_column_name] = df_category['request'].apply(id_response_parser)
df = df.set_index(id_column_name).join(df_category.set_index(id_column_name))
df.drop(columns=['status', 'processed_time','request', 'response'], axis=1, inplace=True)
current_folder = last_path.replace("predictions.jsonl", "").replace(f"{bucket_name}/", "")
logger.log(f"Parsing operation completed, writing the results to the Cloud Storage bucket '{current_folder}'")
bucket.blob(f'{current_folder}feed_updated_by_feedgen.csv').upload_from_string(df.to_csv(), 'text/csv')