Updated with Storage Monitor
Browse files
app.py
CHANGED
|
@@ -1,59 +1,107 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
|
| 4 |
+
# ===== STORAGE MONITOR =====
|
| 5 |
+
def monitor_storage():
|
| 6 |
+
"""Monitor disk usage on startup"""
|
| 7 |
+
try:
|
| 8 |
+
total, used, free = shutil.disk_usage("/")
|
| 9 |
+
used_gb = used // (2**30)
|
| 10 |
+
free_gb = free // (2**30)
|
| 11 |
+
total_gb = total // (2**30)
|
| 12 |
+
|
| 13 |
+
print(f"\n{'='*60}")
|
| 14 |
+
print(f"💾 DISK USAGE:")
|
| 15 |
+
print(f" Total: {total_gb} GB")
|
| 16 |
+
print(f" Used: {used_gb} GB")
|
| 17 |
+
print(f" Free: {free_gb} GB")
|
| 18 |
+
print(f"{'='*60}")
|
| 19 |
+
|
| 20 |
+
# Check cache directories
|
| 21 |
+
cache_paths = {
|
| 22 |
+
"/app/.huggingface": "App Cache",
|
| 23 |
+
"/tmp/.huggingface": "Temp Cache",
|
| 24 |
+
"/app": "App Directory"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
for path, name in cache_paths.items():
|
| 28 |
+
if os.path.exists(path):
|
| 29 |
+
try:
|
| 30 |
+
size = sum(
|
| 31 |
+
os.path.getsize(os.path.join(root, file))
|
| 32 |
+
for root, _, files in os.walk(path)
|
| 33 |
+
for file in files
|
| 34 |
+
)
|
| 35 |
+
size_gb = size / (2**30)
|
| 36 |
+
print(f"📦 {name} ({path}): {size_gb:.2f} GB")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"⚠️ Could not measure {name}: {e}")
|
| 39 |
+
|
| 40 |
+
print(f"{'='*60}\n")
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"⚠️ Storage monitoring failed: {e}")
|
| 44 |
+
|
| 45 |
+
# Run storage check
|
| 46 |
+
monitor_storage()
|
| 47 |
+
# ===== END STORAGE MONITOR =====
|
| 48 |
+
|
| 49 |
+
import sys
|
| 50 |
+
from pathlib import Path
|
| 51 |
+
from fastapi import FastAPI
|
| 52 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 53 |
+
from fastapi.staticfiles import StaticFiles
|
| 54 |
+
from fastapi.responses import FileResponse
|
| 55 |
+
|
| 56 |
+
# add project root to Python path
|
| 57 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 58 |
+
sys.path.append(str(BASE_DIR))
|
| 59 |
+
|
| 60 |
+
# import backend modules
|
| 61 |
+
from backend.models import ChatRequest, ChatResponse
|
| 62 |
+
from src.chatbot import RAGChatBot
|
| 63 |
+
|
| 64 |
+
# Initialize chatbot
|
| 65 |
+
bot = RAGChatBot()
|
| 66 |
+
|
| 67 |
+
# Log FAISS index size safely
|
| 68 |
+
try:
|
| 69 |
+
size = getattr(bot, "index_size", None)
|
| 70 |
+
if size is None and hasattr(bot, "index"):
|
| 71 |
+
# if RAGChatBot has .index (FAISS object) instead
|
| 72 |
+
size = len(bot.index) if hasattr(bot.index, "__len__") else "unknown"
|
| 73 |
+
print(f"✅ FAISS index size: {size}")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"⚠️ Could not determine FAISS index size: {e}")
|
| 76 |
+
|
| 77 |
+
# Create FastAPI app
|
| 78 |
+
app = FastAPI(title="Odisha Disaster Chatbot")
|
| 79 |
+
|
| 80 |
+
# Enable CORS
|
| 81 |
+
app.add_middleware(
|
| 82 |
+
CORSMiddleware,
|
| 83 |
+
allow_origins=["*"],
|
| 84 |
+
allow_credentials=True,
|
| 85 |
+
allow_methods=["*"],
|
| 86 |
+
allow_headers=["*"],
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Serve frontend static files
|
| 90 |
+
frontend_dir = BASE_DIR / "frontend"
|
| 91 |
+
app.mount("/static", StaticFiles(directory=str(frontend_dir)), name="static")
|
| 92 |
+
|
| 93 |
+
# Serve index.html at root
|
| 94 |
+
@app.get("/")
|
| 95 |
+
def read_index():
|
| 96 |
+
return FileResponse(frontend_dir / "index.html")
|
| 97 |
+
|
| 98 |
+
# Backend chat API
|
| 99 |
+
@app.post("/api/chat", response_model=ChatResponse)
|
| 100 |
+
def chat(request: ChatRequest):
|
| 101 |
+
answer = bot.chat(request.query)
|
| 102 |
+
return ChatResponse(answer=answer)
|
| 103 |
+
|
| 104 |
+
# Optional health check
|
| 105 |
+
@app.get("/health")
|
| 106 |
+
def health():
|
| 107 |
+
return {"status": "ok"}
|