This file contains hidden or 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
def removal_effects(df, conversion_rate): | |
removal_effects_dict = {} | |
channels = [channel for channel in df.columns if channel not in ['Start', | |
'Null', | |
'Activation']] | |
for channel in channels: | |
removal_df = df.drop(channel, axis=1).drop(channel, axis=0) | |
for column in removal_df.columns: | |
row_sum = np.sum(list(removal_df.loc[column])) | |
null_pct = float(1) - row_sum |
This file contains hidden or 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
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from markovchain import MarkovChain | |
mc = MarkovChain(trans_matrix.values, trans_matrix.index) | |
mc.draw() |
This file contains hidden or 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
# Function to create intermediate path strings | |
def transition_states(paths): | |
unique_channels = set(x for element in paths for x in element) | |
transition_states = {x + '>' + y: 0 for x in unique_channels for y in unique_channels} | |
for possible_state in unique_channels: | |
if possible_state not in ['Activation', 'Null']: | |
for user_path in paths: | |
if possible_state in user_path: | |
indices = [i for i, s in enumerate(user_path) if possible_state in s] |
This file contains hidden or 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
# Step 1: Convert all interactions to a list | |
journeys = campaign_data.groupby('customer_id')['channel'].aggregate( | |
lambda x: x.tolist()).reset_index() | |
# Step 2: Add last interaction as 1 or 0 event representing activation | |
activation_results = campaign_data.drop_duplicates('customer_id', keep='last')[['customer_id', 'activation']] | |
journeys = pd.merge(journeys, activation_results, how='left', on='customer_id') | |
# Step 3: Add start and end states based on whether customer activated | |
journeys['path'] = np.where( |
This file contains hidden or 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
import numpy as np | |
import pandas as pd | |
campaign_data = pd.read_csv("cashback_activation_data.csv") | |
campaign_data = campaign_data.sort_values(['customer_id', 'timestamp'], | |
ascending=[False, True]) | |
campaign_data['visit_order'] = campaign_data.groupby('customer_id').cumcount() + 1 |
This file contains hidden or 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
op_custom = st.container() | |
def updateChatBox(): | |
if st.session_state['response_default']: | |
frm_ask = op_custom.form('frm_ask') | |
question = frm_ask.text_area('If you have any specific question, ask here.', 'What are the three takeaways from this quarter?') | |
frm_ask_submitted = frm_ask.form_submit_button('Ask') | |
if frm_ask_submitted: | |
if not openai_api_key.startswith('sk-'): | |
op_custom.warning('Please enter your OpenAI API key!', icon='⚠') |
This file contains hidden or 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
# Response function - for questions identified in text | |
def generate_default_response(documents, prompt=prompt_default): | |
qa = get_guery_function(documents) | |
try: | |
llm_response = qa(prompt) | |
response = llm_response["result"] | |
except Exception as err: | |
response = str(err) | |
return response |
This file contains hidden or 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
### Query function for the app ### | |
def get_guery_function(documents): | |
# Text splitter | |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=10) | |
texts = text_splitter.split_documents(documents) | |
# Embeddings | |
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) | |
vectordb = Chroma.from_documents(documents=texts, | |
embedding=embeddings, |
This file contains hidden or 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
prompt_default = ''' | |
This document is the transcript of an earnings conference call of a company. Assume you are an analyst who | |
attended this call. Identify which company this document is talking about. Identify 10 best questions and their | |
answers that would help summarize the company's performance. | |
Create a report in a markdown format that answers each of those 10 questions. Here is an example of the format. | |
Example | |
## Insert company name here |
This file contains hidden or 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
### Read text from PDF file ### | |
def read_pdf(file): | |
loader = OnlinePDFLoader(file) | |
documents = loader.load() | |
return documents |