Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from fastapi.responses import FileResponse
|
@@ -10,29 +15,33 @@ import mimetypes
|
|
10 |
from utils import log
|
11 |
from chat_handler import router as chat_router # β start_session & chat
|
12 |
from admin_routes import router as admin_router, start_cleanup_task
|
13 |
-
from llm_startup import run_in_thread
|
14 |
from session import session_store, start_session_cleanup
|
15 |
|
16 |
from config_provider import ConfigProvider
|
17 |
|
18 |
# ===================== Environment Setup =====================
|
19 |
def setup_environment():
|
20 |
-
"""Setup environment based on deployment"""
|
21 |
cfg = ConfigProvider.get()
|
22 |
|
23 |
log("=" * 60)
|
24 |
log(f"π Flare Starting")
|
25 |
-
log(f"
|
26 |
-
log(f"
|
27 |
-
log(f"
|
28 |
log("=" * 60)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
log("
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
-
log("π’
|
36 |
if not Path(".env").exists():
|
37 |
log("β οΈ WARNING: .env file not found!")
|
38 |
log("π Copy .env.example to .env and configure it")
|
@@ -46,8 +55,8 @@ mimetypes.add_type("text/css", ".css")
|
|
46 |
|
47 |
app = FastAPI(
|
48 |
title="Flare Orchestration Service",
|
49 |
-
version="0.
|
50 |
-
description="LLM-driven intent & API flow engine",
|
51 |
)
|
52 |
|
53 |
# CORS for development
|
@@ -61,10 +70,15 @@ if os.getenv("ENVIRONMENT") == "development":
|
|
61 |
)
|
62 |
log("π§ CORS enabled for development")
|
63 |
|
64 |
-
run_in_thread()
|
65 |
start_cleanup_task() # Activity log cleanup
|
66 |
start_session_cleanup() # Session cleanup
|
67 |
|
|
|
|
|
|
|
|
|
|
|
68 |
# ---------------- Core chat/session routes --------------------------
|
69 |
app.include_router(chat_router, prefix="/api")
|
70 |
|
@@ -72,35 +86,33 @@ app.include_router(chat_router, prefix="/api")
|
|
72 |
app.include_router(admin_router)
|
73 |
|
74 |
# ---------------- Serve Angular UI if exists ------------------------
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
log(f"π Serving UI from: {ui_path}")
|
79 |
|
80 |
-
#
|
81 |
-
app.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
#
|
88 |
-
|
89 |
-
|
|
|
90 |
|
91 |
-
#
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
"version": "0.1.0",
|
101 |
-
"status": "running",
|
102 |
-
"ui": "not available - build Angular UI first"
|
103 |
-
}
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
log("π Starting Flare backend on port 7860...")
|
|
|
1 |
+
"""
|
2 |
+
Flare β Main Application (Refactored)
|
3 |
+
=====================================
|
4 |
+
"""
|
5 |
+
|
6 |
from fastapi import FastAPI
|
7 |
from fastapi.staticfiles import StaticFiles
|
8 |
from fastapi.responses import FileResponse
|
|
|
15 |
from utils import log
|
16 |
from chat_handler import router as chat_router # β start_session & chat
|
17 |
from admin_routes import router as admin_router, start_cleanup_task
|
18 |
+
from llm_startup import run_in_thread # Changed from spark_startup
|
19 |
from session import session_store, start_session_cleanup
|
20 |
|
21 |
from config_provider import ConfigProvider
|
22 |
|
23 |
# ===================== Environment Setup =====================
|
24 |
def setup_environment():
|
25 |
+
"""Setup environment based on deployment mode"""
|
26 |
cfg = ConfigProvider.get()
|
27 |
|
28 |
log("=" * 60)
|
29 |
log(f"π Flare Starting")
|
30 |
+
log(f"π LLM Provider: {cfg.global_config.llm_provider.name}")
|
31 |
+
log(f"π€ TTS Provider: {cfg.global_config.tts_provider.name}")
|
32 |
+
log(f"π§ STT Provider: {cfg.global_config.stt_provider.name}")
|
33 |
log("=" * 60)
|
34 |
|
35 |
+
if cfg.global_config.is_cloud_mode():
|
36 |
+
log("βοΈ Cloud Mode: Using HuggingFace Secrets")
|
37 |
+
log("π Required secrets: JWT_SECRET, FLARE_TOKEN_KEY")
|
38 |
+
|
39 |
+
# Check for provider-specific tokens
|
40 |
+
llm_config = cfg.global_config.get_provider_config("llm", cfg.global_config.llm_provider.name)
|
41 |
+
if llm_config and llm_config.requires_repo_info:
|
42 |
+
log("π LLM requires SPARK_TOKEN for repository operations")
|
43 |
else:
|
44 |
+
log("π’ On-Premise Mode: Using .env file")
|
45 |
if not Path(".env").exists():
|
46 |
log("β οΈ WARNING: .env file not found!")
|
47 |
log("π Copy .env.example to .env and configure it")
|
|
|
55 |
|
56 |
app = FastAPI(
|
57 |
title="Flare Orchestration Service",
|
58 |
+
version="2.0.0",
|
59 |
+
description="LLM-driven intent & API flow engine with multi-provider support",
|
60 |
)
|
61 |
|
62 |
# CORS for development
|
|
|
70 |
)
|
71 |
log("π§ CORS enabled for development")
|
72 |
|
73 |
+
run_in_thread() # Start LLM startup notifier if needed
|
74 |
start_cleanup_task() # Activity log cleanup
|
75 |
start_session_cleanup() # Session cleanup
|
76 |
|
77 |
+
# ---------------- Health probe (HF Spaces watchdog) -----------------
|
78 |
+
@app.get("/")
|
79 |
+
def health_check():
|
80 |
+
return {"status": "ok", "version": "2.0.0"}
|
81 |
+
|
82 |
# ---------------- Core chat/session routes --------------------------
|
83 |
app.include_router(chat_router, prefix="/api")
|
84 |
|
|
|
86 |
app.include_router(admin_router)
|
87 |
|
88 |
# ---------------- Serve Angular UI if exists ------------------------
|
89 |
+
static_dir = Path(__file__).parent / "static"
|
90 |
+
if static_dir.exists():
|
91 |
+
log("π¨ Serving Angular UI from /static directory")
|
|
|
92 |
|
93 |
+
# Mount static files with custom handler for Angular routing
|
94 |
+
@app.get("/{path:path}")
|
95 |
+
async def serve_angular(path: str):
|
96 |
+
# API routes should not be handled here
|
97 |
+
if path.startswith("api/"):
|
98 |
+
return {"error": "Not found"}, 404
|
99 |
+
|
100 |
+
# Try to serve the exact file first
|
101 |
+
file_path = static_dir / path
|
102 |
+
if file_path.is_file():
|
103 |
+
return FileResponse(file_path)
|
104 |
|
105 |
+
# For Angular routes, always serve index.html
|
106 |
+
index_path = static_dir / "index.html"
|
107 |
+
if index_path.exists():
|
108 |
+
return FileResponse(index_path)
|
109 |
+
|
110 |
+
return {"error": "Not found"}, 404
|
111 |
|
112 |
+
# Mount static files for assets
|
113 |
+
app.mount("/", StaticFiles(directory=str(static_dir), html=True), name="static")
|
114 |
+
else:
|
115 |
+
log("β οΈ No UI found. Run 'cd flare-ui && npm run build' to build the UI.")
|
|
|
|
|
|
|
|
|
116 |
|
117 |
if __name__ == "__main__":
|
118 |
log("π Starting Flare backend on port 7860...")
|