Spaces:
Sleeping
Sleeping
Delete memory_store.py
Browse files- memory_store.py +0 -93
memory_store.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
import hashlib
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
|
| 7 |
-
import faiss
|
| 8 |
-
import numpy as np
|
| 9 |
-
import pandas as pd
|
| 10 |
-
from datasets import Dataset
|
| 11 |
-
from huggingface_hub import upload_file, HfFolder
|
| 12 |
-
|
| 13 |
-
from sentence_transformers import SentenceTransformer
|
| 14 |
-
|
| 15 |
-
# π Paths
|
| 16 |
-
FAISS_PATH = "memory/faiss_index.bin"
|
| 17 |
-
HASH_TRACK_PATH = "memory/faiss_hashes.txt"
|
| 18 |
-
DATASET_REPO = "abhidgp1978/agentchat_logs"
|
| 19 |
-
|
| 20 |
-
# π Hugging Face token setup
|
| 21 |
-
HF_TOKEN = os.getenv("agentchat_token")
|
| 22 |
-
if HF_TOKEN:
|
| 23 |
-
HfFolder.save_token(HF_TOKEN)
|
| 24 |
-
else:
|
| 25 |
-
print("β οΈ HF_TOKEN not set β uploads will fail if not running in authenticated context.")
|
| 26 |
-
|
| 27 |
-
# π€ Embedding model
|
| 28 |
-
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 29 |
-
|
| 30 |
-
# π Load FAISS index
|
| 31 |
-
dimension = 384
|
| 32 |
-
try:
|
| 33 |
-
faiss_index = faiss.read_index(FAISS_PATH)
|
| 34 |
-
print("β
FAISS index loaded from file.")
|
| 35 |
-
except Exception as e:
|
| 36 |
-
print(f"β οΈ Failed to load FAISS index: {e}")
|
| 37 |
-
faiss_index = faiss.IndexFlatL2(dimension)
|
| 38 |
-
print("π Creating new empty FAISS index.")
|
| 39 |
-
|
| 40 |
-
# π§ Hash tracker
|
| 41 |
-
try:
|
| 42 |
-
with open(HASH_TRACK_PATH, "r") as f:
|
| 43 |
-
existing_hashes = set(f.read().splitlines())
|
| 44 |
-
except FileNotFoundError:
|
| 45 |
-
existing_hashes = set()
|
| 46 |
-
|
| 47 |
-
def embed(text: str):
|
| 48 |
-
return model.encode([text])[0]
|
| 49 |
-
|
| 50 |
-
def hash_text(text: str):
|
| 51 |
-
return hashlib.sha256(text.encode()).hexdigest()
|
| 52 |
-
|
| 53 |
-
def build_or_update_index(query: str, role: str, answer: str):
|
| 54 |
-
combined = f"{role} | {query}"
|
| 55 |
-
query_hash = hash_text(combined)
|
| 56 |
-
if query_hash in existing_hashes:
|
| 57 |
-
print("π‘ Duplicate entry detected β skipping FAISS update.")
|
| 58 |
-
return
|
| 59 |
-
|
| 60 |
-
embedding = embed(combined).astype("float32")
|
| 61 |
-
faiss_index.add(np.array([embedding]))
|
| 62 |
-
with open(HASH_TRACK_PATH, "a") as f:
|
| 63 |
-
f.write(query_hash + "\n")
|
| 64 |
-
try:
|
| 65 |
-
faiss.write_index(faiss_index, FAISS_PATH)
|
| 66 |
-
print("β
Updated FAISS and saved to memory/faiss_index.bin")
|
| 67 |
-
if HF_TOKEN:
|
| 68 |
-
upload_file(path_or_fileobj=FAISS_PATH, path_in_repo=FAISS_PATH, repo_id=DATASET_REPO, repo_type="dataset", token=HF_TOKEN)
|
| 69 |
-
upload_file(path_or_fileobj=HASH_TRACK_PATH, path_in_repo=HASH_TRACK_PATH, repo_id=DATASET_REPO, repo_type="dataset", token=HF_TOKEN)
|
| 70 |
-
except Exception as e:
|
| 71 |
-
print(f"β Failed to push FAISS: {e}")
|
| 72 |
-
|
| 73 |
-
def search_similar(query: str, role: str, k=3):
|
| 74 |
-
combined = f"{role} | {query}"
|
| 75 |
-
embedding = embed(combined).astype("float32")
|
| 76 |
-
if faiss_index.ntotal == 0:
|
| 77 |
-
return []
|
| 78 |
-
_, indices = faiss_index.search(np.array([embedding]), k)
|
| 79 |
-
return indices[0].tolist()
|
| 80 |
-
|
| 81 |
-
def log_chat(role: str, query: str, answer: str):
|
| 82 |
-
entry = {
|
| 83 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 84 |
-
"role": role,
|
| 85 |
-
"query": query,
|
| 86 |
-
"answer": answer,
|
| 87 |
-
}
|
| 88 |
-
try:
|
| 89 |
-
dataset = Dataset.from_list([entry])
|
| 90 |
-
dataset.push_to_hub(DATASET_REPO, token=HF_TOKEN)
|
| 91 |
-
print("β
Chat log pushed to Hugging Face Dataset")
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"β Failed to log chat: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|