8
8
9
9
# Constants
10
10
ENV_VARS = ["WEAVIATE_URL" , "WEAVIATE_API_KEY" , "COHERE_API_KEY" ]
11
- NUM_IMAGES_PER_ROW = 5
11
+ NUM_RECOMMENDATIONS_PER_ROW = 5
12
12
SEARCH_LIMIT = 10
13
13
14
14
# Search Mode descriptions
@@ -34,11 +34,18 @@ def display_chat_messages():
34
34
with st .chat_message (message ["role" ]):
35
35
st .markdown (message ["content" ])
36
36
if "images" in message :
37
- for i in range (0 , len (message ["images" ]), NUM_IMAGES_PER_ROW ):
38
- cols = st .columns (NUM_IMAGES_PER_ROW )
37
+ for i in range (0 , len (message ["images" ]), NUM_RECOMMENDATIONS_PER_ROW ):
38
+ cols = st .columns (NUM_RECOMMENDATIONS_PER_ROW )
39
39
for j , col in enumerate (cols ):
40
40
if i + j < len (message ["images" ]):
41
41
col .image (message ["images" ][i + j ], width = 200 )
42
+ if "titles" in message :
43
+ for i in range (0 , len (message ["titles" ]), NUM_RECOMMENDATIONS_PER_ROW ):
44
+ cols = st .columns (NUM_RECOMMENDATIONS_PER_ROW )
45
+ for j , col in enumerate (cols ):
46
+ if i + j < len (message ["titles" ]):
47
+ col .write (message ["titles" ][i + j ])
48
+
42
49
43
50
def base64_to_image (base64_str ):
44
51
"""Convert base64 string to image"""
@@ -112,7 +119,10 @@ def perform_search(conn, movie_type, rag_prompt, year_range, mode):
112
119
df = conn .query (
113
120
"MovieDemo" ,
114
121
query = movie_type ,
115
- return_properties = ["title" , "tagline" , "poster" ],
122
+ # Uncomment the line below if you want to use this with poster images
123
+ # return_properties=["title", "tagline", "poster"],
124
+ # Comment out the line below if you want to use this with poster images
125
+ return_properties = ["title" , "tagline" ],
116
126
filters = (
117
127
WeaviateFilter .by_property ("release_year" ).greater_or_equal (year_range [0 ]) &
118
128
WeaviateFilter .by_property ("release_year" ).less_or_equal (year_range [1 ])
@@ -122,6 +132,8 @@ def perform_search(conn, movie_type, rag_prompt, year_range, mode):
122
132
)
123
133
124
134
images = []
135
+ titles = []
136
+
125
137
if df is None or df .empty :
126
138
with st .chat_message ("assistant" ):
127
139
st .write (f"No movies found matching { movie_type } and using { mode } . Please try again." )
@@ -130,16 +142,21 @@ def perform_search(conn, movie_type, rag_prompt, year_range, mode):
130
142
else :
131
143
with st .chat_message ("assistant" ):
132
144
st .write ("Raw search results." )
133
- cols = st .columns (NUM_IMAGES_PER_ROW )
145
+ cols = st .columns (NUM_RECOMMENDATIONS_PER_ROW )
134
146
for index , row in df .iterrows ():
135
- col = cols [index % NUM_IMAGES_PER_ROW ]
136
- col .write (row ['title' ])
147
+ col = cols [index % NUM_RECOMMENDATIONS_PER_ROW ]
148
+ if "poster" in row and row ["poster" ]:
149
+ col .image (base64_to_image (row ["poster" ]), width = 200 )
150
+ images .append (base64_to_image (row ["poster" ]))
151
+ else :
152
+ col .write (f"{ row ['title' ]} " )
153
+ titles .append (row ["title" ])
154
+
137
155
st .write ("Now generating recommendation from these: ..." )
138
156
139
157
st .session_state .messages .append (
140
- {"role" : "assistant" , "content" : "Raw search results. Generating recommendation from these: ..." , "images" : images }
141
- )
142
-
158
+ {"role" : "assistant" , "content" : "Raw search results. Generating recommendation from these: ..." , "images" : images , "titles" : titles })
159
+
143
160
with conn .client () as client :
144
161
collection = client .collections .get ("MovieDemo" )
145
162
response = collection .generate .hybrid (
0 commit comments