-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter_demo.py
311 lines (268 loc) · 11.9 KB
/
router_demo.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import json
import logging
import os
import sys
from typing import List
from llama_index.core import StorageContext, load_index_from_storage
from llama_index.core.query_engine.router_query_engine import RouterQueryEngine
from llama_index.core.selectors.llm_selectors import LLMSingleSelector, LLMMultiSelector
from llama_index.core.tools.query_engine import QueryEngineTool
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.vector_stores.milvus import MilvusVectorStore
from pydantic import BaseModel, Field
import streamlit as st
# ===== Query Engine Setup ===== #
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
HOWTO_STORAGE = "./storage/howto-212k-bge-small-en-384"
HOWTO_COLLECTION = "howto212k"
TRAVEL_STORAGE = "./storage/travel-69k-bge-small-en-384"
TRAVEL_COLLECTION = "travel69k"
EMBED = "BAAI/bge-small-en-v1.5"
EMBED_DIMS = 384
RESPONSE_MODE = "tree_summarize"
class HowToStep(BaseModel):
"""How to step data model"""
description: str = Field(
description=
"description of specific step to take in series of steps to solve problem / accomplish task"
)
url: str = Field(description="link to the source youtube video")
scene: int = Field(
description=
"relevant video scene number in source video, only provide an integer number"
)
timestamp: int = Field(
description=
"relevant timestamp of scene source video, only provide an integer number"
)
class HowToSteps(BaseModel):
"""Data model for how to step extracted information."""
steps: List[HowToStep] = Field(
description="List of steps to take to solve problem / accomplish task")
summary: str = Field(
description=
"High level summary / commentary on how to solve problem / accomplish. task"
)
class NotablePlaceMention(BaseModel):
"""Notable place mention data model"""
name: str = Field(description="the place name")
description: str = Field(
description=
"one to two sentence description of the place and why it is well known / why people love it"
)
place_type: str = Field(
description=
"type of place / point of interest, e.g. restaurant, landmark, transportation, shopping, etc."
)
best_known_for: List[str] = Field(
description="list of things this place is know for")
url: str = Field(description="link to the source youtube video")
scene: int = Field(
description=
"relevant video scene number in source video, only provide an integer number"
)
timestamp: int = Field(
description=
"relevant timestamp of scene source video, only provide an integer number"
)
class NotablePlaceMentionsSummary(BaseModel):
"""Data model for notable place mentions extracted information."""
place_mentions: List[NotablePlaceMention] = Field(
description="List of notable places mentioned in retrieved results")
summary: str = Field(
description=
"High level summary / commentary on the places retrieved and how relevant to the query"
)
def get_milvus_query_engine(collection_name,
storage_dir,
output_cls,
llm,
response_mode=RESPONSE_MODE):
vector_store = MilvusVectorStore(dim=EMBED_DIMS,
uri=os.getenv('MILVUS_HOST'),
token=os.getenv('MILVUS_TOKEN'),
overwrite=False,
collection_name=collection_name)
storage_context = StorageContext.from_defaults(vector_store=vector_store,
persist_dir=storage_dir)
index = load_index_from_storage(storage_context=storage_context)
query_engine = index.as_query_engine(
output_cls=output_cls,
response_mode=response_mode,
llm=llm,
verbose=True,
)
return query_engine
@st.cache_resource
def load_query_engine():
embed_model = HuggingFaceEmbedding(model_name=EMBED)
llm = OpenAI(model="gpt-3.5-turbo", temperature=1)
from llama_index.core import Settings
Settings.embed_model = embed_model
Settings.llm = llm
howto_query_engine = get_milvus_query_engine(HOWTO_COLLECTION,
HOWTO_STORAGE, HowToSteps,
llm)
place_query_engine = get_milvus_query_engine(TRAVEL_COLLECTION,
TRAVEL_STORAGE,
NotablePlaceMentionsSummary,
llm)
howto_tool = QueryEngineTool.from_defaults(
query_engine=howto_query_engine,
description=
"Useful for retrieving information about how to accomplish specific tasks",
)
place_tool = QueryEngineTool.from_defaults(
query_engine=place_query_engine,
description=
"Useful for retrieving information about notable places like restaurants, tourist attractions, shopping, landmarks, etc.",
)
# Create Router Query Engine
query_engine = RouterQueryEngine(
selector=LLMSingleSelector.from_defaults(),
query_engine_tools=[
place_tool,
howto_tool,
],
)
return query_engine
query_engine = load_query_engine()
# ===== Start Main Stream Lit App ===== #
with st.sidebar:
st.title("Thousand Words Video Explorer")
st.markdown(f"""
Loaded Indices: `How-To, Travel`
""")
st.markdown("""
Info:
* Index contains ~282.1K YouTube videos that appeared to be english language and were in the "How To & Style" or "Travel & Events" youtube category
* This simple video DB is representing over 12.4k hours of video (i.e. ~1.42 years of audio/visual information)
Example Queries:
* `what is the best museum in paris aside from the lourve?`
* `how do I repair a loose toilet flusher?`
* `what are the top 5 places in england to visit for a harry potter fan?`
* `show me the best steakhouses in new york city`
* `show me step by step how to make mapu tofu`
""")
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
if message["role"] == "user":
st.markdown(message["content"])
else:
response = message["content"]
print(response)
print(type(response.response))
st.text("Here's what I found:")
if str(type(response.response)) == "<class '__main__.HowToSteps'>":
st.markdown(response.response.summary)
for no, m in enumerate(response.response.steps):
st.header(f'Step {no}')
st.markdown(m.description)
if m.url:
split = m.url[len('https://www.youtube.com/watch?v='
):] + '_split_' + f'{m.scene:05d}'
col1, col2 = st.columns(2)
with col1:
st.image(
f"https://storage.googleapis.com/kdr-public/pandas70m/howto-travel/img/clip-start/{split}.jpg"
)
st.text("clip preview")
with col2:
st.video(m.url,
format="video/mp4",
start_time=m.timestamp)
elif str(type(response.response)
) == "<class '__main__.NotablePlaceMentionsSummary'>":
st.markdown(response.response.summary)
for m in response.response.place_mentions:
st.header(m.name)
t = m.description + "\n\n**Known For**:\n"
for b in m.best_known_for:
t += "\n* " + b
st.markdown(t)
if m.url:
split = m.url[len('https://www.youtube.com/watch?v='
):] + '_split_' + f'{m.scene:05d}'
col1, col2 = st.columns(2)
with col1:
st.image(
f"https://storage.googleapis.com/kdr-public/pandas70m/howto-travel/img/clip-start/{split}.jpg"
)
st.text("clip preview")
with col2:
st.video(m.url,
format="video/mp4",
start_time=m.timestamp)
st.divider()
vs = set([])
t = "Sources:\n\n"
for s in response.source_nodes:
if s.metadata['video_url'] not in vs:
t += f"1. [{s.metadata['video_title']}]({s.metadata['video_url']})\n"
vs.add(s.metadata['video_url'])
st.markdown(t)
if prompt := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
response = query_engine.query(prompt)
print(response)
print(type(response.response))
st.text("Here's what I found:")
if str(type(response.response)) == "<class '__main__.HowToSteps'>":
st.markdown(response.response.summary)
for no, m in enumerate(response.response.steps):
st.header(f'Step {no}')
st.markdown(m.description)
if m.url:
split = m.url[len('https://www.youtube.com/watch?v='
):] + '_split_' + f'{m.scene:05d}'
col1, col2 = st.columns(2)
with col1:
st.image(
f"https://storage.googleapis.com/kdr-public/pandas70m/howto-travel/img/clip-start/{split}.jpg"
)
st.text("clip preview")
with col2:
st.video(m.url,
format="video/mp4",
start_time=m.timestamp)
elif str(type(response.response)
) == "<class '__main__.NotablePlaceMentionsSummary'>":
st.markdown(response.response.summary)
for m in response.response.place_mentions:
st.header(m.name)
t = m.description + "\n\n**Known For**:\n"
for b in m.best_known_for:
t += "\n* " + b
st.markdown(t)
if m.url:
split = m.url[len('https://www.youtube.com/watch?v='
):] + '_split_' + f'{m.scene:05d}'
col1, col2 = st.columns(2)
with col1:
st.image(
f"https://storage.googleapis.com/kdr-public/pandas70m/howto-travel/img/clip-start/{split}.jpg"
)
st.text("clip preview")
with col2:
st.video(m.url,
format="video/mp4",
start_time=m.timestamp)
st.divider()
vs = set([])
t = "Sources:\n\n"
for s in response.source_nodes:
if s.metadata['video_url'] not in vs:
t += f"1. [{s.metadata['video_title']}]({s.metadata['video_url']})\n"
vs.add(s.metadata['video_url'])
st.markdown(t)
st.session_state.messages.append({
"role": "assistant",
"content": response
})