File size: 2,434 Bytes
5b1326d
 
 
 
 
 
 
 
 
 
50474fb
 
 
 
e5bc3ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae68bc9
e5bc3ec
 
ae68bc9
e5bc3ec
ae68bc9
e5bc3ec
 
 
 
 
 
 
 
 
 
dcb704e
e5bc3ec
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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']}")