Spaces:
Paused
Paused
Add HF Hub integration for persistent data storage in HF Spaces
Browse files- app/config.py +2 -0
- app/rag.py +19 -0
- requirements.txt +1 -0
app/config.py
CHANGED
|
@@ -16,3 +16,5 @@ CHUNK_OVERLAP = int(os.getenv("CHUNK_OVERLAP", "120"))
|
|
| 16 |
TOP_K_DEFAULT = int(os.getenv("TOP_K_DEFAULT", "4"))
|
| 17 |
|
| 18 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
|
|
|
|
|
|
|
|
| 16 |
TOP_K_DEFAULT = int(os.getenv("TOP_K_DEFAULT", "4"))
|
| 17 |
|
| 18 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 19 |
+
HF_REPO = os.getenv("HF_REPO", "Imkkrish/rag-data") # Your HF repo for data storage
|
| 20 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
app/rag.py
CHANGED
|
@@ -10,6 +10,7 @@ import numpy as np
|
|
| 10 |
from google import genai
|
| 11 |
from sentence_transformers import SentenceTransformer
|
| 12 |
from PyPDF2 import PdfReader
|
|
|
|
| 13 |
|
| 14 |
from .config import (
|
| 15 |
INDEX_PATH,
|
|
@@ -19,6 +20,8 @@ from .config import (
|
|
| 19 |
CHUNK_SIZE,
|
| 20 |
CHUNK_OVERLAP,
|
| 21 |
GOOGLE_API_KEY,
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
_index_lock = Lock()
|
|
@@ -38,6 +41,11 @@ def _load_model():
|
|
| 38 |
|
| 39 |
def _load_metadata() -> List[Dict]:
|
| 40 |
_ensure_dirs()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if not METADATA_PATH.exists():
|
| 42 |
return []
|
| 43 |
return json.loads(METADATA_PATH.read_text())
|
|
@@ -46,10 +54,18 @@ def _load_metadata() -> List[Dict]:
|
|
| 46 |
def _save_metadata(metadata: List[Dict]):
|
| 47 |
_ensure_dirs()
|
| 48 |
METADATA_PATH.write_text(json.dumps(metadata, indent=2))
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
def _load_index(dimension: int):
|
| 52 |
_ensure_dirs()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if INDEX_PATH.exists():
|
| 54 |
return faiss.read_index(str(INDEX_PATH))
|
| 55 |
return faiss.IndexFlatIP(dimension)
|
|
@@ -58,6 +74,9 @@ def _load_index(dimension: int):
|
|
| 58 |
def _save_index(index):
|
| 59 |
_ensure_dirs()
|
| 60 |
faiss.write_index(index, str(INDEX_PATH))
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
def _normalize(embeddings: np.ndarray) -> np.ndarray:
|
|
|
|
| 10 |
from google import genai
|
| 11 |
from sentence_transformers import SentenceTransformer
|
| 12 |
from PyPDF2 import PdfReader
|
| 13 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 14 |
|
| 15 |
from .config import (
|
| 16 |
INDEX_PATH,
|
|
|
|
| 20 |
CHUNK_SIZE,
|
| 21 |
CHUNK_OVERLAP,
|
| 22 |
GOOGLE_API_KEY,
|
| 23 |
+
HF_REPO,
|
| 24 |
+
HF_TOKEN,
|
| 25 |
)
|
| 26 |
|
| 27 |
_index_lock = Lock()
|
|
|
|
| 41 |
|
| 42 |
def _load_metadata() -> List[Dict]:
|
| 43 |
_ensure_dirs()
|
| 44 |
+
if not METADATA_PATH.exists() and HF_REPO:
|
| 45 |
+
try:
|
| 46 |
+
hf_hub_download(repo_id=HF_REPO, filename="metadata.json", local_dir=DATA_DIR, token=HF_TOKEN)
|
| 47 |
+
except Exception:
|
| 48 |
+
pass # If download fails, start empty
|
| 49 |
if not METADATA_PATH.exists():
|
| 50 |
return []
|
| 51 |
return json.loads(METADATA_PATH.read_text())
|
|
|
|
| 54 |
def _save_metadata(metadata: List[Dict]):
|
| 55 |
_ensure_dirs()
|
| 56 |
METADATA_PATH.write_text(json.dumps(metadata, indent=2))
|
| 57 |
+
if HF_REPO and HF_TOKEN:
|
| 58 |
+
api = HfApi()
|
| 59 |
+
api.upload_file(path_or_fileobj=str(METADATA_PATH), path_in_repo="metadata.json", repo_id=HF_REPO, token=HF_TOKEN)
|
| 60 |
|
| 61 |
|
| 62 |
def _load_index(dimension: int):
|
| 63 |
_ensure_dirs()
|
| 64 |
+
if not INDEX_PATH.exists() and HF_REPO:
|
| 65 |
+
try:
|
| 66 |
+
hf_hub_download(repo_id=HF_REPO, filename="index.faiss", local_dir=DATA_DIR, token=HF_TOKEN)
|
| 67 |
+
except Exception:
|
| 68 |
+
pass
|
| 69 |
if INDEX_PATH.exists():
|
| 70 |
return faiss.read_index(str(INDEX_PATH))
|
| 71 |
return faiss.IndexFlatIP(dimension)
|
|
|
|
| 74 |
def _save_index(index):
|
| 75 |
_ensure_dirs()
|
| 76 |
faiss.write_index(index, str(INDEX_PATH))
|
| 77 |
+
if HF_REPO and HF_TOKEN:
|
| 78 |
+
api = HfApi()
|
| 79 |
+
api.upload_file(path_or_fileobj=str(INDEX_PATH), path_in_repo="index.faiss", repo_id=HF_REPO, token=HF_TOKEN)
|
| 80 |
|
| 81 |
|
| 82 |
def _normalize(embeddings: np.ndarray) -> np.ndarray:
|
requirements.txt
CHANGED
|
@@ -11,3 +11,4 @@ python-multipart==0.0.9
|
|
| 11 |
python-dotenv>=1.0.0
|
| 12 |
pdf2image==1.17.0
|
| 13 |
pytesseract==0.3.13
|
|
|
|
|
|
| 11 |
python-dotenv>=1.0.0
|
| 12 |
pdf2image==1.17.0
|
| 13 |
pytesseract==0.3.13
|
| 14 |
+
huggingface-hub>=0.20.0
|