Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,6 +52,32 @@ model = SentenceTransformer("all-MiniLM-L6-v2")
|
|
| 52 |
documents = extract_text_from_pdf("meal_plans")
|
| 53 |
index = create_index(documents)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Gradio UI
|
| 56 |
with gr.Blocks(title="Meal Plan Chat Assistant") as demo:
|
| 57 |
gr.Markdown("## 🍽️ Meal Plan Assistant\nChat with your PDF documents in `meal_plans/` folder.")
|
|
|
|
| 52 |
documents = extract_text_from_pdf("meal_plans")
|
| 53 |
index = create_index(documents)
|
| 54 |
|
| 55 |
+
def ingest(folder_path="meal_plans", index_file="mealplan.index"):
|
| 56 |
+
if os.path.exists(index_file) and os.path.exists("docstore.npy"):
|
| 57 |
+
print("📦 Loading existing FAISS index...")
|
| 58 |
+
index = faiss.read_index(index_file)
|
| 59 |
+
documents = np.load("docstore.npy", allow_pickle=True).tolist()
|
| 60 |
+
else:
|
| 61 |
+
print("📥 Ingesting PDFs from scratch...")
|
| 62 |
+
documents = extract_text_from_pdf(folder_path)
|
| 63 |
+
texts = [doc["text"] for doc in documents]
|
| 64 |
+
embeddings = model.encode(texts)
|
| 65 |
+
dim = embeddings[0].shape[0]
|
| 66 |
+
index = faiss.IndexFlatL2(dim)
|
| 67 |
+
index.add(np.array(embeddings).astype("float32"))
|
| 68 |
+
|
| 69 |
+
faiss.write_index(index, index_file)
|
| 70 |
+
np.save("docstore.npy", documents)
|
| 71 |
+
print("✅ Index and documents saved.")
|
| 72 |
+
|
| 73 |
+
return documents, index
|
| 74 |
+
|
| 75 |
+
# Load model
|
| 76 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 77 |
+
|
| 78 |
+
# Ingest PDFs and build/load index
|
| 79 |
+
documents, index = ingest("meal_plans")
|
| 80 |
+
|
| 81 |
# Gradio UI
|
| 82 |
with gr.Blocks(title="Meal Plan Chat Assistant") as demo:
|
| 83 |
gr.Markdown("## 🍽️ Meal Plan Assistant\nChat with your PDF documents in `meal_plans/` folder.")
|