Spaces:
Runtime error
Runtime error
Iniital commit
Browse files- app.py +20 -18
- backend/__init__.py +4 -2
- backend/__main__.py +13 -1
- backend/main.py +10 -11
app.py
CHANGED
@@ -7,18 +7,19 @@ from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
from typing import Optional
|
9 |
|
10 |
-
#
|
11 |
project_root = os.path.abspath(os.path.dirname(__file__))
|
12 |
backend_dir = os.path.join(project_root, "backend")
|
13 |
|
14 |
-
# Add to Python path
|
15 |
for path in [project_root, backend_dir]:
|
16 |
if path not in sys.path:
|
17 |
sys.path.insert(0, path)
|
18 |
print(f"Added to path: {path}")
|
19 |
|
20 |
-
# Print the current Python path for debugging
|
21 |
print(f"Python path: {sys.path}")
|
|
|
|
|
22 |
|
23 |
app = FastAPI()
|
24 |
|
@@ -73,28 +74,29 @@ async def health_check():
|
|
73 |
# Try to import and mount the backend API routes
|
74 |
try:
|
75 |
print("Attempting to import backend routes...")
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Mount the backend API under /api
|
80 |
app.mount("/api", backend_app)
|
81 |
-
print("Backend routes mounted at /api")
|
82 |
-
|
83 |
-
# Also include backend routes directly in the root app
|
84 |
app.include_router(backend_app.router, prefix="/api")
|
|
|
85 |
|
86 |
except ImportError as e:
|
87 |
-
import
|
88 |
-
print(f"Error importing backend routes: {e}")
|
89 |
-
print("Traceback:")
|
90 |
-
traceback.print_exc()
|
91 |
print("Running in frontend-only mode")
|
92 |
-
print(f"Python path: {sys.path}")
|
93 |
-
print(f"Current working directory: {os.getcwd()}")
|
94 |
-
print(f"Directory contents: {os.listdir('.')}")
|
95 |
-
print(f"Backend directory exists: {os.path.exists('backend')}")
|
96 |
-
if os.path.exists('backend'):
|
97 |
-
print(f"Backend directory contents: {os.listdir('backend')}")
|
98 |
|
99 |
# Serve the frontend for all routes - this should be the last route
|
100 |
@app.get("/{full_path:path}", response_class=HTMLResponse)
|
|
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
from typing import Optional
|
9 |
|
10 |
+
# Set up paths
|
11 |
project_root = os.path.abspath(os.path.dirname(__file__))
|
12 |
backend_dir = os.path.join(project_root, "backend")
|
13 |
|
14 |
+
# Add necessary directories to Python path
|
15 |
for path in [project_root, backend_dir]:
|
16 |
if path not in sys.path:
|
17 |
sys.path.insert(0, path)
|
18 |
print(f"Added to path: {path}")
|
19 |
|
|
|
20 |
print(f"Python path: {sys.path}")
|
21 |
+
print(f"Current working directory: {os.getcwd()}")
|
22 |
+
print(f"Project root: {project_root}")
|
23 |
|
24 |
app = FastAPI()
|
25 |
|
|
|
74 |
# Try to import and mount the backend API routes
|
75 |
try:
|
76 |
print("Attempting to import backend routes...")
|
77 |
+
|
78 |
+
# First try absolute import
|
79 |
+
try:
|
80 |
+
from backend.main import app as backend_app
|
81 |
+
print("Imported backend using absolute import")
|
82 |
+
except ImportError as e:
|
83 |
+
# Fall back to relative import if absolute fails
|
84 |
+
try:
|
85 |
+
from .backend.main import app as backend_app
|
86 |
+
print("Imported backend using relative import")
|
87 |
+
except ImportError as e2:
|
88 |
+
print(f"Absolute import failed: {e}")
|
89 |
+
print(f"Relative import failed: {e2}")
|
90 |
+
raise ImportError("Could not import backend using either absolute or relative imports")
|
91 |
|
92 |
# Mount the backend API under /api
|
93 |
app.mount("/api", backend_app)
|
|
|
|
|
|
|
94 |
app.include_router(backend_app.router, prefix="/api")
|
95 |
+
print("Backend routes mounted at /api")
|
96 |
|
97 |
except ImportError as e:
|
98 |
+
print(f"Warning: Could not import backend routes: {e}")
|
|
|
|
|
|
|
99 |
print("Running in frontend-only mode")
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
# Serve the frontend for all routes - this should be the last route
|
102 |
@app.get("/{full_path:path}", response_class=HTMLResponse)
|
backend/__init__.py
CHANGED
@@ -3,5 +3,7 @@
|
|
3 |
This package contains the backend implementation for the PDF-based Q&A Chatbot.
|
4 |
"""
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
|
3 |
This package contains the backend implementation for the PDF-based Q&A Chatbot.
|
4 |
"""
|
5 |
|
6 |
+
# Make the app available at the package level
|
7 |
+
from .main import app
|
8 |
+
|
9 |
+
__all__ = ['app']
|
backend/__main__.py
CHANGED
@@ -3,8 +3,20 @@ Entry point for running the backend as a module.
|
|
3 |
Example: python -m backend
|
4 |
"""
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
if __name__ == "__main__":
|
9 |
import uvicorn
|
|
|
|
|
10 |
uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
3 |
Example: python -m backend
|
4 |
"""
|
5 |
|
6 |
+
import os
|
7 |
+
import sys
|
8 |
+
from pathlib import Path
|
9 |
+
|
10 |
+
# Add the backend directory to the Python path
|
11 |
+
backend_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
12 |
+
if backend_dir not in sys.path:
|
13 |
+
sys.path.insert(0, backend_dir)
|
14 |
+
|
15 |
+
# Import the app after setting up the path
|
16 |
+
from backend.main import app
|
17 |
|
18 |
if __name__ == "__main__":
|
19 |
import uvicorn
|
20 |
+
print("Starting backend server...")
|
21 |
+
print(f"Python path: {sys.path}")
|
22 |
uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True)
|
backend/main.py
CHANGED
@@ -1,22 +1,21 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
from fastapi.staticfiles import StaticFiles
|
4 |
-
import logging
|
5 |
import os
|
6 |
import sys
|
|
|
7 |
from pathlib import Path
|
|
|
|
|
8 |
|
9 |
-
# Add the backend directory to
|
10 |
backend_dir = os.path.abspath(os.path.dirname(__file__))
|
11 |
if backend_dir not in sys.path:
|
12 |
sys.path.insert(0, backend_dir)
|
13 |
|
14 |
-
# Use
|
15 |
-
from
|
16 |
-
from
|
17 |
-
from
|
18 |
-
from
|
19 |
-
from
|
20 |
import shutil
|
21 |
|
22 |
# Configure logging
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
+
import logging
|
4 |
from pathlib import Path
|
5 |
+
from fastapi import FastAPI
|
6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
7 |
|
8 |
+
# Add the backend directory to Python path
|
9 |
backend_dir = os.path.abspath(os.path.dirname(__file__))
|
10 |
if backend_dir not in sys.path:
|
11 |
sys.path.insert(0, backend_dir)
|
12 |
|
13 |
+
# Use absolute imports for the application code
|
14 |
+
from app.core.config import settings
|
15 |
+
from app.core.database import create_tables, SessionLocal
|
16 |
+
from app.models.document import Document, ChatMessage
|
17 |
+
from app.api.endpoints import documents, chat
|
18 |
+
from app.services.vector_store import VectorStore
|
19 |
import shutil
|
20 |
|
21 |
# Configure logging
|