chandra7799 commited on
Commit
acdaa0b
·
verified ·
1 Parent(s): 32ef723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -102
app.py CHANGED
@@ -9,7 +9,7 @@ import shutil
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")
@@ -18,6 +18,7 @@ SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
18
  if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
19
  raise ValueError("Missing Salesforce credentials.")
20
 
 
21
  try:
22
  sf = Salesforce(
23
  username=SF_USERNAME,
@@ -32,7 +33,7 @@ except Exception as e:
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)
@@ -47,7 +48,6 @@ def detect_milestone_from_image(image_path):
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,7 +85,6 @@ def process_image(images, project_name):
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
 
@@ -98,102 +97,6 @@ def process_image(images, project_name):
98
  }
99
  percent_complete = milestone_completion_map.get(final_milestone, 0)
100
 
101
- # Task mapping
102
- completion_details = {
103
- "Planning": {
104
- "completed": [
105
- "Initial project outline and objectives have been established.",
106
- "Preliminary designs and architectural plans are drafted.",
107
- "Stakeholder meetings and initial approvals are completed."
108
- ],
109
- "not_completed": [
110
- "Detailed construction plans and blueprints are pending finalization.",
111
- "Permits and regulatory approvals are yet to be obtained.",
112
- "Contractor selection and procurement processes are not yet complete."
113
- ]
114
- },
115
- "Foundation": {
116
- "completed": [
117
- "Site preparation, including clearing and leveling, is finished.",
118
- "Excavation for the foundation has been completed.",
119
- "Concrete pouring for the foundation, including footings and slabs, is done.",
120
- "Initial structural inspections for the foundation have been passed."
121
- ],
122
- "not_completed": [
123
- "Plumbing and electrical groundwork installations are pending.",
124
- "Backfilling and site grading around the foundation are not yet done.",
125
- "Above-ground structural work, such as columns and walls, has not started."
126
- ]
127
- },
128
- "Walls Erected": {
129
- "completed": [
130
- "The concrete framework, including columns and beams, is in place.",
131
- "All structural walls have been erected and stabilized.",
132
- "Temporary scaffolding and safety measures are installed for ongoing work.",
133
- "Initial inspections for structural integrity have been completed."
134
- ],
135
- "not_completed": [
136
- "Roofing installation and weatherproofing are pending.",
137
- "Windows, doors, and exterior cladding are not yet installed.",
138
- "Interior walls, electrical, and plumbing systems are still to be implemented."
139
- ]
140
- },
141
- "Completed": {
142
- "completed": [
143
- "The concrete framework, including columns, beams, and floor slabs, is fully constructed.",
144
- "Exterior walls, windows, and cladding are installed, completing the building's facade.",
145
- "Interior work, including electrical, plumbing, and HVAC systems, is fully implemented.",
146
- "Finishing touches, such as flooring, painting, and fixtures, are completed.",
147
- "All phases of the project are finished, including final inspections and approvals."
148
- ],
149
- "not_completed": [
150
- "There should be no more pending work as the project is fully completed."
151
- ]
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;">
160
- <h2 style="color: #2c3e50; text-align: center;">Project Summary</h2>
161
- <div style="display: flex; justify-content: space-around; margin-bottom: 20px;">
162
- <div style="text-align: center;">
163
- <h3 style="color: #34495e;">Detected Milestone</h3>
164
- <p style="font-size: 18px; font-weight: bold;">{final_milestone}</p>
165
- </div>
166
- <div style="text-align: center;">
167
- <h3 style="color: #34495e;">Completion</h3>
168
- <progress value="{percent_complete}" max="100" style="width: 200px; height: 20px;"></progress>
169
- <p>{percent_complete}%</p>
170
- </div>
171
- </div>
172
-
173
- <h3 style="color: #2c3e50;">Milestone Timeline</h3>
174
- <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
175
- <span style="color: {'#2ecc71' if final_milestone == 'Planning' else '#bdc3c7'};">Planning</span>
176
- <span style="color: {'#2ecc71' if final_milestone == 'Foundation' else '#bdc3c7'};">Foundation</span>
177
- <span style="color: {'#2ecc71' if final_milestone == 'Walls Erected' else '#bdc3c7'};">Walls Erected</span>
178
- <span style="color: {'#2ecc71' if final_milestone == 'Completed' else '#bdc3c7'};">Completed</span>
179
- </div>
180
-
181
- <details style="margin-bottom: 20px;">
182
- <summary style="color: #2c3e50; font-weight: bold;">Completed Tasks</summary>
183
- <ul style="padding-left: 20px;">
184
- {completed_html}
185
- </ul>
186
- </details>
187
-
188
- <details style="margin-bottom: 20px;">
189
- <summary style="color: #2c3e50; font-weight: bold;">Not Completed Tasks</summary>
190
- <ul style="padding-left: 20px;">
191
- {not_completed_html}
192
- </ul>
193
- </details>
194
- </div>
195
- """
196
-
197
  now = datetime.now(local_timezone)
198
  local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
199
 
@@ -212,12 +115,12 @@ def process_image(images, project_name):
212
  except Exception as e:
213
  return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
214
 
215
- return result_html, "Success", final_milestone, f"{percent_complete}%"
216
 
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():
 
9
  import base64
10
  import pytz
11
 
12
+ # Load environment variables
13
  load_dotenv()
14
  SF_USERNAME = os.getenv("SF_USERNAME")
15
  SF_PASSWORD = os.getenv("SF_PASSWORD")
 
18
  if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
19
  raise ValueError("Missing Salesforce credentials.")
20
 
21
+ # Connect to Salesforce
22
  try:
23
  sf = Salesforce(
24
  username=SF_USERNAME,
 
33
  VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
34
  local_timezone = pytz.timezone("Asia/Kolkata")
35
 
36
+ # Milestone detection based on image brightness
37
  def detect_milestone_from_image(image_path):
38
  img = Image.open(image_path).convert("L")
39
  img_array = np.array(img)
 
48
  else:
49
  return "Completed"
50
 
 
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 = detect_milestone_from_image(saved_image_path)
89
  milestones.append(milestone)
90
 
 
97
  }
98
  percent_complete = milestone_completion_map.get(final_milestone, 0)
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  now = datetime.now(local_timezone)
101
  local_time = now.strftime("%Y-%m-%dT%H:%M:%S") + now.strftime("%z")[:-2] + ":" + now.strftime("%z")[-2:]
102
 
 
115
  except Exception as e:
116
  return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
117
 
118
+ return f"<h3 style='color:green;'>Milestone: {final_milestone}</h3>", "Success", final_milestone, f"{percent_complete}%"
119
 
120
  except Exception as e:
121
  return f"Error: {str(e)}", "Failure", "", "", "0%"
122
 
123
+ # Gradio UI
124
  with gr.Blocks(css="") as demo:
125
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
126
  with gr.Row():