dschandra commited on
Commit
419d661
·
verified ·
1 Parent(s): 90a0a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -8
app.py CHANGED
@@ -15,16 +15,25 @@ from fastapi import FastAPI, Form, File, UploadFile
15
  from fastapi.middleware.cors import CORSMiddleware
16
  from fastapi.responses import JSONResponse
17
  from fastapi.staticfiles import StaticFiles
 
 
 
 
18
 
19
  # Configure logging to show detailed messages
20
  logging.basicConfig(level=logging.DEBUG)
21
  logger = logging.getLogger(__name__)
22
 
23
- # Salesforce credentials (use environment variables in production)
24
- SALESFORCE_USERNAME = os.getenv("SALESFORCE_USERNAME", "[email protected]")
25
- SALESFORCE_PASSWORD = os.getenv("SALESFORCE_PASSWORD", "#Prasad@1926")
26
- SALESFORCE_SECURITY_TOKEN = os.getenv("SALESFORCE_SECURITY_TOKEN", "4rmMYJ0augm8HKv9lpWXKYkD6")
27
- SALESFORCE_DOMAIN = os.getenv("SALESFORCE_DOMAIN", "login") # or "test" for sandbox
 
 
 
 
 
28
 
29
  logger.debug(f"Using Salesforce credentials - Username: {SALESFORCE_USERNAME}, Security Token: {SALESFORCE_SECURITY_TOKEN[:5]}...")
30
 
@@ -337,8 +346,26 @@ app.add_middleware(
337
  allow_methods=["*"],
338
  allow_headers=["*"],
339
  )
 
 
340
  app.mount("/static", StaticFiles(directory=tempfile.gettempdir()), name="static")
341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
  # FastAPI endpoint for Salesforce controller (defined before mounting Gradio)
343
  @app.post("/api/gradio_interface")
344
  async def api_gradio_interface(
@@ -440,8 +467,8 @@ async def api_gradio_interface(
440
  shutil.rmtree(temp_dir)
441
  logger.debug(f"Cleaned up temporary directory: {temp_dir}")
442
 
443
- # Create Gradio interface
444
- demo = gr.Blocks(theme="default")
445
  with demo:
446
  gr.Markdown("## AI Civil Work Planner")
447
  gr.Markdown("Generate a project timeline (Gantt chart) and risk tags based on BOQ data and site parameters.")
@@ -465,7 +492,7 @@ with demo:
465
  outputs=[output_image, risk_tags],
466
  )
467
 
468
- # Mount Gradio app (use a different variable to avoid overwriting 'app')
469
  gradio_app = gr.mount_gradio_app(app, demo, path="/")
470
 
471
  if __name__ == "__main__":
 
15
  from fastapi.middleware.cors import CORSMiddleware
16
  from fastapi.responses import JSONResponse
17
  from fastapi.staticfiles import StaticFiles
18
+ from dotenv import load_dotenv # Added for environment variable loading
19
+
20
+ # Load environment variables from .env file
21
+ load_dotenv()
22
 
23
  # Configure logging to show detailed messages
24
  logging.basicConfig(level=logging.DEBUG)
25
  logger = logging.getLogger(__name__)
26
 
27
+ # Salesforce credentials (loaded from environment variables)
28
+ SALESFORCE_USERNAME = os.getenv("SALESFORCE_USERNAME")
29
+ SALESFORCE_PASSWORD = os.getenv("SALESFORCE_PASSWORD")
30
+ SALESFORCE_SECURITY_TOKEN = os.getenv("SALESFORCE_SECURITY_TOKEN")
31
+ SALESFORCE_DOMAIN = os.getenv("SALESFORCE_DOMAIN", "login") # Default to 'login' if not set
32
+
33
+ # Validate that credentials are set
34
+ if not all([SALESFORCE_USERNAME, SALESFORCE_PASSWORD, SALESFORCE_SECURITY_TOKEN]):
35
+ logger.error("Salesforce credentials not set in environment variables. Please set SALESFORCE_USERNAME, SALESFORCE_PASSWORD, and SALESFORCE_SECURITY_TOKEN.")
36
+ raise ValueError("Missing Salesforce credentials in environment variables.")
37
 
38
  logger.debug(f"Using Salesforce credentials - Username: {SALESFORCE_USERNAME}, Security Token: {SALESFORCE_SECURITY_TOKEN[:5]}...")
39
 
 
346
  allow_methods=["*"],
347
  allow_headers=["*"],
348
  )
349
+
350
+ # Mount directory for temporary files (e.g., Gantt chart PNGs)
351
  app.mount("/static", StaticFiles(directory=tempfile.gettempdir()), name="static")
352
 
353
+ # Mount Gradio's static assets to fix UI rendering issue
354
+ gradio_static_path = os.path.join(gr.__path__[0], "templates", "frontend", "dist")
355
+ if os.path.exists(gradio_static_path):
356
+ app.mount("/_app", StaticFiles(directory=gradio_static_path), name="gradio_static")
357
+ logger.info(f"Mounted Gradio static assets at {gradio_static_path}")
358
+ else:
359
+ logger.error(f"Gradio static assets directory not found at {gradio_static_path}")
360
+ raise FileNotFoundError(f"Gradio static assets not found at {gradio_static_path}")
361
+
362
+ # Health check endpoint to verify static asset availability
363
+ @app.get("/health")
364
+ async def health_check():
365
+ if os.path.exists(gradio_static_path):
366
+ return {"status": "healthy", "gradio_static": "available"}
367
+ return {"status": "unhealthy", "gradio_static": "missing"}
368
+
369
  # FastAPI endpoint for Salesforce controller (defined before mounting Gradio)
370
  @app.post("/api/gradio_interface")
371
  async def api_gradio_interface(
 
467
  shutil.rmtree(temp_dir)
468
  logger.debug(f"Cleaned up temporary directory: {temp_dir}")
469
 
470
+ # Create Gradio interface with explicit base_path
471
+ demo = gr.Blocks(theme="default", base_path="/")
472
  with demo:
473
  gr.Markdown("## AI Civil Work Planner")
474
  gr.Markdown("Generate a project timeline (Gantt chart) and risk tags based on BOQ data and site parameters.")
 
492
  outputs=[output_image, risk_tags],
493
  )
494
 
495
+ # Mount Gradio app
496
  gradio_app = gr.mount_gradio_app(app, demo, path="/")
497
 
498
  if __name__ == "__main__":