Spaces:
Sleeping
Sleeping
File size: 2,087 Bytes
5b1326d 4e50689 5b1326d |
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 64 65 66 67 68 69 70 |
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']}") |