File size: 1,857 Bytes
5b1326d
 
 
 
 
 
 
 
 
 
50474fb
 
 
 
 
f935900
50474fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f935900
5b1326d
50474fb
f935900
 
 
 
 
 
 
 
 
 
 
 
 
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
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
st.title("Search Engine")
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']}")