ciyidogan commited on
Commit
25f9893
Β·
verified Β·
1 Parent(s): 4966ebb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -39
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"πŸ€– 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")
@@ -46,8 +55,8 @@ mimetypes.add_type("text/css", ".css")
46
 
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
@@ -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
- 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...")
 
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...")