Spaces:
Runtime error
Runtime error
vhr1007
commited on
Commit
·
5897f5d
1
Parent(s):
b8ef5f6
debug error
Browse files
app.py
CHANGED
|
@@ -54,16 +54,18 @@ try:
|
|
| 54 |
model = AutoModel.from_pretrained('nomic-ai/nomic-embed-text-v1.5', trust_remote_code=True)
|
| 55 |
|
| 56 |
logging.info("Successfully loaded the model and tokenizer with transformers.")
|
|
|
|
| 57 |
# Initialize the Qdrant searcher after the model is successfully loaded
|
| 58 |
global searcher # Ensure searcher is accessible globally if needed
|
| 59 |
searcher = QdrantSearcher(encoder=model, qdrant_url=qdrant_url, access_token=access_token)
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
logging.error(f"Failed to load the model: {e}")
|
| 62 |
-
raise HTTPException(status_code=500, detail="Failed to load the custom model.")
|
| 63 |
|
| 64 |
# Function to embed text using the model
|
| 65 |
-
def
|
| 66 |
-
inputs = tokenizer(
|
| 67 |
outputs = model(**inputs)
|
| 68 |
embeddings = outputs.last_hidden_state.mean(dim=1) # Example: mean pooling
|
| 69 |
return embeddings
|
|
@@ -76,10 +78,6 @@ class SearchDocumentsRequest(BaseModel):
|
|
| 76 |
class GenerateRAGRequest(BaseModel):
|
| 77 |
search_query: str
|
| 78 |
|
| 79 |
-
@app.get("/")
|
| 80 |
-
async def root():
|
| 81 |
-
return {"message": "Welcome to the Search/RAG Response API!"}
|
| 82 |
-
|
| 83 |
# Define the search documents endpoint
|
| 84 |
@app.post("/api/search-documents")
|
| 85 |
async def search_documents(
|
|
@@ -94,7 +92,13 @@ async def search_documents(
|
|
| 94 |
|
| 95 |
logging.info("Received request to search documents")
|
| 96 |
try:
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
if error:
|
| 100 |
logging.error(f"Search documents error: {error}")
|
|
@@ -119,16 +123,21 @@ async def generate_rag_response_api(
|
|
| 119 |
|
| 120 |
logging.info("Received request to generate RAG response")
|
| 121 |
try:
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if error:
|
| 125 |
logging.error(f"Search documents error: {error}")
|
| 126 |
raise HTTPException(status_code=500, detail=error)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
# embeddings = embed_texts([hit['text'] for hit in hits])
|
| 130 |
-
# Use embeddings for further processing...
|
| 131 |
|
|
|
|
| 132 |
response, error = generate_rag_response(hits, body.search_query)
|
| 133 |
|
| 134 |
if error:
|
|
|
|
| 54 |
model = AutoModel.from_pretrained('nomic-ai/nomic-embed-text-v1.5', trust_remote_code=True)
|
| 55 |
|
| 56 |
logging.info("Successfully loaded the model and tokenizer with transformers.")
|
| 57 |
+
|
| 58 |
# Initialize the Qdrant searcher after the model is successfully loaded
|
| 59 |
global searcher # Ensure searcher is accessible globally if needed
|
| 60 |
searcher = QdrantSearcher(encoder=model, qdrant_url=qdrant_url, access_token=access_token)
|
| 61 |
+
|
| 62 |
except Exception as e:
|
| 63 |
+
logging.error(f"Failed to load the model or initialize searcher: {e}")
|
| 64 |
+
raise HTTPException(status_code=500, detail="Failed to load the custom model or initialize searcher.")
|
| 65 |
|
| 66 |
# Function to embed text using the model
|
| 67 |
+
def embed_text(text):
|
| 68 |
+
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
|
| 69 |
outputs = model(**inputs)
|
| 70 |
embeddings = outputs.last_hidden_state.mean(dim=1) # Example: mean pooling
|
| 71 |
return embeddings
|
|
|
|
| 78 |
class GenerateRAGRequest(BaseModel):
|
| 79 |
search_query: str
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Define the search documents endpoint
|
| 82 |
@app.post("/api/search-documents")
|
| 83 |
async def search_documents(
|
|
|
|
| 92 |
|
| 93 |
logging.info("Received request to search documents")
|
| 94 |
try:
|
| 95 |
+
logging.info("Starting document search")
|
| 96 |
+
|
| 97 |
+
# Encode the query using the custom embedding function
|
| 98 |
+
query_embedding = embed_text(body.query)
|
| 99 |
+
|
| 100 |
+
# Assuming searcher.search_documents uses these embeddings for search
|
| 101 |
+
hits, error = searcher.search_documents("documents", query_embedding, user_id, body.limit)
|
| 102 |
|
| 103 |
if error:
|
| 104 |
logging.error(f"Search documents error: {error}")
|
|
|
|
| 123 |
|
| 124 |
logging.info("Received request to generate RAG response")
|
| 125 |
try:
|
| 126 |
+
logging.info("Starting document search")
|
| 127 |
+
|
| 128 |
+
# Encode the query using the custom embedding function
|
| 129 |
+
query_embedding = embed_text(body.search_query)
|
| 130 |
+
|
| 131 |
+
# Perform search using the encoded query
|
| 132 |
+
hits, error = searcher.search_documents("documents", query_embedding, user_id)
|
| 133 |
|
| 134 |
if error:
|
| 135 |
logging.error(f"Search documents error: {error}")
|
| 136 |
raise HTTPException(status_code=500, detail=error)
|
| 137 |
|
| 138 |
+
logging.info("Generating RAG response")
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
# Assuming generate_rag_response uses the retrieved documents to generate a response
|
| 141 |
response, error = generate_rag_response(hits, body.search_query)
|
| 142 |
|
| 143 |
if error:
|