search-engine / app.py
DeepSoft-Tech's picture
Update app.py
4e50689 verified
raw
history blame
2.09 kB
import streamlit as st
import os
from pinecone import Pinecone
# initialize connection to pinecone (get API key at app.pinecone.io)
api_key = os.environ.get('PINECONE_API_KEY') or '68f4d786-9797-4c17-8620-9b5302a3823b'
# 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)
index_name = 'search-index'
import time
existing_indexes = [
index_info["name"] for index_info in pc.list_indexes()
]
# # check if index already exists (it shouldn't if this is first time)
# if index_name not in existing_indexes:
# # if does not exist, create index
# pc.create_index(
# index_name,
# dimension=384, # dimensionality of minilm
# metric='cosine',
# spec=spec
# )
# # wait for index to be initialized
# while not pc.describe_index(index_name).status['ready']:
# time.sleep(1)
# connect to index
index = pc.Index(index_name)
time.sleep(1)
# view index stats
index.describe_index_stats()
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="Hotel Search", page_icon=":hotel:", layout="wide")
# Set up the Streamlit app title and search bar
st.title("Hotel Search")
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=5, namespace="hotel-detail", 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']}")