File size: 3,086 Bytes
762c589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import shutil

# ===== STORAGE MONITOR =====
def monitor_storage():
    """Monitor disk usage on startup"""
    try:
        total, used, free = shutil.disk_usage("/")
        used_gb = used // (2**30)
        free_gb = free // (2**30)
        total_gb = total // (2**30)
        
        print(f"\n{'='*60}")
        print(f"💾 DISK USAGE:")
        print(f"   Total: {total_gb} GB")
        print(f"   Used:  {used_gb} GB")
        print(f"   Free:  {free_gb} GB")
        print(f"{'='*60}")
        
        # Check cache directories
        cache_paths = {
            "/app/.huggingface": "App Cache",
            "/tmp/.huggingface": "Temp Cache",
            "/app": "App Directory"
        }
        
        for path, name in cache_paths.items():
            if os.path.exists(path):
                try:
                    size = sum(
                        os.path.getsize(os.path.join(root, file))
                        for root, _, files in os.walk(path)
                        for file in files
                    )
                    size_gb = size / (2**30)
                    print(f"📦 {name} ({path}): {size_gb:.2f} GB")
                except Exception as e:
                    print(f"⚠️  Could not measure {name}: {e}")
        
        print(f"{'='*60}\n")
        
    except Exception as e:
        print(f"⚠️  Storage monitoring failed: {e}")

# Run storage check
monitor_storage()
# ===== END STORAGE MONITOR =====

import sys
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse

# add project root to Python path
BASE_DIR = Path(__file__).resolve().parent
sys.path.append(str(BASE_DIR))

# import backend modules
from backend.models import ChatRequest, ChatResponse
from src.chatbot import RAGChatBot

# Initialize chatbot
bot = RAGChatBot()

# Log FAISS index size safely
try:
    size = getattr(bot, "index_size", None)
    if size is None and hasattr(bot, "index"):
        # if RAGChatBot has .index (FAISS object) instead
        size = len(bot.index) if hasattr(bot.index, "__len__") else "unknown"
    print(f"✅ FAISS index size: {size}")
except Exception as e:
    print(f"⚠️ Could not determine FAISS index size: {e}")

# Create FastAPI app
app = FastAPI(title="Odisha Disaster Chatbot")

# Enable CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Serve frontend static files
frontend_dir = BASE_DIR / "frontend"
app.mount("/static", StaticFiles(directory=str(frontend_dir)), name="static")

# Serve index.html at root
@app.get("/")
def read_index():
    return FileResponse(frontend_dir / "index.html")

# Backend chat API
@app.post("/api/chat", response_model=ChatResponse)
def chat(request: ChatRequest):
    answer = bot.chat(request.query)
    return ChatResponse(answer=answer)

# Optional health check
@app.get("/health")
def health():
    return {"status": "ok"}