Subhakanta156 commited on
Commit
762c589
·
verified ·
1 Parent(s): 3a34fab

Updated with Storage Monitor

Browse files
Files changed (1) hide show
  1. app.py +107 -59
app.py CHANGED
@@ -1,59 +1,107 @@
1
- import sys
2
- from pathlib import Path
3
- from fastapi import FastAPI
4
- from fastapi.middleware.cors import CORSMiddleware
5
- from fastapi.staticfiles import StaticFiles
6
- from fastapi.responses import FileResponse
7
-
8
- # add project root to Python path
9
- BASE_DIR = Path(__file__).resolve().parent
10
- sys.path.append(str(BASE_DIR))
11
-
12
- # import backend modules
13
- from backend.models import ChatRequest, ChatResponse
14
- from src.chatbot import RAGChatBot
15
-
16
- # Initialize chatbot
17
- bot = RAGChatBot()
18
-
19
- # Log FAISS index size safely
20
- try:
21
- size = getattr(bot, "index_size", None)
22
- if size is None and hasattr(bot, "index"):
23
- # if RAGChatBot has .index (FAISS object) instead
24
- size = len(bot.index) if hasattr(bot.index, "__len__") else "unknown"
25
- print(f"✅ FAISS index size: {size}")
26
- except Exception as e:
27
- print(f"⚠️ Could not determine FAISS index size: {e}")
28
-
29
- # Create FastAPI app
30
- app = FastAPI(title="Odisha Disaster Chatbot")
31
-
32
- # Enable CORS
33
- app.add_middleware(
34
- CORSMiddleware,
35
- allow_origins=["*"],
36
- allow_credentials=True,
37
- allow_methods=["*"],
38
- allow_headers=["*"],
39
- )
40
-
41
- # Serve frontend static files
42
- frontend_dir = BASE_DIR / "frontend"
43
- app.mount("/static", StaticFiles(directory=str(frontend_dir)), name="static")
44
-
45
- # Serve index.html at root
46
- @app.get("/")
47
- def read_index():
48
- return FileResponse(frontend_dir / "index.html")
49
-
50
- # Backend chat API
51
- @app.post("/api/chat", response_model=ChatResponse)
52
- def chat(request: ChatRequest):
53
- answer = bot.chat(request.query)
54
- return ChatResponse(answer=answer)
55
-
56
- # Optional health check
57
- @app.get("/health")
58
- def health():
59
- return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"}