File size: 3,588 Bytes
788b9b3
493b35f
788b9b3
493b35f
788b9b3
 
 
 
 
 
 
 
 
 
 
 
 
11b1e95
788b9b3
 
 
 
 
 
 
493b35f
 
 
788b9b3
 
493b35f
 
 
 
 
788b9b3
 
 
 
493b35f
 
 
 
 
788b9b3
493b35f
 
 
 
 
 
 
 
c21d04d
 
 
493b35f
 
 
 
c21d04d
493b35f
 
c21d04d
493b35f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788b9b3
493b35f
c21d04d
 
 
493b35f
 
788b9b3
 
493b35f
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
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()