Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,13 @@ from utils.auth import token_required
|
|
9 |
from dotenv import load_dotenv
|
10 |
import os
|
11 |
|
12 |
-
|
|
|
13 |
|
|
|
14 |
app = FastAPI()
|
15 |
|
|
|
16 |
os.environ["HF_HOME"] = "/tmp/huggingface_cache"
|
17 |
|
18 |
# Ensure the cache directory exists
|
@@ -22,23 +25,38 @@ if not os.path.exists(cache_dir):
|
|
22 |
|
23 |
# Setup logging
|
24 |
logging.basicConfig(level=logging.INFO)
|
|
|
25 |
# Load Hugging Face token from environment variable
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
# else:
|
32 |
-
# raise ValueError("Hugging Face token is not set. Please set the HUGGINGFACE_HUB_TOKEN environment variable.")
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Initialize the Qdrant searcher
|
36 |
-
qdrant_url = os.getenv('QDRANT_URL')
|
37 |
-
access_token = os.getenv('QDRANT_ACCESS_TOKEN')
|
38 |
-
encoder = SentenceTransformer('nomic-ai/nomic-embed-text-v1.5')
|
39 |
searcher = QdrantSearcher(encoder, qdrant_url, access_token)
|
40 |
|
41 |
-
#
|
42 |
class SearchDocumentsRequest(BaseModel):
|
43 |
query: str
|
44 |
limit: int = 3
|
@@ -46,6 +64,7 @@ class SearchDocumentsRequest(BaseModel):
|
|
46 |
class GenerateRAGRequest(BaseModel):
|
47 |
search_query: str
|
48 |
|
|
|
49 |
@app.post("/api/search-documents")
|
50 |
async def search_documents(
|
51 |
body: SearchDocumentsRequest,
|
@@ -53,15 +72,13 @@ async def search_documents(
|
|
53 |
):
|
54 |
customer_id, user_id = credentials
|
55 |
|
56 |
-
# Check if customer_id or user_id is missing
|
57 |
if not customer_id or not user_id:
|
58 |
logging.error("Failed to extract customer_id or user_id from the JWT token.")
|
59 |
raise HTTPException(status_code=401, detail="Invalid token: missing customer_id or user_id")
|
60 |
|
61 |
logging.info("Received request to search documents")
|
62 |
try:
|
63 |
-
|
64 |
-
hits, error = searcher.search_documents(collection_name, body.query, user_id, body.limit)
|
65 |
|
66 |
if error:
|
67 |
logging.error(f"Search documents error: {error}")
|
@@ -72,6 +89,7 @@ async def search_documents(
|
|
72 |
logging.error(f"Unexpected error: {e}")
|
73 |
raise HTTPException(status_code=500, detail=str(e))
|
74 |
|
|
|
75 |
@app.post("/api/generate-rag-response")
|
76 |
async def generate_rag_response_api(
|
77 |
body: GenerateRAGRequest,
|
@@ -79,15 +97,13 @@ async def generate_rag_response_api(
|
|
79 |
):
|
80 |
customer_id, user_id = credentials
|
81 |
|
82 |
-
# Check if customer_id or user_id is missing
|
83 |
if not customer_id or not user_id:
|
84 |
logging.error("Failed to extract customer_id or user_id from the JWT token.")
|
85 |
raise HTTPException(status_code=401, detail="Invalid token: missing customer_id or user_id")
|
86 |
|
87 |
logging.info("Received request to generate RAG response")
|
88 |
try:
|
89 |
-
|
90 |
-
hits, error = searcher.search_documents(collection_name, body.search_query, user_id)
|
91 |
|
92 |
if error:
|
93 |
logging.error(f"Search documents error: {error}")
|
@@ -106,4 +122,4 @@ async def generate_rag_response_api(
|
|
106 |
|
107 |
if __name__ == '__main__':
|
108 |
import uvicorn
|
109 |
-
uvicorn.run(app, host='0.0.0.0', port=8000)
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
import os
|
11 |
|
12 |
+
# Load environment variables from .env file
|
13 |
+
load_dotenv()
|
14 |
|
15 |
+
# Initialize FastAPI application
|
16 |
app = FastAPI()
|
17 |
|
18 |
+
# Set the cache directory for Hugging Face
|
19 |
os.environ["HF_HOME"] = "/tmp/huggingface_cache"
|
20 |
|
21 |
# Ensure the cache directory exists
|
|
|
25 |
|
26 |
# Setup logging
|
27 |
logging.basicConfig(level=logging.INFO)
|
28 |
+
|
29 |
# Load Hugging Face token from environment variable
|
30 |
+
huggingface_token = os.getenv('HUGGINGFACE_HUB_TOKEN')
|
31 |
+
if huggingface_token:
|
32 |
+
try:
|
33 |
+
login(token=huggingface_token, add_to_git_credential=True)
|
34 |
+
logging.info("Successfully logged into Hugging Face Hub.")
|
35 |
+
except Exception as e:
|
36 |
+
logging.error(f"Failed to log into Hugging Face Hub: {e}")
|
37 |
+
raise HTTPException(status_code=500, detail="Failed to log into Hugging Face Hub.")
|
38 |
+
else:
|
39 |
+
raise ValueError("Hugging Face token is not set. Please set the HUGGINGFACE_HUB_TOKEN environment variable.")
|
40 |
|
41 |
+
# Initialize the Qdrant searcher
|
42 |
+
qdrant_url = os.getenv('QDRANT_URL')
|
43 |
+
access_token = os.getenv('QDRANT_ACCESS_TOKEN')
|
44 |
|
45 |
+
if not qdrant_url or not access_token:
|
46 |
+
raise ValueError("Qdrant URL or Access Token is not set. Please set the QDRANT_URL and QDRANT_ACCESS_TOKEN environment variables.")
|
|
|
|
|
47 |
|
48 |
+
# Initialize the SentenceTransformer model
|
49 |
+
try:
|
50 |
+
encoder = SentenceTransformer('nomic-ai/nomic-embed-text-v1.5')
|
51 |
+
logging.info("Successfully loaded the SentenceTransformer model.")
|
52 |
+
except Exception as e:
|
53 |
+
logging.error(f"Failed to load the SentenceTransformer model: {e}")
|
54 |
+
raise HTTPException(status_code=500, detail="Failed to load the SentenceTransformer model.")
|
55 |
|
56 |
# Initialize the Qdrant searcher
|
|
|
|
|
|
|
57 |
searcher = QdrantSearcher(encoder, qdrant_url, access_token)
|
58 |
|
59 |
+
# Define the request body models
|
60 |
class SearchDocumentsRequest(BaseModel):
|
61 |
query: str
|
62 |
limit: int = 3
|
|
|
64 |
class GenerateRAGRequest(BaseModel):
|
65 |
search_query: str
|
66 |
|
67 |
+
# Define the search documents endpoint
|
68 |
@app.post("/api/search-documents")
|
69 |
async def search_documents(
|
70 |
body: SearchDocumentsRequest,
|
|
|
72 |
):
|
73 |
customer_id, user_id = credentials
|
74 |
|
|
|
75 |
if not customer_id or not user_id:
|
76 |
logging.error("Failed to extract customer_id or user_id from the JWT token.")
|
77 |
raise HTTPException(status_code=401, detail="Invalid token: missing customer_id or user_id")
|
78 |
|
79 |
logging.info("Received request to search documents")
|
80 |
try:
|
81 |
+
hits, error = searcher.search_documents("documents", body.query, user_id, body.limit)
|
|
|
82 |
|
83 |
if error:
|
84 |
logging.error(f"Search documents error: {error}")
|
|
|
89 |
logging.error(f"Unexpected error: {e}")
|
90 |
raise HTTPException(status_code=500, detail=str(e))
|
91 |
|
92 |
+
# Define the generate RAG response endpoint
|
93 |
@app.post("/api/generate-rag-response")
|
94 |
async def generate_rag_response_api(
|
95 |
body: GenerateRAGRequest,
|
|
|
97 |
):
|
98 |
customer_id, user_id = credentials
|
99 |
|
|
|
100 |
if not customer_id or not user_id:
|
101 |
logging.error("Failed to extract customer_id or user_id from the JWT token.")
|
102 |
raise HTTPException(status_code=401, detail="Invalid token: missing customer_id or user_id")
|
103 |
|
104 |
logging.info("Received request to generate RAG response")
|
105 |
try:
|
106 |
+
hits, error = searcher.search_documents("documents", body.search_query, user_id)
|
|
|
107 |
|
108 |
if error:
|
109 |
logging.error(f"Search documents error: {error}")
|
|
|
122 |
|
123 |
if __name__ == '__main__':
|
124 |
import uvicorn
|
125 |
+
uvicorn.run(app, host='0.0.0.0', port=8000)
|