IlyasMoutawwakil HF staff commited on
Commit
6407a06
β€’
1 Parent(s): 3b4c604

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -17,7 +17,6 @@ RETRIEVER_URL = os.getenv("RETRIEVER_URL")
17
  RANKER_URL = os.getenv("RANKER_URL")
18
  HF_TOKEN = os.getenv("HF_TOKEN")
19
 
20
-
21
  class Retriever(EmbeddingRetriever):
22
  def __init__(
23
  self,
@@ -120,17 +119,28 @@ EXAMPLES = [
120
  "The Sphinx is in Egypt.",
121
  ]
122
 
123
- if os.path.exists("faiss_document_store.db"):
124
- os.remove("faiss_document_store.db")
125
-
126
- document_store = FAISSDocumentStore(embedding_dim=384, return_embedding=True)
127
- document_store.write_documents(
128
- [Document(content=d, id=i) for i, d in enumerate(EXAMPLES)]
129
- )
130
-
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
- retriever = Retriever(document_store=document_store, top_k=TOP_K, batch_size=BATCH_SIZE)
133
- document_store.update_embeddings(retriever=retriever)
134
  ranker = Ranker()
135
 
136
  pipe = Pipeline()
@@ -142,12 +152,12 @@ def run(query: str) -> dict:
142
  output = pipe.run(query=query)
143
 
144
  return (
145
- f"Closest document(s): {[output['documents'][i].content for i in range(TOP_K)]}"
146
  )
147
 
148
 
149
- # warm up
150
  run("What is the capital of France?")
 
151
 
152
  gr.Interface(
153
  fn=run,
 
17
  RANKER_URL = os.getenv("RANKER_URL")
18
  HF_TOKEN = os.getenv("HF_TOKEN")
19
 
 
20
  class Retriever(EmbeddingRetriever):
21
  def __init__(
22
  self,
 
119
  "The Sphinx is in Egypt.",
120
  ]
121
 
122
+ if os.path.exists("faiss_document_store.db") and os.path.exists("faiss_index"):
123
+ document_store = FAISSDocumentStore.load("faiss_index")
124
+ retriever = Retriever(
125
+ document_store=document_store, top_k=TOP_K, batch_size=BATCH_SIZE
126
+ )
127
+ else:
128
+ try:
129
+ os.remove("faiss_index")
130
+ os.remove("faiss_document_store.db")
131
+ except FileNotFoundError:
132
+ pass
133
+
134
+ document_store = FAISSDocumentStore(embedding_dim=384, return_embedding=True)
135
+ document_store.write_documents(
136
+ [Document(content=d, id=i) for i, d in enumerate(EXAMPLES)]
137
+ )
138
+ retriever = Retriever(
139
+ document_store=document_store, top_k=TOP_K, batch_size=BATCH_SIZE
140
+ )
141
+ document_store.update_embeddings(retriever=retriever)
142
+ document_store.save(index_path="faiss_index")
143
 
 
 
144
  ranker = Ranker()
145
 
146
  pipe = Pipeline()
 
152
  output = pipe.run(query=query)
153
 
154
  return (
155
+ f"Closest ({TOP_K}) document(s): {[output['documents'][i].content for i in range(TOP_K)]}"
156
  )
157
 
158
 
 
159
  run("What is the capital of France?")
160
+ print("Warmed up successfully!")
161
 
162
  gr.Interface(
163
  fn=run,