ciyidogan commited on
Commit
78be6d7
Β·
verified Β·
1 Parent(s): 0da1272

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -71
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 work_mode"""
21
  cfg = ConfigProvider.get()
22
 
23
  log("=" * 60)
24
- log(f"πŸš€ Flare Starting in {cfg.global_config.work_mode.upper()} mode")
 
 
 
25
  log("=" * 60)
26
 
27
- if cfg.global_config.is_cloud_mode():
28
- log("☁️ Cloud Mode: Using HuggingFace Secrets")
29
- log("πŸ“Œ Required secrets: JWT_SECRET, FLARE_TOKEN_KEY, SPARK_TOKEN")
 
30
  else:
31
- log("🏒 On-Premise Mode: Using .env file")
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 (bootstrap)",
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
- static_path = Path("static")
71
- log(f"πŸ” Checking for static directory at: {static_path.absolute()}")
72
- log(f"πŸ” Static directory exists: {static_path.exists()}")
73
 
74
- if static_path.exists() and static_path.is_dir():
75
- # List files in static directory
76
- files = list(static_path.iterdir())
77
- log(f"πŸ“ Files in static directory: {[f.name for f in files]}")
78
 
79
- # Check for index.html
80
- index_path = static_path / "index.html"
81
- log(f"πŸ” index.html exists: {index_path.exists()}")
82
 
83
- # Mount entire static directory
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
- # Don't catch API routes
122
- if full_path.startswith("api/") or full_path.startswith("health"):
123
- from fastapi import HTTPException
124
- raise HTTPException(status_code=404, detail="Not found")
125
 
126
- # For Angular routes, always return index.html
127
- index_path = static_path / "index.html"
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("⚠️ Static directory not found")
137
- # No UI built, just health endpoint
138
  @app.get("/")
139
- def health_check():
140
- return {"status": "ok", "message": "UI not found. Build Angular app first."}
 
 
 
 
 
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)