Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,16 +24,25 @@ def create_embeddings(texts):
|
|
| 24 |
|
| 25 |
# Use Faiss for similarity search
|
| 26 |
def search(query, documents, k=1):
|
|
|
|
|
|
|
|
|
|
| 27 |
embeddings = create_embeddings([query] + documents)
|
| 28 |
query_embedding = embeddings[0]
|
| 29 |
doc_embeddings = np.stack(embeddings[1:])
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
index = faiss.IndexFlatL2(doc_embeddings.shape[1]) # L2 distance for similarity
|
| 32 |
index.add(doc_embeddings)
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Function for Text-to-Speech
|
| 39 |
def text_to_speech(text):
|
|
|
|
| 24 |
|
| 25 |
# Use Faiss for similarity search
|
| 26 |
def search(query, documents, k=1):
|
| 27 |
+
if not documents: # Ensure documents are not empty
|
| 28 |
+
return ["No documents available for searching."]
|
| 29 |
+
|
| 30 |
embeddings = create_embeddings([query] + documents)
|
| 31 |
query_embedding = embeddings[0]
|
| 32 |
doc_embeddings = np.stack(embeddings[1:])
|
| 33 |
+
|
| 34 |
+
if doc_embeddings.shape[0] == 0: # Ensure embeddings are non-empty
|
| 35 |
+
return ["No embeddings available for searching."]
|
| 36 |
|
| 37 |
index = faiss.IndexFlatL2(doc_embeddings.shape[1]) # L2 distance for similarity
|
| 38 |
index.add(doc_embeddings)
|
| 39 |
+
|
| 40 |
+
# Perform search
|
| 41 |
+
try:
|
| 42 |
+
D, I = index.search(np.array([query_embedding]), k)
|
| 43 |
+
return [documents[i] for i in I[0] if i < len(documents)] # Ensure indices are valid
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return [f"Error during search: {str(e)}"]
|
| 46 |
|
| 47 |
# Function for Text-to-Speech
|
| 48 |
def text_to_speech(text):
|