running faiss script
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from datasets import load_dataset
|
| 4 |
import time
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def log(message):
|
| 7 |
print(f"β
{message}")
|
|
@@ -88,6 +90,33 @@ demo = gr.Interface(
|
|
| 88 |
)
|
| 89 |
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
# Launch Gradio app
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from datasets import load_dataset
|
| 4 |
import time
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
def log(message):
|
| 9 |
print(f"β
{message}")
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
|
| 93 |
+
# β
Function to check FAISS index
|
| 94 |
+
def check_faiss():
|
| 95 |
+
index_path = "my_embeddings" # Adjust if needed
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
index = faiss.read_index(index_path)
|
| 99 |
+
num_vectors = index.ntotal
|
| 100 |
+
dim = index.d
|
| 101 |
+
|
| 102 |
+
if num_vectors > 0:
|
| 103 |
+
sample_vectors = index.reconstruct_n(0, min(5, num_vectors)) # Get first 5 embeddings
|
| 104 |
+
return f"π FAISS index contains {num_vectors} vectors.\nβ
Embedding dimension: {dim}\nπ§ Sample: {sample_vectors[:2]} ..."
|
| 105 |
+
else:
|
| 106 |
+
return "β οΈ No embeddings found in FAISS index!"
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return f"β ERROR: Failed to load FAISS index - {e}"
|
| 110 |
+
|
| 111 |
+
# β
Add a Gradio button to trigger FAISS check
|
| 112 |
+
with gr.Blocks() as demo:
|
| 113 |
+
gr.Markdown("### π FAISS Embedding Check")
|
| 114 |
+
|
| 115 |
+
check_button = gr.Button("π Check FAISS Embeddings")
|
| 116 |
+
output_text = gr.Textbox(label="FAISS Status", interactive=False)
|
| 117 |
+
|
| 118 |
+
check_button.click(fn=check_faiss, outputs=output_text)
|
| 119 |
+
|
| 120 |
# Launch Gradio app
|
| 121 |
if __name__ == "__main__":
|
| 122 |
demo.launch()
|