Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
@@ -213,7 +214,11 @@ def generate_project_timeline(boq_file, weather, workforce, location, project_ti
|
|
213 |
output_path = os.path.join(temp_dir, output_filename)
|
214 |
logger.debug(f"Gantt chart will be saved to: {output_path}")
|
215 |
|
216 |
-
|
|
|
|
|
|
|
|
|
217 |
if "Task Name" not in df.columns or "Duration" not in df.columns:
|
218 |
raise ValueError("CSV must contain 'Task Name' and 'Duration' columns")
|
219 |
|
@@ -242,6 +247,116 @@ def generate_project_timeline(boq_file, weather, workforce, location, project_ti
|
|
242 |
shutil.rmtree(temp_dir)
|
243 |
return None, str(e), None
|
244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
# Create a FastAPI app with CORS support
|
246 |
app = FastAPI()
|
247 |
app.add_middleware(
|
@@ -361,5 +476,5 @@ async def api_gradio_interface(
|
|
361 |
logger.debug(f"Cleaned up temporary directory: {temp_dir}")
|
362 |
|
363 |
if __name__ == "__main__":
|
364 |
-
|
365 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
import matplotlib.pyplot as plt
|
|
|
214 |
output_path = os.path.join(temp_dir, output_filename)
|
215 |
logger.debug(f"Gantt chart will be saved to: {output_path}")
|
216 |
|
217 |
+
if isinstance(boq_file, str):
|
218 |
+
df = pd.read_csv(boq_file)
|
219 |
+
else:
|
220 |
+
df = pd.read_csv(boq_file.name)
|
221 |
+
|
222 |
if "Task Name" not in df.columns or "Duration" not in df.columns:
|
223 |
raise ValueError("CSV must contain 'Task Name' and 'Duration' columns")
|
224 |
|
|
|
247 |
shutil.rmtree(temp_dir)
|
248 |
return None, str(e), None
|
249 |
|
250 |
+
# Gradio interface function
|
251 |
+
def gradio_interface(boq_file, weather, workforce, location, project_title):
|
252 |
+
temp_dir = None
|
253 |
+
try:
|
254 |
+
logger.info("Starting gradio_interface...")
|
255 |
+
if not boq_file:
|
256 |
+
return None, "Error: No BOQ file uploaded"
|
257 |
+
|
258 |
+
boq_file_path = boq_file.name if hasattr(boq_file, 'name') else boq_file
|
259 |
+
file_path, risk_tags, temp_dir = generate_project_timeline(boq_file_path, weather, workforce, location, project_title)
|
260 |
+
if not file_path:
|
261 |
+
return None, f"Error: Failed to generate timeline: {risk_tags}"
|
262 |
+
|
263 |
+
df = pd.read_csv(boq_file_path)
|
264 |
+
estimated_duration = sum(df["Duration"])
|
265 |
+
ai_plan_score = min(100, max(0, 100 - (estimated_duration / 100)))
|
266 |
+
logger.debug(f"Estimated duration: {estimated_duration}, AI plan score: {ai_plan_score}")
|
267 |
+
|
268 |
+
record_id = send_to_salesforce(
|
269 |
+
project_title=project_title,
|
270 |
+
gantt_chart_url="",
|
271 |
+
ai_plan_score=ai_plan_score,
|
272 |
+
estimated_duration=estimated_duration,
|
273 |
+
status="Draft",
|
274 |
+
record_id=None,
|
275 |
+
location=location,
|
276 |
+
weather_type=weather
|
277 |
+
)
|
278 |
+
|
279 |
+
if not record_id:
|
280 |
+
return None, f"Error: Failed to create Salesforce record - check logs for details\n\nRisk Tags:\n{risk_tags}"
|
281 |
+
|
282 |
+
work_items_id = upload_file_to_salesforce(boq_file_path, "Boq_data.csv", record_id)
|
283 |
+
if not work_items_id:
|
284 |
+
logger.warning("Failed to upload BOQ file, but proceeding with record creation")
|
285 |
+
|
286 |
+
record_data = {
|
287 |
+
"project_title": project_title,
|
288 |
+
"estimated_duration": estimated_duration,
|
289 |
+
"ai_plan_score": ai_plan_score,
|
290 |
+
"status": "Draft",
|
291 |
+
"risk_tags": risk_tags,
|
292 |
+
}
|
293 |
+
pdf_file = generate_pdf(record_data)
|
294 |
+
if not pdf_file:
|
295 |
+
logger.warning("Failed to generate PDF, but proceeding with record creation")
|
296 |
+
|
297 |
+
pdf_content_id, pdf_url = None, None
|
298 |
+
if pdf_file:
|
299 |
+
pdf_content_id, pdf_url = upload_pdf_to_salesforce(pdf_file, project_title, record_id)
|
300 |
+
if not pdf_content_id:
|
301 |
+
logger.warning("Failed to upload PDF, but proceeding with record creation")
|
302 |
+
|
303 |
+
update_result = send_to_salesforce(
|
304 |
+
project_title=project_title,
|
305 |
+
gantt_chart_url=pdf_url if pdf_url else "",
|
306 |
+
ai_plan_score=ai_plan_score,
|
307 |
+
estimated_duration=estimated_duration,
|
308 |
+
status="Draft",
|
309 |
+
record_id=record_id,
|
310 |
+
location=location,
|
311 |
+
weather_type=weather,
|
312 |
+
work_items_id=work_items_id if work_items_id else ""
|
313 |
+
)
|
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()
|
321 |
+
if sf:
|
322 |
+
image_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{image_content_id}"
|
323 |
+
logger.debug(f"Generated image URL: {image_url}")
|
324 |
+
|
325 |
+
logger.info("Gradio interface completed successfully.")
|
326 |
+
return image_url if image_url else file_path, f"Successfully created Salesforce record ID: {record_id}\n\nRisk Tags:\n{risk_tags}"
|
327 |
+
except Exception as e:
|
328 |
+
logger.error(f"Error in Gradio interface: {str(e)}", exc_info=True)
|
329 |
+
return None, f"Error in Gradio interface: {str(e)}"
|
330 |
+
finally:
|
331 |
+
if temp_dir and os.path.exists(temp_dir):
|
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(
|
|
|
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)
|