File size: 4,115 Bytes
788b9b3
493b35f
788b9b3
493b35f
 
788b9b3
 
 
 
 
 
 
 
 
 
 
 
 
11b1e95
788b9b3
 
 
 
 
 
 
493b35f
 
 
 
788b9b3
 
493b35f
 
 
 
 
788b9b3
 
 
 
493b35f
 
 
 
 
788b9b3
493b35f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788b9b3
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pandas as pd
import gradio as gr
from datetime import datetime
from inference import RecommendationEngine
import spaces


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()
   
    @spaces.GPU
    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() as interface:
    gr.Markdown("<h1 class='title'>E-Commerce Recommendation Engine Demo</h1>")
    
    with gr.Row():  # Use Row to place components side by side
        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", elem_id="submit-button")
        
        with gr.Column(scale=3):
            gr.Markdown("<h2>Top Recommendations:</h2>")
            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,
    )

# Add custom CSS for styling
interface.css = """
.title {
    color: #E76E00;
    text-align: center;
    margin: 40px;
    margin-bottom: 30px !important;
    font-size: 36px;
    font-weight: bold;
}
#submit-button {
    background-color: #E76E00;
    color: white;
    border: none;
    padding: 10px 20px;
    font-size: 16px;
    border-radius: 5px;
    cursor: pointer;
}
#submit-button:hover {
    background-color: #FF8C00;
}
#recommendation-table .gradio-header {
    background-color: #FF8C00; /* Orange header */
    color: white; /* White text */
    font-weight: bold; /* Optional: Bold text */
}
"""

if __name__ == "__main__":
    interface.launch()