LalitMahale commited on
Commit
a2d4cca
·
1 Parent(s): e2faad1

project push

Browse files
Files changed (5) hide show
  1. .gitignore +3 -0
  2. app.py +38 -0
  3. config.py +2 -0
  4. requirements.txt +5 -0
  5. src/multimodelsearch.py +33 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ data
2
+ src/__pycache__*
3
+ __pycache__*
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from src.multimodelsearch import MultiModelSearch
3
+ st.set_page_config(
4
+ layout="wide",
5
+ page_title="Recommendation_engine"
6
+ )
7
+
8
+
9
+ def main():
10
+ st.markdown("<h1 style = 'text-align:center; color:black;'>Recommendation_engine</h1>",unsafe_allow_html=True)
11
+
12
+ multimodelserch = MultiModelSearch()
13
+
14
+ query = st.text_input("Enter Your Query")
15
+
16
+ if st.button("Search") and len(query) > 0:
17
+ result = multimodelserch.search(query=query)
18
+ st.info(f"Your query : {query}")
19
+ st.subheader("Search Results")
20
+ col1,col2,col3 = st.columns(3)
21
+
22
+ with col1:
23
+ st.write(f"Score: {round(result[0].score*100)}%")
24
+ st.image(result[0].content,use_column_width=True)
25
+
26
+ with col2:
27
+ st.write(f"Score: {round(result[1].score*100)}%")
28
+ st.image(result[1].content,use_column_width=True)
29
+
30
+ with col3:
31
+ st.write(f"Score: {round(result[2].score*100)}%")
32
+ st.image(result[2].content,use_column_width=True)
33
+
34
+ else:
35
+ st.warning("Please Enter query.......")
36
+
37
+ if __name__ == "__main__":
38
+ main()
config.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ MODEL_DIM = 512
2
+ MODEL_NAME = 'sentence-transformers/clip-ViT-B-32'
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ sentence-transformers
2
+ streamlit
3
+ torch
4
+ farm-haystack
5
+
src/multimodelsearch.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from haystack import Document
3
+ from haystack import Pipeline
4
+ from haystack.document_stores import InMemoryDocumentStore
5
+ from haystack.nodes.retriever.multimodal import MultiModalRetriever
6
+ from config import MODEL_DIM, MODEL_NAME
7
+
8
+ class MultiModelSearch:
9
+ def __init__(self):
10
+ self.document_stores = InMemoryDocumentStore(embedding_dim=MODEL_DIM)
11
+ document_directory = os.path.join(os.getcwd(),"data")
12
+ # fetch all images and write into haystack document
13
+ images = [
14
+ Document(content= f"{document_directory}/{filename}",content_type="image" )
15
+ for filename in os.listdir(document_directory)
16
+ ]
17
+
18
+ self.document_stores.write_documents(images)
19
+ self.retriever_text_to_image = MultiModalRetriever(
20
+ document_store= self.document_stores,
21
+ query_embedding_model= MODEL_NAME,
22
+ query_type="text",
23
+ document_embedding_models= {"image":MODEL_NAME},
24
+ )
25
+
26
+ self.document_stores.update_embeddings(retriever=self.retriever_text_to_image)
27
+
28
+ self.pipeline = Pipeline()
29
+ self.pipeline.add_node(component=self.retriever_text_to_image, name="retriever_text_to_image", inputs=["Query"])
30
+
31
+ def search(self,query, top_k = 3):
32
+ results = self.pipeline.run(query=query, params={"retriever_text_to_image": {"top_k":top_k}})
33
+ return sorted(results["documents"],key= lambda d:d.score, reverse=True)