Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,73 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Image processing and Salesforce upload for multiple images
|
| 4 |
def process_image(images, project_name, project_type):
|
|
@@ -13,12 +82,11 @@ def process_image(images, project_name, project_type):
|
|
| 13 |
image_urls = []
|
| 14 |
milestone_confidences = []
|
| 15 |
all_percentages = []
|
| 16 |
-
all_milestones = set()
|
| 17 |
max_percent_complete = 0
|
| 18 |
dominant_milestone = None
|
| 19 |
dominant_image_url = None
|
| 20 |
|
| 21 |
-
# Process each image
|
| 22 |
for idx, image_path in enumerate(images):
|
| 23 |
try:
|
| 24 |
img = Image.open(image_path)
|
|
@@ -55,7 +123,7 @@ def process_image(images, project_name, project_type):
|
|
| 55 |
dominant_image_url = file_url
|
| 56 |
|
| 57 |
all_percentages.append(percent_complete)
|
| 58 |
-
all_milestones.add(milestone)
|
| 59 |
milestone_confidences.append((milestone, confidence_score))
|
| 60 |
results.append(f"Image {idx+1}: {milestone} - {percent_complete}% completion (Confidence: {confidence_score})")
|
| 61 |
|
|
@@ -67,7 +135,7 @@ def process_image(images, project_name, project_type):
|
|
| 67 |
return "<p style='color: red;'>Error: No images were successfully processed.</p>", "Failure", "", "", 0
|
| 68 |
|
| 69 |
average_percent_complete = round(sum(all_percentages) / len(all_percentages), 2) if all_percentages else 0
|
| 70 |
-
all_milestones_str = ", ".join(all_milestones)
|
| 71 |
|
| 72 |
now = datetime.now(local_timezone)
|
| 73 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
|
@@ -76,7 +144,7 @@ def process_image(images, project_name, project_type):
|
|
| 76 |
"Name__c": project_name,
|
| 77 |
"Project_Type__c": project_type,
|
| 78 |
"Completion_Percentage__c": max_percent_complete,
|
| 79 |
-
"Current_Milestone__c": all_milestones_str,
|
| 80 |
"Last_Updated_On__c": local_time,
|
| 81 |
"Upload_Status__c": "Success",
|
| 82 |
"Comments__c": f"Project {project_name} at {dominant_milestone or 'no milestone detected'} with {max_percent_complete}% completion",
|
|
@@ -104,4 +172,84 @@ def process_image(images, project_name, project_type):
|
|
| 104 |
except Exception as e:
|
| 105 |
return f"<p style='color: red;'>Error: {str(e)}</p>", "Failure", "", "", "0%"
|
| 106 |
|
| 107 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from simple_salesforce import Salesforce
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import hashlib
|
| 8 |
+
import shutil
|
| 9 |
+
import base64
|
| 10 |
+
import pytz
|
| 11 |
+
import time
|
| 12 |
+
|
| 13 |
+
# Load environment variables
|
| 14 |
+
print("Loading environment variables at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 15 |
+
load_dotenv()
|
| 16 |
+
SF_USERNAME = os.getenv("SF_USERNAME")
|
| 17 |
+
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
| 18 |
+
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 19 |
+
|
| 20 |
+
# Validate Salesforce credentials
|
| 21 |
+
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
|
| 22 |
+
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN in environment variables.")
|
| 23 |
+
print("Salesforce credentials validated successfully.")
|
| 24 |
+
|
| 25 |
+
# Initialize Salesforce connection
|
| 26 |
+
try:
|
| 27 |
+
print("Attempting Salesforce connection at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 28 |
+
sf = Salesforce(
|
| 29 |
+
username=SF_USERNAME,
|
| 30 |
+
password=SF_PASSWORD,
|
| 31 |
+
security_token=SF_SECURITY_TOKEN,
|
| 32 |
+
domain='login'
|
| 33 |
+
)
|
| 34 |
+
print("Salesforce connection established successfully.")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Salesforce connection failed: {str(e)}")
|
| 37 |
+
raise
|
| 38 |
+
|
| 39 |
+
print("Initializing application at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 40 |
+
|
| 41 |
+
# Updated Milestone Percentages
|
| 42 |
+
milestone_percentage_map = {
|
| 43 |
+
"Excavation and Foundation": 10,
|
| 44 |
+
"Structural Framework": 40,
|
| 45 |
+
"Roofing": 70,
|
| 46 |
+
"Exterior Work": 85,
|
| 47 |
+
"Interior Work": 95,
|
| 48 |
+
"Final Completion": 100
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# Adjust the timezone to your local timezone
|
| 52 |
+
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 53 |
+
|
| 54 |
+
# AI model prediction (for simulation)
|
| 55 |
+
def mock_ai_model(image):
|
| 56 |
+
img = image.convert("RGB")
|
| 57 |
+
max_size = 1024
|
| 58 |
+
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 59 |
+
|
| 60 |
+
img_bytes = img.tobytes()
|
| 61 |
+
img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
|
| 62 |
+
|
| 63 |
+
# Assign milestone based on hash (for simulation)
|
| 64 |
+
milestone_index = img_hash % len(milestone_percentage_map)
|
| 65 |
+
milestone = list(milestone_percentage_map.keys())[milestone_index]
|
| 66 |
+
|
| 67 |
+
confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
|
| 68 |
+
confidence_score = round(confidence_raw, 2)
|
| 69 |
+
|
| 70 |
+
return milestone, milestone_percentage_map[milestone], confidence_score
|
| 71 |
|
| 72 |
# Image processing and Salesforce upload for multiple images
|
| 73 |
def process_image(images, project_name, project_type):
|
|
|
|
| 82 |
image_urls = []
|
| 83 |
milestone_confidences = []
|
| 84 |
all_percentages = []
|
| 85 |
+
all_milestones = set()
|
| 86 |
max_percent_complete = 0
|
| 87 |
dominant_milestone = None
|
| 88 |
dominant_image_url = None
|
| 89 |
|
|
|
|
| 90 |
for idx, image_path in enumerate(images):
|
| 91 |
try:
|
| 92 |
img = Image.open(image_path)
|
|
|
|
| 123 |
dominant_image_url = file_url
|
| 124 |
|
| 125 |
all_percentages.append(percent_complete)
|
| 126 |
+
all_milestones.add(milestone)
|
| 127 |
milestone_confidences.append((milestone, confidence_score))
|
| 128 |
results.append(f"Image {idx+1}: {milestone} - {percent_complete}% completion (Confidence: {confidence_score})")
|
| 129 |
|
|
|
|
| 135 |
return "<p style='color: red;'>Error: No images were successfully processed.</p>", "Failure", "", "", 0
|
| 136 |
|
| 137 |
average_percent_complete = round(sum(all_percentages) / len(all_percentages), 2) if all_percentages else 0
|
| 138 |
+
all_milestones_str = ", ".join(all_milestones)
|
| 139 |
|
| 140 |
now = datetime.now(local_timezone)
|
| 141 |
local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
|
|
|
|
| 144 |
"Name__c": project_name,
|
| 145 |
"Project_Type__c": project_type,
|
| 146 |
"Completion_Percentage__c": max_percent_complete,
|
| 147 |
+
"Current_Milestone__c": all_milestones_str,
|
| 148 |
"Last_Updated_On__c": local_time,
|
| 149 |
"Upload_Status__c": "Success",
|
| 150 |
"Comments__c": f"Project {project_name} at {dominant_milestone or 'no milestone detected'} with {max_percent_complete}% completion",
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
return f"<p style='color: red;'>Error: {str(e)}</p>", "Failure", "", "", "0%"
|
| 174 |
|
| 175 |
+
# Gradio UI with enhanced styling
|
| 176 |
+
with gr.Blocks(css="""
|
| 177 |
+
.gradio-container {
|
| 178 |
+
background-color: #f0f4f8;
|
| 179 |
+
font-family: Arial, sans-serif;
|
| 180 |
+
}
|
| 181 |
+
.title {
|
| 182 |
+
color: #2c3e50;
|
| 183 |
+
font-size: 24px;
|
| 184 |
+
text-align: center;
|
| 185 |
+
font-weight: bold;
|
| 186 |
+
}
|
| 187 |
+
.gradio-row {
|
| 188 |
+
text-align: center;
|
| 189 |
+
}
|
| 190 |
+
.gradio-container .output {
|
| 191 |
+
text-align: center;
|
| 192 |
+
}
|
| 193 |
+
.gradio-container .input {
|
| 194 |
+
text-align: center;
|
| 195 |
+
}
|
| 196 |
+
.gradio-container .button {
|
| 197 |
+
display: block;
|
| 198 |
+
margin: 0 auto;
|
| 199 |
+
background-color: #3498db;
|
| 200 |
+
color: white;
|
| 201 |
+
border: none;
|
| 202 |
+
padding: 10px 20px;
|
| 203 |
+
border-radius: 5px;
|
| 204 |
+
cursor: pointer;
|
| 205 |
+
}
|
| 206 |
+
.gradio-container .button:hover {
|
| 207 |
+
background-color: #2980b9;
|
| 208 |
+
}
|
| 209 |
+
progress::-webkit-progress-value {
|
| 210 |
+
background-color: #2ecc71;
|
| 211 |
+
border-radius: 5px;
|
| 212 |
+
}
|
| 213 |
+
progress::-webkit-progress-bar {
|
| 214 |
+
background-color: #ecf0f1;
|
| 215 |
+
border-radius: 5px;
|
| 216 |
+
}
|
| 217 |
+
details summary {
|
| 218 |
+
cursor: pointer;
|
| 219 |
+
padding: 10px;
|
| 220 |
+
background-color: #ecf0f1;
|
| 221 |
+
border-radius: 5px;
|
| 222 |
+
}
|
| 223 |
+
details ul {
|
| 224 |
+
margin-top: 10px;
|
| 225 |
+
}
|
| 226 |
+
""") as demo:
|
| 227 |
+
gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
|
| 228 |
+
|
| 229 |
+
with gr.Row():
|
| 230 |
+
project_type_input = gr.Dropdown(label="Project Type", choices=["House", "Apartment"], value="House")
|
| 231 |
+
project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
|
| 232 |
+
|
| 233 |
+
image_input = gr.File(
|
| 234 |
+
file_count="multiple",
|
| 235 |
+
file_types=["image"],
|
| 236 |
+
label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB each)"
|
| 237 |
+
)
|
| 238 |
+
submit_button = gr.Button("Process Images")
|
| 239 |
+
output_html = gr.HTML(label="Result")
|
| 240 |
+
|
| 241 |
+
submit_button.click(
|
| 242 |
+
fn=process_image,
|
| 243 |
+
inputs=[image_input, project_name_input, project_type_input],
|
| 244 |
+
outputs=[output_html]
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
print("Launching Gradio interface at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 248 |
+
try:
|
| 249 |
+
demo.launch(share=True, debug=True) # Block and enable debug mode
|
| 250 |
+
print("Gradio interface launched successfully.")
|
| 251 |
+
while True: # Keep the script alive
|
| 252 |
+
time.sleep(10)
|
| 253 |
+
except Exception as e:
|
| 254 |
+
print(f"Failed to launch Gradio interface: {str(e)}")
|
| 255 |
+
raise
|