Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,130 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
def evaluate_readiness(logs, qa_report, punch_list_text):
|
| 4 |
-
score =
|
| 5 |
missing_items = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Process Project Logs
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
else:
|
| 11 |
missing_items.append("Project Logs Incomplete")
|
|
|
|
| 12 |
|
| 13 |
# Process QA Report
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
else:
|
| 17 |
missing_items.append("QA Approval Missing")
|
|
|
|
| 18 |
|
| 19 |
-
# Process Punch List
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
missing_items.append("Open Punch Points Detected")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Build
|
| 26 |
-
checklist_summary = "
|
| 27 |
missing_summary = "None" if not missing_items else ", ".join(missing_items)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
from reportlab.lib.pagesizes import letter
|
| 4 |
+
from reportlab.pdfgen import canvas
|
| 5 |
+
import bleach
|
| 6 |
|
| 7 |
+
# Clean input to prevent injection attacks
|
| 8 |
+
def sanitize_input(text):
|
| 9 |
+
if not text or not isinstance(text, str):
|
| 10 |
+
return ""
|
| 11 |
+
return bleach.clean(text.strip())
|
| 12 |
+
|
| 13 |
+
# Evaluate project readiness with enhanced logic
|
| 14 |
def evaluate_readiness(logs, qa_report, punch_list_text):
|
| 15 |
+
score = 0 # Base score set to 0 for clearer range
|
| 16 |
missing_items = []
|
| 17 |
+
checklist_details = []
|
| 18 |
+
|
| 19 |
+
# Sanitize inputs
|
| 20 |
+
logs = sanitize_input(logs)
|
| 21 |
+
qa_report = sanitize_input(qa_report)
|
| 22 |
+
punch_list_text = sanitize_input(punch_list_text)
|
| 23 |
|
| 24 |
# Process Project Logs
|
| 25 |
+
log_keywords = r"complete|handover done|finished|closed|successful"
|
| 26 |
+
if logs and re.search(log_keywords, logs.lower()):
|
| 27 |
+
score += 30
|
| 28 |
+
checklist_details.append("Logs: Completed")
|
| 29 |
else:
|
| 30 |
missing_items.append("Project Logs Incomplete")
|
| 31 |
+
checklist_details.append("Logs: Pending")
|
| 32 |
|
| 33 |
# Process QA Report
|
| 34 |
+
qa_keywords = r"approved|passed|cleared"
|
| 35 |
+
if qa_report and re.search(qa_keywords, qa_report.lower()):
|
| 36 |
+
score += 40
|
| 37 |
+
checklist_details.append("QA Report: Approved")
|
| 38 |
else:
|
| 39 |
missing_items.append("QA Approval Missing")
|
| 40 |
+
checklist_details.append("QA Report: Pending")
|
| 41 |
|
| 42 |
+
# Process Punch List
|
| 43 |
+
punch_keywords = r"none|resolved|closed|no issues"
|
| 44 |
+
if punch_list_text and re.search(punch_keywords, punch_list_text.lower()):
|
| 45 |
+
score += 30
|
| 46 |
+
checklist_details.append("Punch List: Resolved")
|
| 47 |
else:
|
| 48 |
missing_items.append("Open Punch Points Detected")
|
| 49 |
+
checklist_details.append("Punch List: Pending")
|
| 50 |
+
|
| 51 |
+
# Handle critical issues (escalation)
|
| 52 |
+
escalated = any("incomplete" in item.lower() or "missing" in item.lower() for item in missing_items)
|
| 53 |
+
status = "Escalated" if escalated else ("Completed" if not missing_items else "Pending")
|
| 54 |
|
| 55 |
+
# Build summaries
|
| 56 |
+
checklist_summary = "\n".join(checklist_details)
|
| 57 |
missing_summary = "None" if not missing_items else ", ".join(missing_items)
|
| 58 |
|
| 59 |
+
# Generate progress bar HTML
|
| 60 |
+
color = "red" if score < 70 else "yellow" if score < 90 else "green"
|
| 61 |
+
progress_bar = f'<progress value="{score}" max="100" style="width:100%;height:20px;background-color:{color}">{score}%</progress>'
|
| 62 |
+
|
| 63 |
+
return score, checklist_summary, missing_summary, status, progress_bar
|
| 64 |
+
|
| 65 |
+
# Generate PDF report
|
| 66 |
+
def generate_pdf(score, checklist_summary, missing_summary, status):
|
| 67 |
+
pdf_file = "readiness_report.pdf"
|
| 68 |
+
c = canvas.Canvas(pdf_file, pagesize=letter)
|
| 69 |
+
c.setFont("Helvetica-Bold", 16)
|
| 70 |
+
c.drawString(50, 750, "Project Closure Readiness Report")
|
| 71 |
+
c.setFont("Helvetica", 12)
|
| 72 |
+
c.drawString(50, 720, f"Readiness Score: {score}%")
|
| 73 |
+
c.drawString(50, 700, f"Status: {status}")
|
| 74 |
+
c.drawString(50, 680, "Checklist Summary:")
|
| 75 |
+
y = 660
|
| 76 |
+
for line in checklist_summary.split("\n"):
|
| 77 |
+
c.drawString(50, y, line)
|
| 78 |
+
y -= 20
|
| 79 |
+
c.drawString(50, y-20, "Missing Items:")
|
| 80 |
+
c.drawString(50, y-40, missing_summary)
|
| 81 |
+
c.drawString(50, y-80, "Stakeholder Signature: ____________________")
|
| 82 |
+
c.drawString(50, y-100, "Date: ____________________")
|
| 83 |
+
c.save()
|
| 84 |
+
return pdf_file
|
| 85 |
+
|
| 86 |
+
# Gradio interface with enhanced UI
|
| 87 |
+
with gr.Blocks(css="""progress { background-color: #f0f0f0; } .red { background-color: red; }""") as demo:
|
| 88 |
+
gr.Markdown(
|
| 89 |
+
"""
|
| 90 |
+
# Project Closure Readiness Evaluator
|
| 91 |
+
Evaluate project readiness based on logs, QA report, and punch list status.
|
| 92 |
+
"""
|
| 93 |
+
)
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column(scale=2):
|
| 96 |
+
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project complete, handover done')")
|
| 97 |
+
qa_input = gr.Textbox(label="QA Report", placeholder="Enter QA status (e.g., 'Approved')")
|
| 98 |
+
punch_input = gr.Textbox(label="Punch List (Plain Text)", placeholder="Enter punch list status (e.g., 'None' or 'Resolved')")
|
| 99 |
+
submit_btn = gr.Button("Evaluate Readiness")
|
| 100 |
+
with gr.Column(scale=3):
|
| 101 |
+
score_output = gr.Number(label="Readiness Score (%)")
|
| 102 |
+
progress_output = gr.HTML(label="Progress")
|
| 103 |
+
status_output = gr.Textbox(label="Overall Status")
|
| 104 |
+
checklist_output = gr.Textbox(label="Checklist Summary")
|
| 105 |
+
missing_output = gr.Textbox(label="Missing Items")
|
| 106 |
+
with gr.Accordion("Details", open=False):
|
| 107 |
+
gr.Markdown("Detailed breakdown of checklist items and alerts.")
|
| 108 |
+
checklist_output_detailed = gr.Textbox(label="Checklist Details", interactive=False)
|
| 109 |
+
pdf_btn = gr.Button("Download PDF Report")
|
| 110 |
+
pdf_output = gr.File(label="PDF Report")
|
| 111 |
+
|
| 112 |
+
# Connect inputs to evaluate_readiness
|
| 113 |
+
submit_btn.click(
|
| 114 |
+
fn=evaluate_readiness,
|
| 115 |
+
inputs=[logs_input, qa_input, punch_input],
|
| 116 |
+
outputs=[score_output, checklist_output, missing_output, status_output, progress_output]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Connect PDF button to generate_pdf
|
| 120 |
+
pdf_btn.click(
|
| 121 |
+
fn=generate_pdf,
|
| 122 |
+
inputs=[score_output, checklist_output, missing_output, status_output],
|
| 123 |
+
outputs=pdf_output
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# Launch the app (commented out for Hugging Face deployment)
|
| 127 |
+
# demo.launch()
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|