Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
from pinecone import Pinecone | |
from sentence_transformers import SentenceTransformer | |
# import torch | |
# device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
model = SentenceTransformer('intfloat/e5-small') | |
# Set up the Streamlit app | |
st.set_page_config(page_title="Search Engine", layout="wide") | |
# Set up the Streamlit app title and search bar | |
with st.form("my_form"): | |
st.write("Login to Search Engine") | |
index_name = st.text_input("Enter a database name:", "") | |
key = st.text_input("Enter a key:", "") | |
namespace = st.text_input("Enter a table name:", "") | |
# slider_val = st.slider("Form slider") | |
# checkbox_val = st.checkbox("Form checkbox") | |
# Every form must have a submit button. | |
submitted = st.form_submit_button("Connect to My Search Engine") | |
if submitted: | |
# if st.button("Connect to Search Engine Database", type="primary"): | |
# index_name = st.text_input("Enter a database name:", "") | |
# key = st.text_input("Enter a key:", "") | |
# namespace = st.text_input("Enter a table name:", "") | |
# # initialize connection to pinecone (get API key at app.pinecone.io) | |
api_key = os.environ.get('PINECONE_API_KEY') or key | |
# configure client | |
pc = Pinecone(api_key=api_key) | |
from pinecone import ServerlessSpec | |
cloud = os.environ.get('PINECONE_CLOUD') or 'aws' | |
region = os.environ.get('PINECONE_REGION') or 'us-east-1' | |
spec = ServerlessSpec(cloud=cloud, region=region) | |
# connect to index | |
index = pc.Index(index_name) | |
st.write('Successfully connected to your Search Engine DB!') | |
st.write('Start searching...') | |
query = st.text_input("Enter a search query:", "") | |
# If the user has entered a search query, search the Pinecone index with the query | |
if query: | |
# Upsert the embeddings for the query into the Pinecone index | |
query_embeddings = model.encode(query).tolist() | |
# now query | |
xc = index.query(vector=query_embeddings, top_k=10, namespace=namespace, include_metadata=True) | |
# Display the search results | |
st.write(f"Search results for '{query}':") | |
for result in xc['matches']: | |
st.write(f"{round(result['score'], 2)}: {result['metadata']['meta_text']}") |