dschandra commited on
Commit
5e0790c
·
verified ·
1 Parent(s): da293d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -15,7 +15,7 @@ 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
- from dotenv import load_dotenv # Added for environment variable loading
19
 
20
  # Load environment variables from .env file
21
  load_dotenv()
@@ -28,14 +28,14 @@ logger = logging.getLogger(__name__)
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
 
40
  # Function to authenticate with Salesforce
41
  def get_salesforce_connection():
@@ -318,7 +318,6 @@ def gradio_interface(boq_file, weather, workforce, location, project_title):
318
  if not update_result:
319
  logger.warning("Failed to update record with PDF URL, but record was created")
320
 
321
- # Upload Gantt chart image to Salesforce for a public URL
322
  image_content_id = upload_file_to_salesforce(file_path, f"{project_title}_Gantt_Chart.png", record_id)
323
  image_url = None
324
  if image_content_id:
@@ -351,13 +350,19 @@ app.add_middleware(
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")
@@ -379,7 +384,6 @@ async def api_gradio_interface(
379
  try:
380
  logger.info("Starting api_gradio_interface...")
381
 
382
- # Save uploaded file to temporary path
383
  temp_dir = tempfile.mkdtemp()
384
  boq_file_path = os.path.join(temp_dir, boq_file.filename)
385
  with open(boq_file_path, "wb") as f:
 
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
19
 
20
  # Load environment variables from .env file
21
  load_dotenv()
 
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")
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("Using Salesforce credentials - Username and Security Token loaded from environment variables.")
39
 
40
  # Function to authenticate with Salesforce
41
  def get_salesforce_connection():
 
318
  if not update_result:
319
  logger.warning("Failed to update record with PDF URL, but record was created")
320
 
 
321
  image_content_id = upload_file_to_salesforce(file_path, f"{project_title}_Gantt_Chart.png", record_id)
322
  image_url = None
323
  if image_content_id:
 
350
  app.mount("/static", StaticFiles(directory=tempfile.gettempdir()), name="static")
351
 
352
  # Mount Gradio's static assets to fix UI rendering issue
353
+ gradio_base_path = gr.__path__[0]
354
+ gradio_static_path = os.path.join(gradio_base_path, "templates", "frontend", "dist")
355
+
356
+ # Fallback to alternative path if dist is not found
357
+ if not os.path.exists(gradio_static_path):
358
+ gradio_static_path = os.path.join(gradio_base_path, "templates", "frontend", "build")
359
+ if not os.path.exists(gradio_static_path):
360
+ logger.error(f"Gradio static assets directory not found at {gradio_static_path}. Attempted paths: templates/frontend/dist, templates/frontend/build.")
361
+ logger.error("Please ensure Gradio's frontend assets are built by reinstalling Gradio or manually building the frontend with 'npm run build' in the Gradio frontend directory.")
362
+ raise FileNotFoundError(f"Gradio static assets not found. Please rebuild Gradio's frontend assets.")
363
+
364
+ app.mount("/_app", StaticFiles(directory=gradio_static_path), name="gradio_static")
365
+ logger.info(f"Mounted Gradio static assets at {gradio_static_path}")
366
 
367
  # Health check endpoint to verify static asset availability
368
  @app.get("/health")
 
384
  try:
385
  logger.info("Starting api_gradio_interface...")
386
 
 
387
  temp_dir = tempfile.mkdtemp()
388
  boq_file_path = os.path.join(temp_dir, boq_file.filename)
389
  with open(boq_file_path, "wb") as f: