Spaces:
Sleeping
Sleeping
Edited Docker file again
Browse files- rag_pipeline/llms.py +31 -24
- requirements.txt +3 -0
- streamlit_app.py +4 -0
rag_pipeline/llms.py
CHANGED
|
@@ -1,47 +1,54 @@
|
|
| 1 |
-
#
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from langchain.embeddings import CacheBackedEmbeddings
|
| 4 |
from langchain.storage import LocalFileStore
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
-
from
|
| 7 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
import sys
|
| 11 |
-
import os
|
| 12 |
-
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
| 15 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
|
| 17 |
if parent_dir not in sys.path:
|
| 18 |
sys.path.insert(0, parent_dir)
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
chat_model = ChatGroq(
|
| 24 |
-
model
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
max_retries = 2,
|
| 30 |
)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
store = LocalFileStore("./cache/")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
underlying_embeddings = HuggingFaceEmbeddings(
|
| 38 |
-
model_name="
|
| 39 |
-
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
Embeddings = CacheBackedEmbeddings.from_bytes_store(
|
| 44 |
underlying_embeddings,
|
| 45 |
store,
|
| 46 |
-
namespace="
|
| 47 |
-
)
|
|
|
|
| 1 |
+
# llms.py
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
from langchain.embeddings import CacheBackedEmbeddings
|
| 6 |
from langchain.storage import LocalFileStore
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_huggingface import HuggingFaceEmbeddings # Updated import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
+
# π§ Path Setup for Modular Imports
|
| 12 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
|
| 15 |
if parent_dir not in sys.path:
|
| 16 |
sys.path.insert(0, parent_dir)
|
| 17 |
|
| 18 |
+
from config import set_environment # Ensure env vars are loaded early
|
| 19 |
|
| 20 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
# π¬ Chat Model Initialization
|
| 22 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
chat_model = ChatGroq(
|
| 24 |
+
model="deepseek-r1-distill-llama-70b",
|
| 25 |
+
temperature=0,
|
| 26 |
+
max_tokens=1024,
|
| 27 |
+
timeout=None,
|
| 28 |
+
max_retries=2,
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
+
# π§ Embedding Setup with Caching
|
| 33 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
|
| 35 |
+
# Ensure Hugging Face cache is writable (esp. on Spaces)
|
| 36 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 37 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
| 38 |
+
|
| 39 |
+
# Local byte store for embedding cache
|
| 40 |
store = LocalFileStore("./cache/")
|
| 41 |
|
| 42 |
+
# Updated embedding class from langchain-huggingface
|
| 43 |
underlying_embeddings = HuggingFaceEmbeddings(
|
| 44 |
+
model_name="BAAI/bge-small-en-v1.5", # More stable than intfloat/e5-base
|
| 45 |
+
model_kwargs={"device": "cpu"},
|
| 46 |
+
encode_kwargs={"normalize_embeddings": True}
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Wrap with LangChain's cache-backed embedding layer
|
| 50 |
Embeddings = CacheBackedEmbeddings.from_bytes_store(
|
| 51 |
underlying_embeddings,
|
| 52 |
store,
|
| 53 |
+
namespace="bge-small-en-v1.5"
|
| 54 |
+
)
|
requirements.txt
CHANGED
|
@@ -33,4 +33,7 @@ python-docx
|
|
| 33 |
unstructured
|
| 34 |
beautifulsoup4
|
| 35 |
html2text
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 33 |
unstructured
|
| 34 |
beautifulsoup4
|
| 35 |
html2text
|
| 36 |
+
langchain-huggingface>=0.3.1
|
| 37 |
+
sentence-transformers>=2.6.1
|
| 38 |
+
transformers>=4.40.0
|
| 39 |
|
streamlit_app.py
CHANGED
|
@@ -5,6 +5,10 @@ import os
|
|
| 5 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
sys.path.append(current_dir)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Import and run the actual Streamlit app
|
| 9 |
from rag_pipeline import rag_streamlit_app
|
| 10 |
|
|
|
|
| 5 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 6 |
sys.path.append(current_dir)
|
| 7 |
|
| 8 |
+
import torch
|
| 9 |
+
torch.classes.__path__ = [] # Suppress PyTorch class instantiation error
|
| 10 |
+
|
| 11 |
+
|
| 12 |
# Import and run the actual Streamlit app
|
| 13 |
from rag_pipeline import rag_streamlit_app
|
| 14 |
|