Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,54 +12,52 @@ model = SentenceTransformer('intfloat/e5-small')
|
|
| 12 |
st.set_page_config(page_title="Search Engine", layout="wide")
|
| 13 |
|
| 14 |
# Set up the Streamlit app title and search bar
|
| 15 |
-
st.
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
if submitted:
|
| 28 |
-
# if st.button("Connect to Search Engine Database", type="primary"):
|
| 29 |
-
# index_name = st.text_input("Enter a database name:", "")
|
| 30 |
-
# key = st.text_input("Enter a key:", "")
|
| 31 |
-
# namespace = st.text_input("Enter a table name:", "")
|
| 32 |
-
# # initialize connection to pinecone (get API key at app.pinecone.io)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# configure client
|
| 37 |
-
pc = Pinecone(api_key=api_key)
|
| 38 |
-
|
| 39 |
-
from pinecone import ServerlessSpec
|
| 40 |
-
|
| 41 |
-
cloud = os.environ.get('PINECONE_CLOUD') or 'aws'
|
| 42 |
-
region = os.environ.get('PINECONE_REGION') or 'us-east-1'
|
| 43 |
-
|
| 44 |
-
spec = ServerlessSpec(cloud=cloud, region=region)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# connect to index
|
| 48 |
-
index = pc.Index(index_name)
|
| 49 |
-
st.write('Successfully connected to your Search Engine DB!')
|
| 50 |
-
st.write('Start searching...')
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
st.set_page_config(page_title="Search Engine", layout="wide")
|
| 13 |
|
| 14 |
# Set up the Streamlit app title and search bar
|
| 15 |
+
with st.form("my_form"):
|
| 16 |
+
st.write("Login to Search Engine")
|
| 17 |
+
index_name = st.text_input("Enter a database name:", "")
|
| 18 |
+
key = st.text_input("Enter a key:", "")
|
| 19 |
+
namespace = st.text_input("Enter a table name:", "")
|
| 20 |
+
# slider_val = st.slider("Form slider")
|
| 21 |
+
# checkbox_val = st.checkbox("Form checkbox")
|
| 22 |
+
|
| 23 |
+
# Every form must have a submit button.
|
| 24 |
+
submitted = st.form_submit_button("Connect to My Search Engine")
|
| 25 |
+
if submitted:
|
| 26 |
+
# if st.button("Connect to Search Engine Database", type="primary"):
|
| 27 |
+
# index_name = st.text_input("Enter a database name:", "")
|
| 28 |
+
# key = st.text_input("Enter a key:", "")
|
| 29 |
+
# namespace = st.text_input("Enter a table name:", "")
|
| 30 |
+
# # initialize connection to pinecone (get API key at app.pinecone.io)
|
| 31 |
+
|
| 32 |
+
api_key = os.environ.get('PINECONE_API_KEY') or key
|
| 33 |
|
| 34 |
+
# configure client
|
| 35 |
+
pc = Pinecone(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
from pinecone import ServerlessSpec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
cloud = os.environ.get('PINECONE_CLOUD') or 'aws'
|
| 40 |
+
region = os.environ.get('PINECONE_REGION') or 'us-east-1'
|
| 41 |
+
|
| 42 |
+
spec = ServerlessSpec(cloud=cloud, region=region)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# connect to index
|
| 46 |
+
index = pc.Index(index_name)
|
| 47 |
+
st.write('Successfully connected to your Search Engine DB!')
|
| 48 |
+
st.write('Start searching...')
|
| 49 |
|
| 50 |
+
|
| 51 |
+
query = st.text_input("Enter a search query:", "")
|
| 52 |
+
|
| 53 |
+
# If the user has entered a search query, search the Pinecone index with the query
|
| 54 |
+
if query:
|
| 55 |
+
# Upsert the embeddings for the query into the Pinecone index
|
| 56 |
+
query_embeddings = model.encode(query).tolist()
|
| 57 |
+
# now query
|
| 58 |
+
xc = index.query(vector=query_embeddings, top_k=10, namespace=namespace, include_metadata=True)
|
| 59 |
+
|
| 60 |
+
# Display the search results
|
| 61 |
+
st.write(f"Search results for '{query}':")
|
| 62 |
+
for result in xc['matches']:
|
| 63 |
+
st.write(f"{round(result['score'], 2)}: {result['metadata']['meta_text']}")
|