Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 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
|
|
@@ -8,14 +9,14 @@ import shutil
|
|
| 8 |
import base64
|
| 9 |
import pytz
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
load_dotenv()
|
| 13 |
SF_USERNAME = os.getenv("SF_USERNAME")
|
| 14 |
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
| 15 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 16 |
|
| 17 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
|
| 18 |
-
raise ValueError("Missing Salesforce credentials.
|
| 19 |
|
| 20 |
try:
|
| 21 |
sf = Salesforce(
|
|
@@ -31,6 +32,22 @@ except Exception as e:
|
|
| 31 |
VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
|
| 32 |
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def process_image(images, project_name):
|
| 35 |
try:
|
| 36 |
if not images or len(images) == 0:
|
|
@@ -68,19 +85,8 @@ def process_image(images, project_name):
|
|
| 68 |
except Exception as e:
|
| 69 |
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
if "completed" in filename:
|
| 74 |
-
milestone = "Completed"
|
| 75 |
-
elif "walls" in filename or "erected" in filename:
|
| 76 |
-
milestone = "Walls Erected"
|
| 77 |
-
elif "foundation" in filename:
|
| 78 |
-
milestone = "Foundation"
|
| 79 |
-
elif "planning" in filename:
|
| 80 |
-
milestone = "Planning"
|
| 81 |
-
else:
|
| 82 |
-
milestone = "Planning" # default fallback
|
| 83 |
-
|
| 84 |
milestones.append(milestone)
|
| 85 |
|
| 86 |
final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
|
|
@@ -92,6 +98,7 @@ def process_image(images, project_name):
|
|
| 92 |
}
|
| 93 |
percent_complete = milestone_completion_map.get(final_milestone, 0)
|
| 94 |
|
|
|
|
| 95 |
completion_details = {
|
| 96 |
"Planning": {
|
| 97 |
"completed": [
|
|
@@ -145,11 +152,8 @@ def process_image(images, project_name):
|
|
| 145 |
}
|
| 146 |
}
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completed_tasks])
|
| 152 |
-
not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in not_completed_tasks])
|
| 153 |
|
| 154 |
result_html = f"""
|
| 155 |
<div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
|
|
@@ -213,58 +217,8 @@ def process_image(images, project_name):
|
|
| 213 |
except Exception as e:
|
| 214 |
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 215 |
|
| 216 |
-
# UI
|
| 217 |
-
with gr.Blocks(css=""
|
| 218 |
-
.gradio-container {
|
| 219 |
-
background-color: #f0f4f8;
|
| 220 |
-
font-family: Arial, sans-serif;
|
| 221 |
-
}
|
| 222 |
-
.title {
|
| 223 |
-
color: #2c3e50;
|
| 224 |
-
font-size: 24px;
|
| 225 |
-
text-align: center;
|
| 226 |
-
font-weight: bold;
|
| 227 |
-
}
|
| 228 |
-
.gradio-row {
|
| 229 |
-
text-align: center;
|
| 230 |
-
}
|
| 231 |
-
.gradio-container .output {
|
| 232 |
-
text-align: center;
|
| 233 |
-
}
|
| 234 |
-
.gradio-container .input {
|
| 235 |
-
text-align: center;
|
| 236 |
-
}
|
| 237 |
-
.gradio-container .button {
|
| 238 |
-
display: block;
|
| 239 |
-
margin: 0 auto;
|
| 240 |
-
background-color: #3498db;
|
| 241 |
-
color: white;
|
| 242 |
-
border: none;
|
| 243 |
-
padding: 10px 20px;
|
| 244 |
-
border-radius: 5px;
|
| 245 |
-
cursor: pointer;
|
| 246 |
-
}
|
| 247 |
-
.gradio-container .button:hover {
|
| 248 |
-
background-color: #2980b9;
|
| 249 |
-
}
|
| 250 |
-
progress::-webkit-progress-value {
|
| 251 |
-
background-color: #2ecc71;
|
| 252 |
-
border-radius: 5px;
|
| 253 |
-
}
|
| 254 |
-
progress::-webkit-progress-bar {
|
| 255 |
-
background-color: #ecf0f1;
|
| 256 |
-
border-radius: 5px;
|
| 257 |
-
}
|
| 258 |
-
details summary {
|
| 259 |
-
cursor: pointer;
|
| 260 |
-
padding: 10px;
|
| 261 |
-
background-color: #ecf0f1;
|
| 262 |
-
border-radius: 5px;
|
| 263 |
-
}
|
| 264 |
-
details ul {
|
| 265 |
-
margin-top: 10px;
|
| 266 |
-
}
|
| 267 |
-
""") as demo:
|
| 268 |
gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
|
| 269 |
with gr.Row():
|
| 270 |
image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from simple_salesforce import Salesforce
|
|
|
|
| 9 |
import base64
|
| 10 |
import pytz
|
| 11 |
|
| 12 |
+
# Load env
|
| 13 |
load_dotenv()
|
| 14 |
SF_USERNAME = os.getenv("SF_USERNAME")
|
| 15 |
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
| 16 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 17 |
|
| 18 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
|
| 19 |
+
raise ValueError("Missing Salesforce credentials.")
|
| 20 |
|
| 21 |
try:
|
| 22 |
sf = Salesforce(
|
|
|
|
| 32 |
VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
|
| 33 |
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 34 |
|
| 35 |
+
# ✅ Actual image analysis logic
|
| 36 |
+
def detect_milestone_from_image(image_path):
|
| 37 |
+
img = Image.open(image_path).convert("L")
|
| 38 |
+
img_array = np.array(img)
|
| 39 |
+
brightness = np.mean(img_array)
|
| 40 |
+
|
| 41 |
+
if brightness < 80:
|
| 42 |
+
return "Planning"
|
| 43 |
+
elif 80 <= brightness < 130:
|
| 44 |
+
return "Foundation"
|
| 45 |
+
elif 130 <= brightness < 180:
|
| 46 |
+
return "Walls Erected"
|
| 47 |
+
else:
|
| 48 |
+
return "Completed"
|
| 49 |
+
|
| 50 |
+
# Main logic
|
| 51 |
def process_image(images, project_name):
|
| 52 |
try:
|
| 53 |
if not images or len(images) == 0:
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 87 |
|
| 88 |
+
# 🧠 Milestone from actual image content
|
| 89 |
+
milestone = detect_milestone_from_image(saved_image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
milestones.append(milestone)
|
| 91 |
|
| 92 |
final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
|
|
|
|
| 98 |
}
|
| 99 |
percent_complete = milestone_completion_map.get(final_milestone, 0)
|
| 100 |
|
| 101 |
+
# Task mapping
|
| 102 |
completion_details = {
|
| 103 |
"Planning": {
|
| 104 |
"completed": [
|
|
|
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
+
completed_html = "".join([f'<li style="color: green;">✔ {task}</li>' for task in completion_details[final_milestone]["completed"]])
|
| 156 |
+
not_completed_html = "".join([f'<li style="color: red;">✘ {task}</li>' for task in completion_details[final_milestone]["not_completed"]])
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
result_html = f"""
|
| 159 |
<div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px;">
|
|
|
|
| 217 |
except Exception as e:
|
| 218 |
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 219 |
|
| 220 |
+
# UI
|
| 221 |
+
with gr.Blocks(css="") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
|
| 223 |
with gr.Row():
|
| 224 |
image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
|