sagravela's picture
update
c21d04d
import pandas as pd
import gradio as gr
from datetime import datetime
from inference import RecommendationEngine
class QueryInputForm:
def __init__(self):
# Predefined options for channel and device type
self.channel_options = [
'Paid Social', 'Paid Search - Brand', 'Organic', 'Email - Transactional',
'Affiliate', 'Paid Search', 'Direct', 'Referral', 'Email - Marketing',
'Paid Search - Brand Reactivation', 'SMS - Marketing', 'Email - Trigger',
'Referral - Whitelabel', 'Referral - Merchant', 'Social', 'SMS - Trigger',
]
self.device_type_options = [
'Mobile', 'Desktop', 'Phablet', 'Tablet', 'TV',
'Portable Media Player', 'Wearable',
]
# Default values for the form
self.default_query_text = "pizza"
# Initialize the recommender engine
self.recommender_engine = RecommendationEngine()
def get_recommendations(self, channel, device_type, query_text):
# Pass the query information to the recommender engine
raw_query = {
'user_id': "new_user", # any user will be considered as a new user
'channel': channel,
'device_type': device_type,
'query_text': query_text,
'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"), # query time
}
# Get recommendations
self.recommender_engine.get_recommendations(raw_query)
recommendations_df = pd.DataFrame(self.recommender_engine.recommendations)
recommendations_df = recommendations_df.style.format({'Score': '{:.2f}'})
# Return the recommendations as a dataframe
return gr.update(value=recommendations_df)
# Instantiate the form
form = QueryInputForm()
# Gradio interface
def recommendation_interface(channel, device_type, query_text):
return form.get_recommendations(channel, device_type, query_text)
with gr.Blocks(theme= gr.themes.Origin(text_size="md", spacing_size="lg"), title="E-Commerce Recommendation Engine Demo") as interface:
gr.Markdown("# E-Commerce Recommendation Engine Demo", elem_id='title')
with gr.Row():
with gr.Column():
channel_dropdown = gr.Dropdown(choices=form.channel_options, label="Channel")
device_dropdown = gr.Dropdown(choices=form.device_type_options, label="Device Type")
query_input = gr.Textbox(value=form.default_query_text, label="Query Text")
submit_button = gr.Button("Submit", variant="primary")
with gr.Column(scale=3):
gr.Markdown("## Top Recommendations:")
recommendation_output = gr.Dataframe(
show_label=False,
headers = [
'Score', 'Product Name', 'Category', 'Price (in cents)', 'Reviews',
'Merchant', 'City', 'State', 'Region',
'Free Shipping', 'Sold Out', 'Editor\'s Pick', 'On Sale',
],
interactive=False,
elem_id="recommendation-table",
max_height=400,
column_widths=["90px", "350px", "250px", "90px", "90px", "200px", "150px", "150px", "150px", "90px", "90px", "90px", "90px"]
)
submit_button.click(
recommendation_interface,
inputs=[channel_dropdown, device_dropdown, query_input],
outputs=recommendation_output,
)
interface.css = """
#title {
text-align: center;
margin: 20px;
}
"""
if __name__ == "__main__":
interface.launch()