Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -17,18 +17,22 @@ from config_provider import ConfigProvider
|
|
17 |
|
18 |
# ===================== Environment Setup =====================
|
19 |
def setup_environment():
|
20 |
-
"""Setup environment based on
|
21 |
cfg = ConfigProvider.get()
|
22 |
|
23 |
log("=" * 60)
|
24 |
-
log(f"π Flare Starting
|
|
|
|
|
|
|
25 |
log("=" * 60)
|
26 |
|
27 |
-
if
|
28 |
-
|
29 |
-
log("
|
|
|
30 |
else:
|
31 |
-
log("π’ On-Premise
|
32 |
if not Path(".env").exists():
|
33 |
log("β οΈ WARNING: .env file not found!")
|
34 |
log("π Copy .env.example to .env and configure it")
|
@@ -43,7 +47,7 @@ mimetypes.add_type("text/css", ".css")
|
|
43 |
app = FastAPI(
|
44 |
title="Flare Orchestration Service",
|
45 |
version="0.1.0",
|
46 |
-
description="LLM-driven intent & API flow engine
|
47 |
)
|
48 |
|
49 |
# CORS for development
|
@@ -59,6 +63,7 @@ if os.getenv("ENVIRONMENT") == "development":
|
|
59 |
|
60 |
run_in_thread()
|
61 |
start_cleanup_task() # Activity log cleanup
|
|
|
62 |
|
63 |
# ---------------- Core chat/session routes --------------------------
|
64 |
app.include_router(chat_router, prefix="/api")
|
@@ -67,78 +72,36 @@ app.include_router(chat_router, prefix="/api")
|
|
67 |
app.include_router(admin_router)
|
68 |
|
69 |
# ---------------- Serve Angular UI if exists ------------------------
|
70 |
-
|
71 |
-
log(f"π Checking for static directory at: {static_path.absolute()}")
|
72 |
-
log(f"π Static directory exists: {static_path.exists()}")
|
73 |
|
74 |
-
if
|
75 |
-
|
76 |
-
files = list(static_path.iterdir())
|
77 |
-
log(f"π Files in static directory: {[f.name for f in files]}")
|
78 |
|
79 |
-
#
|
80 |
-
|
81 |
-
log(f"π index.html exists: {index_path.exists()}")
|
82 |
|
83 |
-
#
|
84 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
85 |
-
|
86 |
-
# Serve static files (Angular assets) - only if assets directory exists
|
87 |
-
assets_path = static_path / "assets"
|
88 |
-
if assets_path.exists() and assets_path.is_dir():
|
89 |
-
app.mount("/assets", StaticFiles(directory=str(assets_path)), name="assets")
|
90 |
-
|
91 |
-
# Root path - serve index.html
|
92 |
-
@app.get("/")
|
93 |
-
async def serve_root():
|
94 |
-
index_path = static_path / "index.html"
|
95 |
-
if index_path.exists():
|
96 |
-
log("π Serving index.html")
|
97 |
-
return FileResponse(str(index_path), media_type="text/html")
|
98 |
-
log("β οΈ index.html not found, returning health check")
|
99 |
-
return {"status": "ok", "sessions": len(session_store._sessions)} # Fallback to health check
|
100 |
-
|
101 |
-
# Serve JS files with correct MIME type
|
102 |
-
@app.get("/{filename:path}.js")
|
103 |
-
async def serve_js(filename: str):
|
104 |
-
js_path = static_path / f"{filename}.js"
|
105 |
-
if js_path.exists():
|
106 |
-
return FileResponse(str(js_path), media_type="application/javascript")
|
107 |
-
return {"error": "JS file not found"}, 404
|
108 |
-
|
109 |
-
# Serve CSS files with correct MIME type
|
110 |
-
@app.get("/{filename:path}.css")
|
111 |
-
async def serve_css(filename: str):
|
112 |
-
css_path = static_path / f"{filename}.css"
|
113 |
-
if css_path.exists():
|
114 |
-
return FileResponse(str(css_path), media_type="text/css")
|
115 |
-
return {"error": "CSS file not found"}, 404
|
116 |
-
|
117 |
-
|
118 |
-
# Catch-all route for Angular routing (must be last!)
|
119 |
@app.get("/{full_path:path}")
|
120 |
async def serve_angular(full_path: str):
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
126 |
-
#
|
127 |
-
|
128 |
-
if index_path.exists():
|
129 |
-
log(f"π Serving index.html for Angular route: /{full_path}")
|
130 |
-
return FileResponse(str(index_path), media_type="text/html")
|
131 |
-
|
132 |
-
# If no index.html, return 404
|
133 |
-
from fastapi import HTTPException
|
134 |
-
raise HTTPException(status_code=404, detail="UI not found")
|
135 |
else:
|
136 |
-
log("β οΈ
|
137 |
-
|
138 |
@app.get("/")
|
139 |
-
def
|
140 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
if __name__ == "__main__":
|
143 |
-
log("π Starting Flare backend
|
144 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
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"π€ LLM Provider: {cfg.global_config.llm_provider.name}")
|
26 |
+
log(f"π TTS Provider: {cfg.global_config.tts_provider.name}")
|
27 |
+
log(f"π€ STT Provider: {cfg.global_config.stt_provider.name}")
|
28 |
log("=" * 60)
|
29 |
|
30 |
+
# Check if running in HuggingFace Space
|
31 |
+
if os.environ.get("SPACE_ID"):
|
32 |
+
log("βοΈ Running in HuggingFace Space")
|
33 |
+
log("π Using environment secrets for API keys")
|
34 |
else:
|
35 |
+
log("π’ Local/On-Premise deployment")
|
36 |
if not Path(".env").exists():
|
37 |
log("β οΈ WARNING: .env file not found!")
|
38 |
log("π Copy .env.example to .env and configure it")
|
|
|
47 |
app = FastAPI(
|
48 |
title="Flare Orchestration Service",
|
49 |
version="0.1.0",
|
50 |
+
description="LLM-driven intent & API flow engine",
|
51 |
)
|
52 |
|
53 |
# CORS 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")
|
|
|
72 |
app.include_router(admin_router)
|
73 |
|
74 |
# ---------------- Serve Angular UI if exists ------------------------
|
75 |
+
ui_path = Path(__file__).parent / "ui" / "dist" / "flare-ui"
|
|
|
|
|
76 |
|
77 |
+
if ui_path.exists():
|
78 |
+
log(f"π Serving UI from: {ui_path}")
|
|
|
|
|
79 |
|
80 |
+
# Serve static files
|
81 |
+
app.mount("/assets", StaticFiles(directory=ui_path / "assets"), name="assets")
|
|
|
82 |
|
83 |
+
# Catch-all route for Angular routing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
@app.get("/{full_path:path}")
|
85 |
async def serve_angular(full_path: str):
|
86 |
+
"""Serve Angular app for all non-API routes"""
|
87 |
+
# Skip API routes
|
88 |
+
if full_path.startswith("api/"):
|
89 |
+
return {"error": "API endpoint not found"}
|
90 |
|
91 |
+
# Serve index.html for all routes (Angular will handle routing)
|
92 |
+
return FileResponse(ui_path / "index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
else:
|
94 |
+
log("β οΈ UI not found - only API endpoints available")
|
95 |
+
|
96 |
@app.get("/")
|
97 |
+
def root():
|
98 |
+
return {
|
99 |
+
"service": "Flare Orchestration Service",
|
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...")
|
107 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|