AldawsariNLP commited on
Commit
75d826f
·
1 Parent(s): 8360218

uploading pdfs...5

Browse files
Files changed (2) hide show
  1. app.py +55 -2
  2. backend/main.py +4 -3
app.py CHANGED
@@ -9,7 +9,8 @@ from fastapi import FastAPI, Request
9
  from fastapi.staticfiles import StaticFiles
10
  from fastapi.responses import FileResponse
11
  from fastapi.middleware.cors import CORSMiddleware
12
- from backend.main import app as backend_app
 
13
 
14
  @asynccontextmanager
15
  async def lifespan(app: FastAPI):
@@ -18,6 +19,58 @@ async def lifespan(app: FastAPI):
18
  print("[root_app] Startup event triggered")
19
  print("[root_app] Mounting backend application at /api")
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  yield
22
 
23
  # Shutdown (if needed in the future)
@@ -75,6 +128,6 @@ else:
75
 
76
  if __name__ == "__main__":
77
  import uvicorn
78
- port = int(os.getenv("PORT", 7860))
79
  uvicorn.run(app, host="0.0.0.0", port=port)
80
 
 
9
  from fastapi.staticfiles import StaticFiles
10
  from fastapi.responses import FileResponse
11
  from fastapi.middleware.cors import CORSMiddleware
12
+ from dotenv import load_dotenv
13
+ from backend.main import app as backend_app, initialize_rag_system
14
 
15
  @asynccontextmanager
16
  async def lifespan(app: FastAPI):
 
19
  print("[root_app] Startup event triggered")
20
  print("[root_app] Mounting backend application at /api")
21
 
22
+ # Initialize RAG system here to ensure it runs
23
+ # The backend's lifespan might not run when mounted, so initialize explicitly
24
+ print("[root_app] Initializing RAG system...")
25
+ try:
26
+ # Set up paths (same as backend/main.py)
27
+ # Calculate paths relative to app.py (project root)
28
+ project_root = Path(__file__).parent
29
+ documents_dir = project_root / "documents"
30
+ processed_json = project_root / "processed_documents.json"
31
+
32
+ # Load environment variables from .env file (if it exists)
33
+ env_path = project_root / ".env"
34
+ print(f"[root_app] .env file path: {env_path}")
35
+ print(f"[root_app] .env file exists? {env_path.exists()}")
36
+
37
+ if env_path.exists():
38
+ load_dotenv(env_path, override=True)
39
+ print("[root_app] Loaded .env file")
40
+ else:
41
+ # Try loading anyway in case it's in a different location
42
+ load_dotenv(env_path, override=True)
43
+ print("[root_app] .env file not found, using environment variables")
44
+
45
+ # Check for API keys (from .env or environment variables)
46
+ openai_key = os.getenv("OPENAI_API_KEY")
47
+ hf_token = os.getenv("HF_TOKEN")
48
+
49
+ if openai_key:
50
+ print(f"[root_app] OPENAI_API_KEY found (length: {len(openai_key)} characters)")
51
+ else:
52
+ print("[root_app] WARNING: OPENAI_API_KEY not found in environment")
53
+
54
+ if hf_token:
55
+ print(f"[root_app] HF_TOKEN found (length: {len(hf_token)} characters)")
56
+
57
+ # Set global variables in backend.main module so initialize_rag_system can use them
58
+ import backend.main as backend_main
59
+ backend_main.PROJECT_ROOT = project_root
60
+ backend_main.DOCUMENTS_DIR = documents_dir
61
+ backend_main.PROCESSED_JSON = processed_json
62
+
63
+ print(f"[root_app] PROJECT_ROOT: {project_root}")
64
+ print(f"[root_app] DOCUMENTS_DIR: {documents_dir} (exists: {documents_dir.exists()})")
65
+ print(f"[root_app] PROCESSED_JSON: {processed_json} (exists: {processed_json.exists()})")
66
+
67
+ initialize_rag_system()
68
+ print("[root_app] RAG system initialization completed")
69
+ except Exception as e:
70
+ print(f"[root_app] WARNING: RAG system initialization failed: {e}")
71
+ import traceback
72
+ traceback.print_exc()
73
+
74
  yield
75
 
76
  # Shutdown (if needed in the future)
 
128
 
129
  if __name__ == "__main__":
130
  import uvicorn
131
+ port = int(os.getenv("PORT", 7860))# 7860
132
  uvicorn.run(app, host="0.0.0.0", port=port)
133
 
backend/main.py CHANGED
@@ -15,9 +15,10 @@ except ModuleNotFoundError:
15
  from rag_system import RAGSystem
16
 
17
  # Load environment variables from .env file in project root
18
- # PROJECT_ROOT = Path(__file__).resolve().parents[1]
19
- # DOCUMENTS_DIR = PROJECT_ROOT / "documents"
20
- # PROCESSED_JSON = PROJECT_ROOT / "processed_documents.json"
 
21
 
22
  # env_path = PROJECT_ROOT / ".env"
23
  # print(f"[Module Init] .env file path: {env_path}")
 
15
  from rag_system import RAGSystem
16
 
17
  # Load environment variables from .env file in project root
18
+ # Initialize paths at module level (will be updated in lifespan if needed)
19
+ PROJECT_ROOT = Path(__file__).resolve().parents[1]
20
+ DOCUMENTS_DIR = PROJECT_ROOT / "documents"
21
+ PROCESSED_JSON = PROJECT_ROOT / "processed_documents.json"
22
 
23
  # env_path = PROJECT_ROOT / ".env"
24
  # print(f"[Module Init] .env file path: {env_path}")