Add directory creation and health check functionality in app.py
Browse files
app.py
CHANGED
|
@@ -23,9 +23,42 @@ from typing import Union
|
|
| 23 |
# Initialize environment variables
|
| 24 |
load_dotenv()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
app = FastAPI(title="Status Law Assistant API")
|
| 27 |
app.include_router(analysis_router)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Add custom exception handlers
|
| 30 |
@app.exception_handler(requests.exceptions.RequestException)
|
| 31 |
async def network_error_handler(request: Request, exc: requests.exceptions.RequestException):
|
|
@@ -66,7 +99,6 @@ def init_models():
|
|
| 66 |
raise HTTPException(status_code=500, detail=f"Model initialization failed: {str(e)}")
|
| 67 |
|
| 68 |
# --------------- Knowledge Base Management ---------------
|
| 69 |
-
VECTOR_STORE_PATH = "vector_store"
|
| 70 |
URLS = [
|
| 71 |
"https://status.law",
|
| 72 |
"https://status.law/about",
|
|
@@ -89,7 +121,9 @@ def build_knowledge_base(_embeddings):
|
|
| 89 |
start_time = time.time()
|
| 90 |
documents = []
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
for url in URLS:
|
| 95 |
try:
|
|
@@ -254,6 +288,23 @@ async def health_check():
|
|
| 254 |
}
|
| 255 |
)
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
if __name__ == "__main__":
|
| 258 |
import uvicorn
|
| 259 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 23 |
# Initialize environment variables
|
| 24 |
load_dotenv()
|
| 25 |
|
| 26 |
+
# Define constants for directory paths
|
| 27 |
+
VECTOR_STORE_PATH = "vector_store"
|
| 28 |
+
CHAT_HISTORY_PATH = "chat_history"
|
| 29 |
+
|
| 30 |
+
def create_required_directories():
|
| 31 |
+
"""Create required directories if they don't exist"""
|
| 32 |
+
directories = [VECTOR_STORE_PATH, CHAT_HISTORY_PATH]
|
| 33 |
+
for directory in directories:
|
| 34 |
+
try:
|
| 35 |
+
if not os.path.exists(directory):
|
| 36 |
+
os.makedirs(directory, exist_ok=True)
|
| 37 |
+
print(f"Created directory: {directory}")
|
| 38 |
+
|
| 39 |
+
# Create .gitkeep file to preserve empty directory
|
| 40 |
+
gitkeep_path = os.path.join(directory, '.gitkeep')
|
| 41 |
+
with open(gitkeep_path, 'w') as f:
|
| 42 |
+
pass
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error creating directory {directory}: {str(e)}")
|
| 45 |
+
raise HTTPException(
|
| 46 |
+
status_code=500,
|
| 47 |
+
detail=f"Failed to create required directory: {directory}"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Create directories before initializing the app
|
| 51 |
+
create_required_directories()
|
| 52 |
+
|
| 53 |
app = FastAPI(title="Status Law Assistant API")
|
| 54 |
app.include_router(analysis_router)
|
| 55 |
|
| 56 |
+
# Add startup event handler to ensure directories exist
|
| 57 |
+
@app.on_event("startup")
|
| 58 |
+
async def startup_event():
|
| 59 |
+
"""Ensure required directories exist on startup"""
|
| 60 |
+
create_required_directories()
|
| 61 |
+
|
| 62 |
# Add custom exception handlers
|
| 63 |
@app.exception_handler(requests.exceptions.RequestException)
|
| 64 |
async def network_error_handler(request: Request, exc: requests.exceptions.RequestException):
|
|
|
|
| 99 |
raise HTTPException(status_code=500, detail=f"Model initialization failed: {str(e)}")
|
| 100 |
|
| 101 |
# --------------- Knowledge Base Management ---------------
|
|
|
|
| 102 |
URLS = [
|
| 103 |
"https://status.law",
|
| 104 |
"https://status.law/about",
|
|
|
|
| 121 |
start_time = time.time()
|
| 122 |
documents = []
|
| 123 |
|
| 124 |
+
# Ensure vector store directory exists
|
| 125 |
+
if not os.path.exists(VECTOR_STORE_PATH):
|
| 126 |
+
os.makedirs(VECTOR_STORE_PATH, exist_ok=True)
|
| 127 |
|
| 128 |
for url in URLS:
|
| 129 |
try:
|
|
|
|
| 288 |
}
|
| 289 |
)
|
| 290 |
|
| 291 |
+
# Add diagnostic endpoint
|
| 292 |
+
@app.get("/directory-status")
|
| 293 |
+
async def check_directory_status():
|
| 294 |
+
"""Check status of required directories"""
|
| 295 |
+
return {
|
| 296 |
+
"vector_store": {
|
| 297 |
+
"exists": os.path.exists(VECTOR_STORE_PATH),
|
| 298 |
+
"path": os.path.abspath(VECTOR_STORE_PATH),
|
| 299 |
+
"contents": os.listdir(VECTOR_STORE_PATH) if os.path.exists(VECTOR_STORE_PATH) else []
|
| 300 |
+
},
|
| 301 |
+
"chat_history": {
|
| 302 |
+
"exists": os.path.exists(CHAT_HISTORY_PATH),
|
| 303 |
+
"path": os.path.abspath(CHAT_HISTORY_PATH),
|
| 304 |
+
"contents": os.listdir(CHAT_HISTORY_PATH) if os.path.exists(CHAT_HISTORY_PATH) else []
|
| 305 |
+
}
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
if __name__ == "__main__":
|
| 309 |
import uvicorn
|
| 310 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|