Jim Dowling commited on
Commit
b4865be
ยท
1 Parent(s): a08fda1

first version

Browse files
Files changed (2) hide show
  1. app.py +131 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import hopsworks
3
+ from mimesis import Generic
4
+ from mimesis.locales import Locale
5
+ import pandas as pd
6
+ import random
7
+
8
+ # Function to print a styled header
9
+ def print_header(text, font_size=22):
10
+ res = f'<span style=" font-size: {font_size}px;">{text}</span>'
11
+ st.markdown(res, unsafe_allow_html=True)
12
+
13
+ # Function to retrieve and start model deployments
14
+ @st.cache_resource()
15
+ def get_deployments():
16
+ # Displaying a message indicating the process has started
17
+ st.write("๐Ÿš€ Retrieving and Starting Deployments...")
18
+
19
+ # Logging into the Hopsworks project
20
+ project = hopsworks.login()
21
+
22
+ fs = project.get_feature_store()
23
+
24
+ interactions_fg = fs.get_feature_group(
25
+ name="interactions",
26
+ version=1,
27
+ )
28
+
29
+ videos_fg = fs.get_feature_group(
30
+ name="videos",
31
+ version=1,
32
+ )
33
+
34
+ # Getting the model serving instance from the project
35
+ ms = project.get_model_serving()
36
+
37
+ # Retrieving deployments for the query model and ranking model
38
+ query_model_deployment = ms.get_deployment("querydeployment")
39
+ ranking_deployment = ms.get_deployment("rankingdeployment")
40
+
41
+ # Starting the ranking deployment with a maximum waiting time of 180 seconds
42
+ ranking_deployment.start(await_running=180)
43
+
44
+ # Starting the query model deployment with a maximum waiting time of 180 seconds
45
+ query_model_deployment.start(await_running=180)
46
+
47
+ # Displaying a message indicating that deployments are ready
48
+ st.write('โœ… Deployments are ready!')
49
+
50
+ # Returning deployment instances
51
+ return interactions_fg, videos_fg, ranking_deployment, query_model_deployment
52
+
53
+ def insert_interaction(user_id, video_id, interactions_fg):
54
+ generic = Generic(locale=Locale.EN)
55
+ interaction_id = generic.person.identifier(mask='####-##-####')
56
+ interaction_type = random.choices(
57
+ ['like', 'dislike', 'view', 'comment', 'share', 'skip'],
58
+ weights=[1.5, 0.2, 3, 0.5, 0.8, 10], k=1
59
+ )[0]
60
+ watch_time = random.randint(1, 50)
61
+
62
+ interaction_df = pd.DataFrame({
63
+ 'interaction_id': [interaction_id],
64
+ 'interaction_type': [interaction_type],
65
+ 'user_id': [user_id],
66
+ 'video_id': [video_id],
67
+ 'watch_time': [watch_time]
68
+ })
69
+
70
+ interactions_fg.insert(interaction_df, write_options={"start_offline_materialization": False})
71
+
72
+
73
+ # Define function to fetch recommendations
74
+ def fetch_recommendations(user_id, query_model_deployment):
75
+ st.write('๐Ÿ”ฎ Getting recommendations...')
76
+ deployment_input = {"instances": {"user_id": user_id}}
77
+ prediction = query_model_deployment.predict(deployment_input)['predictions']['ranking']
78
+ return prediction
79
+
80
+ # Function to insert interaction and fetch new recommendations
81
+ def handle_interaction(user_id, video_id, interactions_fg, query_model_deployment):
82
+ insert_interaction(user_id, video_id, interactions_fg)
83
+ return fetch_recommendations(user_id, query_model_deployment)
84
+
85
+
86
+ # Main Streamlit application logic
87
+ def main():
88
+ st.title('๐ŸŽฌ Tiktok Personalized Video Recommendations')
89
+ # Initialize or re-use existing deployments
90
+ if 'deployments_initialized' not in st.session_state:
91
+ st.session_state.interactions_fg, st.session_state.videos_fg, st.session_state.ranking_deployment, st.session_state.query_model_deployment = get_deployments()
92
+ st.session_state['deployments_initialized'] = True
93
+
94
+ # User selection box
95
+ user_id_option = st.selectbox(
96
+ 'For which user?',
97
+ ('SW104K', 'RA693D', 'DR282A', 'SN496G',),
98
+ key='user_select'
99
+ )
100
+
101
+ # Initialize or refresh recommendations
102
+ if 'recommendations' not in st.session_state or 'refresh' in st.session_state:
103
+ recommendations = fetch_recommendations(user_id_option, st.session_state.query_model_deployment)
104
+ random.shuffle(recommendations)
105
+ st.session_state.recommendations = recommendations
106
+ st.session_state.pop('refresh', None)
107
+
108
+ print_header('๐Ÿ“ Top 3 Recommendations:')
109
+ displayed_recommendations = st.session_state.recommendations[:3]
110
+ for recommendation in displayed_recommendations:
111
+ video_id = recommendation[1]
112
+ if st.button(f"๐Ÿ”— Video ID: {video_id}", key=video_id):
113
+ new_recommendations = handle_interaction(
114
+ user_id_option,
115
+ video_id,
116
+ st.session_state.interactions_fg,
117
+ st.session_state.query_model_deployment,
118
+ )
119
+ random.shuffle(new_recommendations)
120
+ st.session_state.recommendations = new_recommendations
121
+ st.experimental_rerun()
122
+
123
+ if st.button("Stop Streamlit"):
124
+ st.write('โš™๏ธ Stopping Deployments...')
125
+ st.session_state.ranking_deployment.stop()
126
+ st.session_state.query_model_deployment.stop()
127
+ st.success('โœ… App finished successfully!')
128
+ st.stop()
129
+
130
+ if __name__ == '__main__':
131
+ main()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ mimesis==15.1.0
2
+ tensorflow==2.13
3
+ tensorflow-recommenders==0.7.2
4
+ catboost==1.2.1
5
+ streamlit==1.33.0
6
+ hopsworks