Update app.py
Browse files
app.py
CHANGED
|
@@ -31,14 +31,14 @@ except Exception as e:
|
|
| 31 |
print(f"Salesforce connection failed: {str(e)}")
|
| 32 |
raise
|
| 33 |
|
| 34 |
-
# Updated Milestone Percentages
|
| 35 |
milestone_percentage_map = {
|
| 36 |
-
"Excavation and Foundation": 10,
|
| 37 |
-
"Structural Framework": 40,
|
| 38 |
-
"Roofing": 70,
|
| 39 |
-
"Exterior Work": 85,
|
| 40 |
-
"Interior Work": 95,
|
| 41 |
-
"Final Completion": 100
|
| 42 |
}
|
| 43 |
|
| 44 |
# Adjust the timezone to your local timezone
|
|
@@ -62,67 +62,105 @@ def mock_ai_model(image):
|
|
| 62 |
|
| 63 |
return milestone, milestone_percentage_map[milestone], confidence_score
|
| 64 |
|
| 65 |
-
# Image processing and Salesforce upload
|
| 66 |
-
def process_image(
|
| 67 |
try:
|
| 68 |
-
if not
|
| 69 |
-
return "Error: Please upload
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
# Create Salesforce record
|
| 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 |
-
# Create Salesforce record
|
| 106 |
record = {
|
| 107 |
"Name__c": project_name,
|
| 108 |
"Project_Type__c": project_type,
|
| 109 |
-
"Completion_Percentage__c":
|
| 110 |
-
"Current_Milestone__c":
|
| 111 |
"Last_Updated_On__c": local_time,
|
| 112 |
"Upload_Status__c": "Success",
|
| 113 |
-
"Comments__c": f"Project {project_name} at {
|
| 114 |
-
"Last_Updated_Image__c":
|
| 115 |
}
|
| 116 |
|
| 117 |
try:
|
| 118 |
-
sf.Construction__c.create(record)
|
| 119 |
except Exception as e:
|
| 120 |
-
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
return
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
-
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 126 |
|
| 127 |
# Gradio UI with enhanced styling
|
| 128 |
with gr.Blocks(css="""
|
|
@@ -182,9 +220,9 @@ with gr.Blocks(css="""
|
|
| 182 |
project_type_input = gr.Dropdown(label="Project Type", choices=["House", "Apartment"], value="House")
|
| 183 |
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 184 |
|
| 185 |
-
#
|
| 186 |
-
image_input = gr.Image(type="filepath", label="Upload Construction Site
|
| 187 |
-
submit_button = gr.Button("Process
|
| 188 |
output_html = gr.HTML(label="Result")
|
| 189 |
|
| 190 |
submit_button.click(
|
|
@@ -193,4 +231,4 @@ with gr.Blocks(css="""
|
|
| 193 |
outputs=[output_html]
|
| 194 |
)
|
| 195 |
|
| 196 |
-
demo.launch(share=True)
|
|
|
|
| 31 |
print(f"Salesforce connection failed: {str(e)}")
|
| 32 |
raise
|
| 33 |
|
| 34 |
+
# Updated Milestone Percentages
|
| 35 |
milestone_percentage_map = {
|
| 36 |
+
"Excavation and Foundation": 10,
|
| 37 |
+
"Structural Framework": 40,
|
| 38 |
+
"Roofing": 70,
|
| 39 |
+
"Exterior Work": 85,
|
| 40 |
+
"Interior Work": 95,
|
| 41 |
+
"Final Completion": 100
|
| 42 |
}
|
| 43 |
|
| 44 |
# Adjust the timezone to your local timezone
|
|
|
|
| 62 |
|
| 63 |
return milestone, milestone_percentage_map[milestone], confidence_score
|
| 64 |
|
| 65 |
+
# Image processing and Salesforce upload for multiple images
|
| 66 |
+
def process_image(images, project_name, project_type):
|
| 67 |
try:
|
| 68 |
+
if not images:
|
| 69 |
+
return "<p style='color: red;'>Error: Please upload at least one image to proceed.</p>", "Pending", "", "", 0
|
| 70 |
+
|
| 71 |
+
if not project_name:
|
| 72 |
+
return "<p style='color: red;'>Error: Project Name is required.</p>", "Pending", "", "", 0
|
| 73 |
+
|
| 74 |
+
results = []
|
| 75 |
+
image_urls = []
|
| 76 |
+
milestone_confidences = []
|
| 77 |
+
max_percent_complete = 0
|
| 78 |
+
dominant_milestone = None
|
| 79 |
+
|
| 80 |
+
# Process each image
|
| 81 |
+
for idx, image_path in enumerate(images):
|
| 82 |
+
try:
|
| 83 |
+
# Open the image
|
| 84 |
+
img = Image.open(image_path)
|
| 85 |
+
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 86 |
+
|
| 87 |
+
# Save image to public folder temporarily
|
| 88 |
+
upload_dir = "public_uploads"
|
| 89 |
+
os.makedirs(upload_dir, exist_ok=True)
|
| 90 |
+
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 91 |
+
image_filename = f"{unique_id}_{idx}_{os.path.basename(image_path)}"
|
| 92 |
+
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 93 |
+
shutil.copy(image_path, saved_image_path)
|
| 94 |
+
|
| 95 |
+
# Convert image to base64
|
| 96 |
+
with open(saved_image_path, 'rb') as image_file:
|
| 97 |
+
image_data = base64.b64encode(image_file.read()).decode('utf-8')
|
| 98 |
+
|
| 99 |
+
# Upload to Salesforce (ContentVersion)
|
| 100 |
+
content_version = {
|
| 101 |
+
'Title': image_filename,
|
| 102 |
+
'PathOnClient': saved_image_path,
|
| 103 |
+
'VersionData': image_data
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
content_version_result = sf.ContentVersion.create(content_version)
|
| 108 |
+
content_version_id = content_version_result['id']
|
| 109 |
+
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 110 |
+
image_urls.append(file_url)
|
| 111 |
+
except Exception as e:
|
| 112 |
+
results.append(f"Image {idx+1}: Failed to upload to Salesforce - {str(e)}")
|
| 113 |
+
continue
|
| 114 |
+
|
| 115 |
+
# Track the highest completion percentage and corresponding milestone
|
| 116 |
+
if percent_complete > max_percent_complete:
|
| 117 |
+
max_percent_complete = percent_complete
|
| 118 |
+
dominant_milestone = milestone
|
| 119 |
+
|
| 120 |
+
milestone_confidences.append((milestone, confidence_score))
|
| 121 |
+
results.append(f"Image {idx+1}: {milestone} - {percent_complete}% completion (Confidence: {confidence_score})")
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
results.append(f"Image {idx+1}: Error processing image - {str(e)}")
|
| 125 |
+
continue
|
| 126 |
+
|
| 127 |
+
# If no images were successfully processed
|
| 128 |
+
if not results:
|
| 129 |
+
return "<p style='color: red;'>Error: No images were successfully processed.</p>", "Failure", "", "", 0
|
| 130 |
|
| 131 |
+
# Create Salesforce record
|
| 132 |
now = datetime.now(local_timezone)
|
| 133 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
| 134 |
|
|
|
|
| 135 |
record = {
|
| 136 |
"Name__c": project_name,
|
| 137 |
"Project_Type__c": project_type,
|
| 138 |
+
"Completion_Percentage__c": max_percent_complete,
|
| 139 |
+
"Current_Milestone__c": dominant_milestone or "Unknown",
|
| 140 |
"Last_Updated_On__c": local_time,
|
| 141 |
"Upload_Status__c": "Success",
|
| 142 |
+
"Comments__c": f"Project {project_name} at {dominant_milestone or 'Unknown'} with {max_percent_complete}% completion",
|
| 143 |
+
"Last_Updated_Image__c": ";".join(image_urls) # Store all image URLs
|
| 144 |
}
|
| 145 |
|
| 146 |
try:
|
| 147 |
+
sf.Construction__c.create(record)
|
| 148 |
except Exception as e:
|
| 149 |
+
return f"<p style='color: red;'>Error: Failed to update Salesforce - {str(e)}</p>", "Failure", "", "", 0
|
| 150 |
+
|
| 151 |
+
# Format output
|
| 152 |
+
output_html = "<div style='text-align: left;'>"
|
| 153 |
+
output_html += "<h3>Processing Results:</h3><ul>"
|
| 154 |
+
for result in results:
|
| 155 |
+
output_html += f"<li>{result}</li>"
|
| 156 |
+
output_html += "</ul>"
|
| 157 |
+
output_html += f"<p><strong>Project Record Created:</strong> {project_name} - {dominant_milestone or 'Unknown'} ({max_percent_complete}%)</p>"
|
| 158 |
+
output_html += "</div>"
|
| 159 |
|
| 160 |
+
return output_html, "Success", dominant_milestone or "", f"Max Confidence: {max([conf for _, conf in milestone_confidences], default=0)}", f"{max_percent_complete}%"
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
+
return f"<p style='color: red;'>Error: {str(e)}</p>", "Failure", "", "", "0%"
|
| 164 |
|
| 165 |
# Gradio UI with enhanced styling
|
| 166 |
with gr.Blocks(css="""
|
|
|
|
| 220 |
project_type_input = gr.Dropdown(label="Project Type", choices=["House", "Apartment"], value="House")
|
| 221 |
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 222 |
|
| 223 |
+
# Updated to allow multiple image uploads
|
| 224 |
+
image_input = gr.Image(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB each)", allow_multiple=True)
|
| 225 |
+
submit_button = gr.Button("Process Images")
|
| 226 |
output_html = gr.HTML(label="Result")
|
| 227 |
|
| 228 |
submit_button.click(
|
|
|
|
| 231 |
outputs=[output_html]
|
| 232 |
)
|
| 233 |
|
| 234 |
+
demo.launch(share=True)
|