Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from pinecone import Pinecone
|
4 |
+
|
5 |
+
# initialize connection to pinecone (get API key at app.pinecone.io)
|
6 |
+
api_key = os.environ.get('PINECONE_API_KEY') or '68f4d786-9797-4c17-8620-9b5302a3823b'
|
7 |
+
|
8 |
+
# configure client
|
9 |
+
pc = Pinecone(api_key=api_key)
|
10 |
+
|
11 |
+
from pinecone import ServerlessSpec
|
12 |
+
|
13 |
+
cloud = os.environ.get('PINECONE_CLOUD') or 'aws'
|
14 |
+
region = os.environ.get('PINECONE_REGION') or 'us-east-1'
|
15 |
+
|
16 |
+
spec = ServerlessSpec(cloud=cloud, region=region)
|
17 |
+
|
18 |
+
index_name = 'search-index'
|
19 |
+
|
20 |
+
import time
|
21 |
+
|
22 |
+
existing_indexes = [
|
23 |
+
index_info["name"] for index_info in pc.list_indexes()
|
24 |
+
]
|
25 |
+
|
26 |
+
# # check if index already exists (it shouldn't if this is first time)
|
27 |
+
# if index_name not in existing_indexes:
|
28 |
+
# # if does not exist, create index
|
29 |
+
# pc.create_index(
|
30 |
+
# index_name,
|
31 |
+
# dimension=384, # dimensionality of minilm
|
32 |
+
# metric='cosine',
|
33 |
+
# spec=spec
|
34 |
+
# )
|
35 |
+
# # wait for index to be initialized
|
36 |
+
# while not pc.describe_index(index_name).status['ready']:
|
37 |
+
# time.sleep(1)
|
38 |
+
|
39 |
+
# connect to index
|
40 |
+
index = pc.Index(index_name)
|
41 |
+
time.sleep(1)
|
42 |
+
# view index stats
|
43 |
+
index.describe_index_stats()
|
44 |
+
|
45 |
+
from sentence_transformers import SentenceTransformer
|
46 |
+
# import torch
|
47 |
+
|
48 |
+
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
49 |
+
|
50 |
+
model = SentenceTransformer('intfloat/e5-small')
|
51 |
+
|
52 |
+
|
53 |
+
# Set up the Streamlit app
|
54 |
+
st.set_page_config(page_title="Hotel Search", page_icon=":hotel:", layout="wide")
|
55 |
+
|
56 |
+
# Set up the Streamlit app title and search bar
|
57 |
+
st.title("Hotel Search")
|
58 |
+
query = st.text_input("Enter a search query:", "")
|
59 |
+
|
60 |
+
# If the user has entered a search query, search the Pinecone index with the query
|
61 |
+
if query:
|
62 |
+
# Upsert the embeddings for the query into the Pinecone index
|
63 |
+
query_embeddings = model.encode(query).tolist()
|
64 |
+
# now query
|
65 |
+
xc = index.query(vector=query_embeddings, top_k=5,, namespace="hotel-detail", include_metadata=True)
|
66 |
+
|
67 |
+
# Display the search results
|
68 |
+
st.write(f"Search results for '{query}':")
|
69 |
+
for result in xc['matches']:
|
70 |
+
st.write(f"{round(result['score'], 2)}: {result['metadata']['meta_text']}")
|