dschandra commited on
Commit
3f8fd70
·
verified ·
1 Parent(s): 29da1ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -58
app.py CHANGED
@@ -13,7 +13,7 @@ from reportlab.lib.pagesizes import letter
13
  from reportlab.pdfgen import canvas
14
  from fastapi import FastAPI, Form, File, UploadFile
15
  from fastapi.middleware.cors import CORSMiddleware
16
- from fastapi.responses import JSONResponse, HTMLResponse
17
  from fastapi.staticfiles import StaticFiles
18
  from dotenv import load_dotenv
19
 
@@ -314,7 +314,7 @@ def gradio_interface(boq_file, weather, workforce, location, project_title):
314
  if not update_result:
315
  logger.warning("Failed to update record with PDF URL, but record was created")
316
 
317
- image_content_id = upload_file_to_salesforce(file_path, f"{project_title}_Gantt_Chart.png", record_id)
318
  image_url = None
319
  if image_content_id:
320
  sf = get_salesforce_connection()
@@ -332,6 +332,31 @@ def gradio_interface(boq_file, weather, workforce, location, project_title):
332
  shutil.rmtree(temp_dir)
333
  logger.debug(f"Cleaned up temporary directory: {temp_dir}")
334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  # Create a FastAPI app with CORS support
336
  app = FastAPI()
337
  app.add_middleware(
@@ -345,32 +370,6 @@ app.add_middleware(
345
  # Mount directory for temporary files (e.g., Gantt chart PNGs)
346
  app.mount("/static", StaticFiles(directory=tempfile.gettempdir()), name="static")
347
 
348
- # Fallback route for root path (in case Gradio is not used)
349
- @app.get("/", response_class=HTMLResponse)
350
- async def root():
351
- return """
352
- <html>
353
- <head>
354
- <title>AI Civil Work Planner API</title>
355
- </head>
356
- <body>
357
- <h1>AI Civil Work Planner API</h1>
358
- <p>The web interface is available via Gradio or use the API endpoint at <code>/api/gradio_interface</code>.</p>
359
- <p>Example using curl:</p>
360
- <pre>
361
- curl -X POST \
362
- -F "boq_file=@path/to/your/boq_data.csv" \
363
- -F "weather=sunny" \
364
- -F "workforce=10" \
365
- -F "location=New York" \
366
- -F "project_title=Test Project" \
367
- http://your-server:7860/api/gradio_interface
368
- </pre>
369
- <p>For more details, contact the administrator.</p>
370
- </body>
371
- </html>
372
- """
373
-
374
  # Health check endpoint to verify server status
375
  @app.get("/health")
376
  async def health_check():
@@ -476,34 +475,6 @@ async def api_gradio_interface(
476
  shutil.rmtree(temp_dir)
477
  logger.debug(f"Cleaned up temporary directory: {temp_dir}")
478
 
479
- # Create Gradio interface
480
- demo = gr.Blocks(theme="default")
481
- with demo:
482
- gr.Markdown("## AI Civil Work Planner")
483
- gr.Markdown("Generate a project timeline (Gantt chart) and risk tags based on BOQ data and site parameters.")
484
-
485
- with gr.Row():
486
- with gr.Column():
487
- boq_file = gr.File(label="Upload BOQ Data (CSV format)")
488
- weather = gr.Dropdown(label="Weather", choices=["sunny", "rainy", "cloudy"], value="sunny")
489
- workforce = gr.Number(label="Workforce Size", value=10, precision=0)
490
- location = gr.Textbox(label="Location", placeholder="Enter project location")
491
- project_title = gr.Textbox(label="Project Title", placeholder="Enter project title")
492
- submit_btn = gr.Button("Generate Timeline")
493
-
494
- with gr.Column():
495
- output_image = gr.Image(label="Gantt Chart")
496
- risk_tags = gr.Textbox(label="Risk Tags and Salesforce Status")
497
-
498
- submit_btn.click(
499
- fn=gradio_interface,
500
- inputs=[boq_file, weather, workforce, location, project_title],
501
- outputs=[output_image, risk_tags],
502
- )
503
-
504
- # Mount Gradio app on FastAPI
505
- app = gr.mount_gradio_app(app, demo, path="/gradio")
506
-
507
  if __name__ == "__main__":
508
- import uvicorn
509
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
13
  from reportlab.pdfgen import canvas
14
  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
19
 
 
314
  if not update_result:
315
  logger.warning("Failed to update record with PDF URL, but record was created")
316
 
317
+ image_content_id = upload_file_to_salesforce(fileLf_path, f"{project_title}_Gantt_Chart.png", record_id)
318
  image_url = None
319
  if image_content_id:
320
  sf = get_salesforce_connection()
 
332
  shutil.rmtree(temp_dir)
333
  logger.debug(f"Cleaned up temporary directory: {temp_dir}")
334
 
335
+ # Create Gradio interface
336
+ demo = gr.Blocks(theme="default")
337
+ with demo:
338
+ gr.Markdown("## AI Civil Work Planner")
339
+ gr.Markdown("Generate a project timeline (Gantt chart) and risk tags based on BOQ data and site parameters.")
340
+
341
+ with gr.Row():
342
+ with gr.Column():
343
+ boq_file = gr.File(label="Upload BOQ Data (CSV format)")
344
+ weather = gr.Dropdown(label="Weather", choices=["sunny", "rainy", "cloudy"], value="sunny")
345
+ workforce = gr.Number(label="Workforce Size", value=10, precision=0)
346
+ location = gr.Textbox(label="Location", placeholder="Enter project location")
347
+ project_title = gr.Textbox(label="Project Title", placeholder="Enter project title")
348
+ submit_btn = gr.Button("Generate Timeline")
349
+
350
+ with gr.Column():
351
+ output_image = gr.Image(label="Gantt Chart")
352
+ risk_tags = gr.Textbox(label="Risk Tags and Salesforce Status")
353
+
354
+ submit_btn.click(
355
+ fn=gradio_interface,
356
+ inputs=[boq_file, weather, workforce, location, project_title],
357
+ outputs=[output_image, risk_tags],
358
+ )
359
+
360
  # Create a FastAPI app with CORS support
361
  app = FastAPI()
362
  app.add_middleware(
 
370
  # Mount directory for temporary files (e.g., Gantt chart PNGs)
371
  app.mount("/static", StaticFiles(directory=tempfile.gettempdir()), name="static")
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  # Health check endpoint to verify server status
374
  @app.get("/health")
375
  async def health_check():
 
475
  shutil.rmtree(temp_dir)
476
  logger.debug(f"Cleaned up temporary directory: {temp_dir}")
477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  if __name__ == "__main__":
479
+ # Run Gradio UI
480
+ demo.launch(server_name="0.0.0.0", server_port=7860)