-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchApp.py
71 lines (56 loc) · 1.96 KB
/
searchApp.py
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
71
ELASTIC_ENDPOINT = ""
ELASTIC_USERNAME = "elastic"
ELASTIC_PASSWORD = ""
ELASTIC_INDEX = "dk_semantic_search"
SAMPLE_COUNT = 1000
import streamlit as st
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer
try:
es = Elasticsearch(
ELASTIC_ENDPOINT,
basic_auth=(ELASTIC_USERNAME, ELASTIC_PASSWORD),
)
except ConnectionError as e:
print("Connection Error:", e)
if es.ping():
print("Successfully connected to ElasticSearch")
else:
print("(!) Can not connect to Elasticsearch")
def search(input_keyword):
model = SentenceTransformer('intfloat/multilingual-e5-large')
vector_of_input_keyword = model.encode(input_keyword)
query = {
"field": "titleVector",
"query_vector": vector_of_input_keyword,
"k": 10,
"num_candidates": SAMPLE_COUNT
}
res = es.knn_search(index=ELASTIC_INDEX, knn=query, source=["title_fa", "Category1"])
results = res["hits"]["hits"]
return results
def main():
st.title("Search DigiKala Semantic")
# Input: User enters search query
search_query = st.text_input("Enter your search query")
# Button: User triggers the search
if st.button("Search"):
if search_query:
# Perform the search and get results
results = search(search_query)
# Display search results
st.subheader("Search Results")
for result in results:
with st.container():
if '_source' in result:
try:
st.header(f"{result['_source']['title_fa']}")
except Exception as e:
print(e)
try:
st.write(f"Category: {result['_source']['Category1']}")
except Exception as e:
print(e)
st.divider()
if __name__ == "__main__":
main()