Spaces:
Runtime error
Runtime error
Commit ·
123b9a8
1
Parent(s): 225f40f
- __pycache__/app.cpython-313.pyc +0 -0
- app.py +45 -7
- req.txt +8 -9
__pycache__/app.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -6,6 +6,12 @@ import asyncio
|
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
import tempfile
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import requests
|
| 10 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -39,11 +45,15 @@ from qdrant_client.http.models import Distance, VectorParams, PointStruct
|
|
| 39 |
logging.basicConfig(level=logging.INFO)
|
| 40 |
logger = logging.getLogger(__name__)
|
| 41 |
|
| 42 |
-
# Environment variables
|
| 43 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 44 |
QDRANT_URL = os.getenv("QDRANT_URL")
|
| 45 |
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if not GROQ_API_KEY:
|
| 48 |
raise ValueError("GROQ_API_KEY environment variable is required")
|
| 49 |
|
|
@@ -79,16 +89,38 @@ async def lifespan(app: FastAPI):
|
|
| 79 |
verify=False
|
| 80 |
)
|
| 81 |
|
| 82 |
-
# Initialize embeddings model
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Initialize LLM
|
| 89 |
llm = ChatGroq(
|
| 90 |
groq_api_key=GROQ_API_KEY,
|
| 91 |
-
model_name="
|
| 92 |
temperature=0
|
| 93 |
)
|
| 94 |
|
|
@@ -285,6 +317,9 @@ async def extract_pdf_pdfplumber(pdf_path: str) -> str:
|
|
| 285 |
async def store_in_qdrant(documents: List[Document], collection_name: str):
|
| 286 |
"""Store documents in Qdrant vector database"""
|
| 287 |
try:
|
|
|
|
|
|
|
|
|
|
| 288 |
# Create collection if it doesn't exist
|
| 289 |
try:
|
| 290 |
qdrant_client.get_collection(collection_name)
|
|
@@ -329,6 +364,9 @@ async def store_in_qdrant(documents: List[Document], collection_name: str):
|
|
| 329 |
def retriever_tool(query: str, collection_name: str) -> str:
|
| 330 |
"""Retrieve relevant documents from Qdrant based on the query."""
|
| 331 |
try:
|
|
|
|
|
|
|
|
|
|
| 332 |
# Generate query embedding
|
| 333 |
query_embedding = embeddings_model.embed_query(query)
|
| 334 |
|
|
|
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
+
# Set environment variables for Hugging Face Spaces
|
| 10 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
| 11 |
+
os.environ["HF_HOME"] = "/tmp/hf_home"
|
| 12 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/hf_hub_cache"
|
| 13 |
+
os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/tmp/sentence_transformers"
|
| 14 |
+
|
| 15 |
import requests
|
| 16 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 17 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 45 |
logging.basicConfig(level=logging.INFO)
|
| 46 |
logger = logging.getLogger(__name__)
|
| 47 |
|
| 48 |
+
# Environment variables - Use environment variables or fallback to defaults
|
| 49 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 50 |
QDRANT_URL = os.getenv("QDRANT_URL")
|
| 51 |
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY")
|
| 52 |
|
| 53 |
+
# Create cache directories if they don't exist
|
| 54 |
+
for cache_dir in ["/tmp/transformers_cache", "/tmp/hf_home", "/tmp/hf_hub_cache", "/tmp/sentence_transformers"]:
|
| 55 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 56 |
+
|
| 57 |
if not GROQ_API_KEY:
|
| 58 |
raise ValueError("GROQ_API_KEY environment variable is required")
|
| 59 |
|
|
|
|
| 89 |
verify=False
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# Initialize embeddings model with error handling
|
| 93 |
+
try:
|
| 94 |
+
# Try to initialize with custom cache directory
|
| 95 |
+
embeddings_model = HuggingFaceEmbeddings(
|
| 96 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 97 |
+
model_kwargs={
|
| 98 |
+
'device': 'cpu',
|
| 99 |
+
'cache_folder': '/tmp/sentence_transformers'
|
| 100 |
+
},
|
| 101 |
+
cache_folder='/tmp/sentence_transformers'
|
| 102 |
+
)
|
| 103 |
+
logger.info("HuggingFace embeddings initialized successfully")
|
| 104 |
+
except Exception as e:
|
| 105 |
+
logger.error(f"Failed to initialize HuggingFace embeddings: {e}")
|
| 106 |
+
# Try with a smaller model that might work better
|
| 107 |
+
try:
|
| 108 |
+
embeddings_model = HuggingFaceEmbeddings(
|
| 109 |
+
model_name="all-MiniLM-L6-v2", # Try without the prefix
|
| 110 |
+
model_kwargs={'device': 'cpu'},
|
| 111 |
+
cache_folder='/tmp/sentence_transformers'
|
| 112 |
+
)
|
| 113 |
+
logger.info("HuggingFace embeddings initialized with fallback model")
|
| 114 |
+
except Exception as e2:
|
| 115 |
+
logger.error(f"Failed to initialize fallback embeddings: {e2}")
|
| 116 |
+
# Use a minimal fallback - this will cause issues but app will start
|
| 117 |
+
embeddings_model = None
|
| 118 |
+
logger.warning("No embeddings model available - embedding operations will fail")
|
| 119 |
|
| 120 |
# Initialize LLM
|
| 121 |
llm = ChatGroq(
|
| 122 |
groq_api_key=GROQ_API_KEY,
|
| 123 |
+
model_name="mixtral-8x7b-32768",
|
| 124 |
temperature=0
|
| 125 |
)
|
| 126 |
|
|
|
|
| 317 |
async def store_in_qdrant(documents: List[Document], collection_name: str):
|
| 318 |
"""Store documents in Qdrant vector database"""
|
| 319 |
try:
|
| 320 |
+
if embeddings_model is None:
|
| 321 |
+
raise ValueError("Embeddings model not available")
|
| 322 |
+
|
| 323 |
# Create collection if it doesn't exist
|
| 324 |
try:
|
| 325 |
qdrant_client.get_collection(collection_name)
|
|
|
|
| 364 |
def retriever_tool(query: str, collection_name: str) -> str:
|
| 365 |
"""Retrieve relevant documents from Qdrant based on the query."""
|
| 366 |
try:
|
| 367 |
+
if embeddings_model is None:
|
| 368 |
+
return "Embeddings model not available. Cannot perform retrieval."
|
| 369 |
+
|
| 370 |
# Generate query embedding
|
| 371 |
query_embedding = embeddings_model.embed_query(query)
|
| 372 |
|
req.txt
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
langchain
|
| 4 |
langchain-core
|
| 5 |
langchain-groq
|
| 6 |
-
langchain-
|
| 7 |
langgraph
|
| 8 |
-
docling
|
| 9 |
qdrant-client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
sentence-transformers
|
| 11 |
transformers
|
| 12 |
torch
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
python-multipart
|
| 16 |
-
numpy
|
| 17 |
-
pandas
|
| 18 |
-
Pillow
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
|
|
|
| 3 |
langchain-core
|
| 4 |
langchain-groq
|
| 5 |
+
langchain-huggingface
|
| 6 |
langgraph
|
|
|
|
| 7 |
qdrant-client
|
| 8 |
+
docling
|
| 9 |
+
PyPDF2
|
| 10 |
+
pdfplumber
|
| 11 |
+
requests
|
| 12 |
+
pydantic
|
| 13 |
sentence-transformers
|
| 14 |
transformers
|
| 15 |
torch
|
| 16 |
+
torchvision
|
| 17 |
+
torchaudio
|
|
|
|
|
|
|
|
|
|
|
|