Spaces:
Runtime error
Runtime error
jamescalam
commited on
Commit
•
666b09a
1
Parent(s):
71a7702
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pinecone
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
|
5 |
+
PINECONE_KEY = st.secrets["PINECONE_KEY"] # app.pinecone.io
|
6 |
+
|
7 |
+
@st.experimental_singleton
|
8 |
+
def init_pinecone():
|
9 |
+
pinecone.init(api_key=PINECONE_KEY, environment="us-west1-gcp")
|
10 |
+
return pinecone.Index('youtube-search')
|
11 |
+
|
12 |
+
@st.experimental_singleton
|
13 |
+
def init_retriever():
|
14 |
+
return SentenceTransformer("multi-qa-mpnet-base-dot-v1")
|
15 |
+
|
16 |
+
index = init_pinecone()
|
17 |
+
retriever = init_retriever()
|
18 |
+
|
19 |
+
def card(thubmnail, title, url, context):
|
20 |
+
return st.markdown(f"""
|
21 |
+
<div class="container-fluid">
|
22 |
+
<div class="row align-items-start">
|
23 |
+
<div class="col-md-4 col-sm-4">
|
24 |
+
<div class="position-relative">
|
25 |
+
<a href={url}><img src={thubmnail} class="img-fluid" style="width: 192px; height: 106px"></a>
|
26 |
+
</div>
|
27 |
+
</div>
|
28 |
+
<div class="col-md-8 col-sm-8">
|
29 |
+
<a href={url}>{title}</a>
|
30 |
+
<br>
|
31 |
+
<span style="color: #808080;">
|
32 |
+
<small>{context[:200].capitalize()+"...."}</small>
|
33 |
+
</span>
|
34 |
+
</div>
|
35 |
+
</div>
|
36 |
+
</div>
|
37 |
+
""", unsafe_allow_html=True)
|
38 |
+
|
39 |
+
|
40 |
+
st.write("""
|
41 |
+
# YouTube Q&A
|
42 |
+
Ask me a question!
|
43 |
+
""")
|
44 |
+
|
45 |
+
st.markdown("""
|
46 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
|
47 |
+
""", unsafe_allow_html=True)
|
48 |
+
|
49 |
+
query = st.text_input("Search!", "")
|
50 |
+
|
51 |
+
if query != "":
|
52 |
+
xq = retriever.encode([query]).tolist()
|
53 |
+
xc = index.query(xq, top_k=5, include_metadata=True)
|
54 |
+
|
55 |
+
for context in xc['matches']:
|
56 |
+
card(
|
57 |
+
f"https://img.youtube.com/vi/{context['metadata']['url'].split('/')[-1]}/maxresdefault.jpg",
|
58 |
+
context['metadata']['title'],
|
59 |
+
f"{context['metadata']['url']}?t={int(context['metadata']['start'])}",
|
60 |
+
context['metadata']['text']
|
61 |
+
)
|