-
Notifications
You must be signed in to change notification settings - Fork 6
/
03_05_app-complete.py
148 lines (121 loc) · 5.54 KB
/
03_05_app-complete.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import utils
import streamlit as st
import weaviate.classes as wvc
from weaviate.util import generate_uuid5
client = utils.connect_to_demo_db() # Connect to the demo database
# client = utils.connect_to_my_db() # You can also connect to your own database
try: # Wrap everything in a try-finally block to ensure the connection is closed
movies = client.collections.get("Movie")
synopses = client.collections.get("Synopsis")
# Show page title - ReelRecommender
st.title("ReelRecommender")
# Have multiple tabs, each one for different features (search / movie details / recommendations)
search_tab, movie_tab, rec_tab = st.tabs(["Search", "Movie details", "Recommend"])
with search_tab:
st.header("Search for a movie")
query_string = st.text_input(label="Search for a movie")
srch_col1, srch_col2 = st.columns(2)
with srch_col1:
search_type = st.radio(
label="How do you want to search?",
options=["Vector", "Hybrid"]
)
with srch_col2:
value_range = st.slider(label="Rating range", value=(0.0, 5.0), step=0.1)
# Search results - movie summaries
st.header("Search results")
movie_filter = (
wvc.query.Filter.by_property("rating").greater_or_equal(value_range[0])
& wvc.query.Filter.by_property("rating").less_or_equal(value_range[1])
)
synopsis_xref = wvc.query.QueryReference(
link_on="hasSynopsis", return_properties=["body"]
)
if len(query_string) > 0: # Only run a search if there is an input
if search_type == "Vector":
response = movies.query.near_text(
query=query_string,
filters=movie_filter,
limit=5,
return_references=[synopsis_xref],
)
else:
response = movies.query.hybrid(
query=query_string,
filters=movie_filter,
limit=5,
return_references=[synopsis_xref],
)
else:
response = movies.query.fetch_objects(
filters=movie_filter,
limit=5,
return_references=[synopsis_xref],
)
for movie in response.objects:
with st.expander(movie.properties["title"]):
rating = movie.properties["rating"]
movie_id = movie.properties["movie_id"]
st.write(f"**Movie rating**: {rating}, **ID**: {movie_id}")
synopsis = movie.references["hasSynopsis"].objects[0].properties["body"]
st.write("**Synopsis**")
st.write(synopsis[:200] + "...")
with movie_tab:
# Detailed movie information
st.header("Movie details")
title_input = st.text_input(label="Enter the movie row ID here (0-120)", value="")
if len(title_input) > 0: # Only do something if there is an input
movie_uuid = generate_uuid5(int(title_input))
movie = movies.query.fetch_object_by_id(
uuid=movie_uuid,
return_references=[
wvc.query.QueryReference(
link_on="hasSynopsis", return_properties=["body"]
),
],
)
title = movie.properties["title"]
director = movie.properties["director"]
rating = movie.properties["rating"]
movie_id = movie.properties["movie_id"]
year = movie.properties["year"]
st.header(title)
st.write(f"Director: {director}")
st.write(f"Rating: {rating}")
st.write(f"Movie ID: {movie_id}")
st.write(f"Year: {year}")
with st.expander("See synopsis"):
st.write(movie.references["hasSynopsis"].objects[0].properties["body"])
with rec_tab:
# AI-powered recommendations
st.header("Recommend me a movie")
search_string = st.text_input(label="Recommend me a ...", value="")
occasion = st.text_input(label="In this context ...", value="any occasion")
# Only do something if the user fills in the search string and the context
if len(search_string) > 0 and len(occasion) > 0:
st.subheader("Recommendations")
response = synopses.generate.hybrid(
query=search_string,
grouped_task=f"""
The user is looking to watch
{search_string} types of movies for {occasion}.
Provide a movie recommendation
based on the provided movie synopses.
""",
limit=3,
return_references=[
wvc.query.QueryReference(
link_on="forMovie", return_properties=["title", "movie_id", "description"]
),
],
)
st.write(response.generated)
st.subheader("Movies analysed")
for i, m in enumerate(response.objects):
movie_title = m.references["forMovie"].objects[0].properties["title"]
movie_id = m.references["forMovie"].objects[0].properties["movie_id"]
movie_description = m.references["forMovie"].objects[0].properties["description"]
with st.expander(f"Movie title: {movie_title}, ID: {movie_id}"):
st.write(movie_description)
finally:
client.close() # Gracefully close the connection