fixing
Browse files- api.py +16 -12
- index.py +11 -1
- {models β tmp/models}/.gitkeep +0 -0
- {offload β tmp/offload}/.gitkeep +0 -0
api.py
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
@@ -5,7 +14,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndB
|
|
| 5 |
from langchain_community.llms import HuggingFacePipeline
|
| 6 |
from qdrant_client import QdrantClient
|
| 7 |
from langchain_qdrant import QdrantVectorStore
|
| 8 |
-
import os
|
| 9 |
from pydantic import BaseModel
|
| 10 |
from langchain.chains import RetrievalQA
|
| 11 |
from langchain.schema import Document
|
|
@@ -25,11 +33,7 @@ class Item(BaseModel):
|
|
| 25 |
query: str
|
| 26 |
|
| 27 |
app = FastAPI()
|
| 28 |
-
|
| 29 |
-
app.mount("/TestFolder", StaticFiles(directory="./TestFolder"), name="TestFolder")
|
| 30 |
-
os.makedirs("./cache", exist_ok=True)
|
| 31 |
-
os.makedirs("./offload", exist_ok=True)
|
| 32 |
-
os.makedirs("./models", exist_ok=True)
|
| 33 |
|
| 34 |
@app.on_event("startup")
|
| 35 |
async def startup_event():
|
|
@@ -41,10 +45,10 @@ async def startup_event():
|
|
| 41 |
start_time = time.perf_counter()
|
| 42 |
|
| 43 |
embed_model = HuggingFaceEmbeddings(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
)
|
| 49 |
|
| 50 |
try:
|
|
@@ -54,8 +58,8 @@ async def startup_event():
|
|
| 54 |
print(f"β Error initializing Qdrant: {e}")
|
| 55 |
|
| 56 |
model_path = "distilbert-base-cased-distilled-squad"
|
| 57 |
-
model = AutoModelForQuestionAnswering.from_pretrained(model_path, cache_dir=
|
| 58 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, cache_dir=
|
| 59 |
qa_pipeline = pipeline(
|
| 60 |
"question-answering",
|
| 61 |
model=model,
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Set a writable directory for Hugging Face cache and environment variables
|
| 4 |
+
hf_cache_dir = "/tmp/huggingface_cache"
|
| 5 |
+
os.environ["HF_HOME"] = hf_cache_dir
|
| 6 |
+
os.environ["TRANSFORMERS_CACHE"] = os.path.join(hf_cache_dir, "transformers")
|
| 7 |
+
os.makedirs(hf_cache_dir, exist_ok=True)
|
| 8 |
+
os.makedirs(os.environ["TRANSFORMERS_CACHE"], exist_ok=True)
|
| 9 |
+
|
| 10 |
from fastapi import FastAPI
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
| 14 |
from langchain_community.llms import HuggingFacePipeline
|
| 15 |
from qdrant_client import QdrantClient
|
| 16 |
from langchain_qdrant import QdrantVectorStore
|
|
|
|
| 17 |
from pydantic import BaseModel
|
| 18 |
from langchain.chains import RetrievalQA
|
| 19 |
from langchain.schema import Document
|
|
|
|
| 33 |
query: str
|
| 34 |
|
| 35 |
app = FastAPI()
|
| 36 |
+
# app.mount("/TestFolder", StaticFiles(directory="./TestFolder"), name="TestFolder")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@app.on_event("startup")
|
| 39 |
async def startup_event():
|
|
|
|
| 45 |
start_time = time.perf_counter()
|
| 46 |
|
| 47 |
embed_model = HuggingFaceEmbeddings(
|
| 48 |
+
model_name=sentence_embedding_model_path,
|
| 49 |
+
model_kwargs={"device": "cpu"},
|
| 50 |
+
encode_kwargs={"normalize_embeddings": True},
|
| 51 |
+
cache_folder=hf_cache_dir,
|
| 52 |
)
|
| 53 |
|
| 54 |
try:
|
|
|
|
| 58 |
print(f"β Error initializing Qdrant: {e}")
|
| 59 |
|
| 60 |
model_path = "distilbert-base-cased-distilled-squad"
|
| 61 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_path, cache_dir=hf_cache_dir)
|
| 62 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, cache_dir=hf_cache_dir)
|
| 63 |
qa_pipeline = pipeline(
|
| 64 |
"question-answering",
|
| 65 |
model=model,
|
index.py
CHANGED
|
@@ -13,6 +13,16 @@ from qdrant_client.models import Distance, VectorParams
|
|
| 13 |
import docx
|
| 14 |
import os
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def get_files(dir):
|
| 17 |
file_list = []
|
| 18 |
for dir, _, filenames in os.walk(dir):
|
|
@@ -48,7 +58,7 @@ def main_indexing(mypath):
|
|
| 48 |
model_name=model_name,
|
| 49 |
model_kwargs=model_kwargs,
|
| 50 |
encode_kwargs=encode_kwargs,
|
| 51 |
-
|
| 52 |
)
|
| 53 |
client = QdrantClient(path="qdrant/")
|
| 54 |
collection_name = "MyCollection"
|
|
|
|
| 13 |
import docx
|
| 14 |
import os
|
| 15 |
|
| 16 |
+
# Set a writable directory for Hugging Face cache and environment variables
|
| 17 |
+
hf_cache_dir = "/tmp/huggingface_cache"
|
| 18 |
+
transformers_cache_dir = os.path.join(hf_cache_dir, "transformers")
|
| 19 |
+
os.environ["HF_HOME"] = hf_cache_dir
|
| 20 |
+
os.environ["TRANSFORMERS_CACHE"] = transformers_cache_dir
|
| 21 |
+
|
| 22 |
+
# Ensure the writable directories exist
|
| 23 |
+
os.makedirs(hf_cache_dir, exist_ok=True)
|
| 24 |
+
os.makedirs(transformers_cache_dir, exist_ok=True)
|
| 25 |
+
|
| 26 |
def get_files(dir):
|
| 27 |
file_list = []
|
| 28 |
for dir, _, filenames in os.walk(dir):
|
|
|
|
| 58 |
model_name=model_name,
|
| 59 |
model_kwargs=model_kwargs,
|
| 60 |
encode_kwargs=encode_kwargs,
|
| 61 |
+
cache_folder=hf_cache_dir,
|
| 62 |
)
|
| 63 |
client = QdrantClient(path="qdrant/")
|
| 64 |
collection_name = "MyCollection"
|
{models β tmp/models}/.gitkeep
RENAMED
|
File without changes
|
{offload β tmp/offload}/.gitkeep
RENAMED
|
File without changes
|