Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,7 @@ local_timezone = pytz.timezone("Asia/Kolkata")
|
|
| 41 |
def process_image(images, project_name):
|
| 42 |
try:
|
| 43 |
if not images or len(images) == 0:
|
| 44 |
-
return "Error: Please upload at least one image to proceed.", "Pending", "", 0
|
| 45 |
|
| 46 |
# Process each image
|
| 47 |
milestones = []
|
|
@@ -49,9 +49,9 @@ def process_image(images, project_name):
|
|
| 49 |
img = Image.open(image)
|
| 50 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 51 |
if image_size_mb > 20:
|
| 52 |
-
return "Error: One or more images exceed 20MB.", "Failure", "", 0
|
| 53 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 54 |
-
return "Error: Only JPG/PNG images are supported.", "Failure", "", 0
|
| 55 |
|
| 56 |
# Save image to public folder temporarily before uploading to Salesforce
|
| 57 |
upload_dir = "public_uploads"
|
|
@@ -78,7 +78,7 @@ def process_image(images, project_name):
|
|
| 78 |
content_version_id = content_version_result['id']
|
| 79 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 80 |
except Exception as e:
|
| 81 |
-
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", 0
|
| 82 |
|
| 83 |
# Mock AI model to detect milestone based on image content
|
| 84 |
img_bytes = img.tobytes()
|
|
@@ -88,11 +88,14 @@ def process_image(images, project_name):
|
|
| 88 |
|
| 89 |
# Adjust milestone detection based on internal/external features
|
| 90 |
if milestone == "Walls Erected" and any("interior" in img_text.lower() for img_text in [os.path.basename(image)] if img_text):
|
| 91 |
-
# Check for visible internal works like electrical/plumbing
|
| 92 |
if any(keyword in str(os.path.basename(image)).lower() for keyword in ["electrical", "plumbing", "wiring", "pipes"]):
|
| 93 |
-
milestone = "Completed"
|
| 94 |
else:
|
| 95 |
-
milestone = "Walls Erected"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
milestones.append(milestone)
|
| 98 |
|
|
@@ -106,7 +109,6 @@ def process_image(images, project_name):
|
|
| 106 |
}
|
| 107 |
percent_complete = milestone_completion_map.get(final_milestone, 0)
|
| 108 |
|
| 109 |
-
# Detailed Completion Breakdown based on final milestone
|
| 110 |
completion_details = {
|
| 111 |
"Planning": {
|
| 112 |
"completed": [
|
|
@@ -166,7 +168,6 @@ def process_image(images, project_name):
|
|
| 166 |
completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
|
| 167 |
not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
|
| 168 |
|
| 169 |
-
# Create HTML for the output with collapsible sections and progress bar
|
| 170 |
result_html = f"""
|
| 171 |
<div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
|
| 172 |
<h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
|
|
@@ -206,11 +207,9 @@ def process_image(images, project_name):
|
|
| 206 |
</div>
|
| 207 |
"""
|
| 208 |
|
| 209 |
-
# Adjust the current time to local timezone with proper ISO 8601 format
|
| 210 |
now = datetime.now(local_timezone)
|
| 211 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
| 212 |
|
| 213 |
-
# Create the Salesforce record (removing confidence score)
|
| 214 |
record = {
|
| 215 |
"Name__c": project_name,
|
| 216 |
"Current_Milestone__c": final_milestone,
|
|
@@ -224,14 +223,14 @@ def process_image(images, project_name):
|
|
| 224 |
try:
|
| 225 |
sf.Construction__c.create(record)
|
| 226 |
except Exception as e:
|
| 227 |
-
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", 0
|
| 228 |
|
| 229 |
return result_html, "Success", final_milestone, f"{percent_complete}%"
|
| 230 |
|
| 231 |
except Exception as e:
|
| 232 |
-
return f"Error: {str(e)}", "Failure", "", "0%"
|
| 233 |
|
| 234 |
-
# Gradio UI
|
| 235 |
with gr.Blocks(css="""
|
| 236 |
.gradio-container {
|
| 237 |
background-color: #f0f4f8;
|
|
@@ -289,7 +288,7 @@ with gr.Blocks(css="""
|
|
| 289 |
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 290 |
|
| 291 |
submit_button = gr.Button("Process Image")
|
| 292 |
-
output_html = gr.HTML(label="Result")
|
| 293 |
upload_status = gr.Textbox(label="Upload Status")
|
| 294 |
milestone = gr.Textbox(label="Detected Milestone")
|
| 295 |
progress = gr.Textbox(label="Completion Percentage", interactive=False)
|
|
@@ -300,4 +299,4 @@ with gr.Blocks(css="""
|
|
| 300 |
outputs=[output_html, upload_status, milestone, progress]
|
| 301 |
)
|
| 302 |
|
| 303 |
-
demo.launch(share=True)
|
|
|
|
| 41 |
def process_image(images, project_name):
|
| 42 |
try:
|
| 43 |
if not images or len(images) == 0:
|
| 44 |
+
return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
|
| 45 |
|
| 46 |
# Process each image
|
| 47 |
milestones = []
|
|
|
|
| 49 |
img = Image.open(image)
|
| 50 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 51 |
if image_size_mb > 20:
|
| 52 |
+
return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
|
| 53 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 54 |
+
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 55 |
|
| 56 |
# Save image to public folder temporarily before uploading to Salesforce
|
| 57 |
upload_dir = "public_uploads"
|
|
|
|
| 78 |
content_version_id = content_version_result['id']
|
| 79 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 82 |
|
| 83 |
# Mock AI model to detect milestone based on image content
|
| 84 |
img_bytes = img.tobytes()
|
|
|
|
| 88 |
|
| 89 |
# Adjust milestone detection based on internal/external features
|
| 90 |
if milestone == "Walls Erected" and any("interior" in img_text.lower() for img_text in [os.path.basename(image)] if img_text):
|
|
|
|
| 91 |
if any(keyword in str(os.path.basename(image)).lower() for keyword in ["electrical", "plumbing", "wiring", "pipes"]):
|
| 92 |
+
milestone = "Completed"
|
| 93 |
else:
|
| 94 |
+
milestone = "Walls Erected"
|
| 95 |
+
|
| 96 |
+
# Specifically checking for external features indicating completion
|
| 97 |
+
if "completed" in os.path.basename(image).lower():
|
| 98 |
+
milestone = "Completed" # Detect "completed" word or finished facade
|
| 99 |
|
| 100 |
milestones.append(milestone)
|
| 101 |
|
|
|
|
| 109 |
}
|
| 110 |
percent_complete = milestone_completion_map.get(final_milestone, 0)
|
| 111 |
|
|
|
|
| 112 |
completion_details = {
|
| 113 |
"Planning": {
|
| 114 |
"completed": [
|
|
|
|
| 168 |
completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
|
| 169 |
not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
|
| 170 |
|
|
|
|
| 171 |
result_html = f"""
|
| 172 |
<div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
|
| 173 |
<h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
|
|
|
|
| 207 |
</div>
|
| 208 |
"""
|
| 209 |
|
|
|
|
| 210 |
now = datetime.now(local_timezone)
|
| 211 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
| 212 |
|
|
|
|
| 213 |
record = {
|
| 214 |
"Name__c": project_name,
|
| 215 |
"Current_Milestone__c": final_milestone,
|
|
|
|
| 223 |
try:
|
| 224 |
sf.Construction__c.create(record)
|
| 225 |
except Exception as e:
|
| 226 |
+
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
|
| 227 |
|
| 228 |
return result_html, "Success", final_milestone, f"{percent_complete}%"
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
+
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 232 |
|
| 233 |
+
# Gradio UI
|
| 234 |
with gr.Blocks(css="""
|
| 235 |
.gradio-container {
|
| 236 |
background-color: #f0f4f8;
|
|
|
|
| 288 |
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 289 |
|
| 290 |
submit_button = gr.Button("Process Image")
|
| 291 |
+
output_html = gr.HTML(label="Result")
|
| 292 |
upload_status = gr.Textbox(label="Upload Status")
|
| 293 |
milestone = gr.Textbox(label="Detected Milestone")
|
| 294 |
progress = gr.Textbox(label="Completion Percentage", interactive=False)
|
|
|
|
| 299 |
outputs=[output_html, upload_status, milestone, progress]
|
| 300 |
)
|
| 301 |
|
| 302 |
+
demo.launch(share=True)
|