Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,31 +50,28 @@ def detect_milestone_from_image(image_path):
|
|
| 50 |
|
| 51 |
def process_image(images, project_name):
|
| 52 |
try:
|
| 53 |
-
# Validates that at least one image is provided
|
| 54 |
if not images or len(images) == 0:
|
| 55 |
-
return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
|
| 56 |
|
| 57 |
-
# List to store milestones for all images
|
| 58 |
milestones = []
|
|
|
|
| 59 |
for image in images:
|
| 60 |
img = Image.open(image)
|
| 61 |
|
| 62 |
-
# Check file size and format
|
| 63 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 64 |
if image_size_mb > 20:
|
| 65 |
-
return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
|
| 66 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 67 |
-
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 68 |
|
| 69 |
-
|
| 70 |
-
upload_dir = "public_Uploads"
|
| 71 |
os.makedirs(upload_dir, exist_ok=True)
|
| 72 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 73 |
image_filename = f"{unique_id}_{os.path.basename(image)}"
|
| 74 |
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 75 |
shutil.copy(image, saved_image_path)
|
|
|
|
| 76 |
|
| 77 |
-
# Upload image to Salesforce
|
| 78 |
with open(saved_image_path, 'rb') as image_file:
|
| 79 |
image_data = base64.b64encode(image_file.read()).decode('utf-8')
|
| 80 |
|
|
@@ -88,13 +85,11 @@ def process_image(images, project_name):
|
|
| 88 |
content_version_id = content_version_result['id']
|
| 89 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 90 |
except Exception as e:
|
| 91 |
-
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 92 |
|
| 93 |
-
# Detect milestone for each image and store
|
| 94 |
milestone = detect_milestone_from_image(saved_image_path)
|
| 95 |
milestones.append(milestone)
|
| 96 |
|
| 97 |
-
# Aggregate milestones by selecting the most frequent one
|
| 98 |
final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
|
| 99 |
milestone_completion_map = {
|
| 100 |
"Planning": 10,
|
|
@@ -104,11 +99,9 @@ def process_image(images, project_name):
|
|
| 104 |
}
|
| 105 |
percent_complete = milestone_completion_map.get(final_milestone, 0)
|
| 106 |
|
| 107 |
-
# Get current time in Asia/Kolkata timezone
|
| 108 |
now = datetime.now(local_timezone)
|
| 109 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
| 110 |
|
| 111 |
-
# Create Salesforce record with aggregated milestone and last image URL
|
| 112 |
record = {
|
| 113 |
"Name__c": project_name,
|
| 114 |
"Current_Milestone__c": final_milestone,
|
|
@@ -116,26 +109,27 @@ def process_image(images, project_name):
|
|
| 116 |
"Last_Updated_On__c": local_time,
|
| 117 |
"Upload_Status__c": "Success",
|
| 118 |
"Comments__c": f"{final_milestone}",
|
| 119 |
-
"Last_Updated_Image__c": file_url
|
| 120 |
}
|
| 121 |
|
| 122 |
try:
|
| 123 |
sf.Construction__c.create(record)
|
| 124 |
except Exception as e:
|
| 125 |
-
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
|
| 126 |
|
| 127 |
-
# Return
|
| 128 |
return (
|
| 129 |
f"<h3 style='color:green;'>Milestone: {final_milestone}</h3>",
|
| 130 |
"Success",
|
| 131 |
final_milestone,
|
| 132 |
-
f"{percent_complete}%"
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
-
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 137 |
|
| 138 |
-
# Gradio UI
|
| 139 |
with gr.Blocks(css="") as demo:
|
| 140 |
gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
|
| 141 |
with gr.Row():
|
|
@@ -147,11 +141,12 @@ with gr.Blocks(css="") as demo:
|
|
| 147 |
upload_status = gr.Textbox(label="Upload Status")
|
| 148 |
milestone = gr.Textbox(label="Detected Milestone")
|
| 149 |
progress = gr.Textbox(label="Completion Percentage", interactive=False)
|
|
|
|
| 150 |
|
| 151 |
submit_button.click(
|
| 152 |
fn=process_image,
|
| 153 |
inputs=[image_input, project_name_input],
|
| 154 |
-
outputs=[output_html, upload_status, milestone, progress]
|
| 155 |
)
|
| 156 |
|
| 157 |
demo.launch(share=True)
|
|
|
|
| 50 |
|
| 51 |
def process_image(images, project_name):
|
| 52 |
try:
|
|
|
|
| 53 |
if not images or len(images) == 0:
|
| 54 |
+
return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0, None # Added None for image output
|
| 55 |
|
|
|
|
| 56 |
milestones = []
|
| 57 |
+
last_image_path = None # Track the last saved image path
|
| 58 |
for image in images:
|
| 59 |
img = Image.open(image)
|
| 60 |
|
|
|
|
| 61 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 62 |
if image_size_mb > 20:
|
| 63 |
+
return "Error: One or more images exceed 20MB.", "Failure", "", "", 0, None
|
| 64 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 65 |
+
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0, None
|
| 66 |
|
| 67 |
+
upload_dir = "public_uploads"
|
|
|
|
| 68 |
os.makedirs(upload_dir, exist_ok=True)
|
| 69 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 70 |
image_filename = f"{unique_id}_{os.path.basename(image)}"
|
| 71 |
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 72 |
shutil.copy(image, saved_image_path)
|
| 73 |
+
last_image_path = saved_image_path # Update last image path
|
| 74 |
|
|
|
|
| 75 |
with open(saved_image_path, 'rb') as image_file:
|
| 76 |
image_data = base64.b64encode(image_file.read()).decode('utf-8')
|
| 77 |
|
|
|
|
| 85 |
content_version_id = content_version_result['id']
|
| 86 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 87 |
except Exception as e:
|
| 88 |
+
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0, None
|
| 89 |
|
|
|
|
| 90 |
milestone = detect_milestone_from_image(saved_image_path)
|
| 91 |
milestones.append(milestone)
|
| 92 |
|
|
|
|
| 93 |
final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
|
| 94 |
milestone_completion_map = {
|
| 95 |
"Planning": 10,
|
|
|
|
| 99 |
}
|
| 100 |
percent_complete = milestone_completion_map.get(final_milestone, 0)
|
| 101 |
|
|
|
|
| 102 |
now = datetime.now(local_timezone)
|
| 103 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
| 104 |
|
|
|
|
| 105 |
record = {
|
| 106 |
"Name__c": project_name,
|
| 107 |
"Current_Milestone__c": final_milestone,
|
|
|
|
| 109 |
"Last_Updated_On__c": local_time,
|
| 110 |
"Upload_Status__c": "Success",
|
| 111 |
"Comments__c": f"{final_milestone}",
|
| 112 |
+
"Last_Updated_Image__c": file_url
|
| 113 |
}
|
| 114 |
|
| 115 |
try:
|
| 116 |
sf.Construction__c.create(record)
|
| 117 |
except Exception as e:
|
| 118 |
+
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0, None
|
| 119 |
|
| 120 |
+
# Return last image path for display (filename not shown in UI)
|
| 121 |
return (
|
| 122 |
f"<h3 style='color:green;'>Milestone: {final_milestone}</h3>",
|
| 123 |
"Success",
|
| 124 |
final_milestone,
|
| 125 |
+
f"{percent_complete}%",
|
| 126 |
+
last_image_path # Added to return the last image for display
|
| 127 |
)
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
+
return f"Error: {str(e)}", "Failure", "", "", "0%", None
|
| 131 |
|
| 132 |
+
# Gradio UI with added image output
|
| 133 |
with gr.Blocks(css="") as demo:
|
| 134 |
gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
|
| 135 |
with gr.Row():
|
|
|
|
| 141 |
upload_status = gr.Textbox(label="Upload Status")
|
| 142 |
milestone = gr.Textbox(label="Detected Milestone")
|
| 143 |
progress = gr.Textbox(label="Completion Percentage", interactive=False)
|
| 144 |
+
image_output = gr.Image(label="Uploaded Image", interactive=False) # Added to display image
|
| 145 |
|
| 146 |
submit_button.click(
|
| 147 |
fn=process_image,
|
| 148 |
inputs=[image_input, project_name_input],
|
| 149 |
+
outputs=[output_html, upload_status, milestone, progress, image_output] # Added image_output
|
| 150 |
)
|
| 151 |
|
| 152 |
demo.launch(share=True)
|