chandra7799 commited on
Commit
6989ab0
·
verified ·
1 Parent(s): 1089dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -82
app.py CHANGED
@@ -45,31 +45,23 @@ def process_image(images, project_name):
45
 
46
  milestones = []
47
  for image in images:
48
- img = Image.open(image)
49
 
50
  # Check if the image exceeds the size limit (20MB)
51
- image_size_mb = os.path.getsize(image) / (1024 * 1024)
52
  if image_size_mb > 20:
53
  return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
54
- if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
55
  return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
56
 
57
- # Save image to public folder temporarily before uploading to Salesforce
58
- upload_dir = "public_uploads"
59
- os.makedirs(upload_dir, exist_ok=True)
60
- unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
61
- image_filename = f"{unique_id}_{os.path.basename(image)}"
62
- saved_image_path = os.path.join(upload_dir, image_filename)
63
- shutil.copy(image, saved_image_path)
64
-
65
  # Convert image to base64 before uploading to Salesforce
66
- with open(saved_image_path, 'rb') as image_file:
67
  image_data = base64.b64encode(image_file.read()).decode('utf-8')
68
 
69
  # Create the ContentVersion record in Salesforce
70
  content_version = {
71
- 'Title': image_filename,
72
- 'PathOnClient': saved_image_path,
73
  'VersionData': image_data
74
  }
75
 
@@ -82,7 +74,7 @@ def process_image(images, project_name):
82
  return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
83
 
84
  # Enhanced Milestone Detection
85
- milestone = detect_milestone(image)
86
 
87
  milestones.append(milestone)
88
 
@@ -97,71 +89,6 @@ def process_image(images, project_name):
97
  }
98
  percent_complete = milestone_completion_map.get(final_milestone, 0)
99
 
100
- completion_details = {
101
- "Planning": {
102
- "completed": [
103
- "Initial project outline and objectives have been established.",
104
- "Preliminary designs and architectural plans are drafted.",
105
- "Stakeholder meetings and initial approvals are completed."
106
- ],
107
- "not_completed": [
108
- "Detailed construction plans and blueprints are pending finalization.",
109
- "Permits and regulatory approvals are yet to be obtained.",
110
- "Contractor selection and procurement processes are not yet complete."
111
- ]
112
- },
113
- "Foundation": {
114
- "completed": [
115
- "Site preparation, including clearing and leveling, is finished.",
116
- "Excavation for the foundation has been completed.",
117
- "Concrete pouring for the foundation, including footings and slabs, is done.",
118
- "Initial structural inspections for the foundation have been passed."
119
- ],
120
- "not_completed": [
121
- "Plumbing and electrical groundwork installations are pending.",
122
- "Backfilling and site grading around the foundation are not yet done.",
123
- "Above-ground structural work, such as columns and walls, has not started."
124
- ]
125
- },
126
- "Walls Erected": {
127
- "completed": [
128
- "The concrete framework, including columns and beams, is in place.",
129
- "All structural walls have been erected and stabilized.",
130
- "Temporary scaffolding and safety measures are installed for ongoing work.",
131
- "Initial inspections for structural integrity have been completed."
132
- ],
133
- "not_completed": [
134
- "Roofing installation and weatherproofing are pending.",
135
- "Windows, doors, and exterior cladding are not yet installed.",
136
- "Interior walls, electrical, and plumbing systems are still to be implemented."
137
- ]
138
- },
139
- "Interior Furnishing": {
140
- "completed": [
141
- "Interior walls are fully constructed and painted.",
142
- "Windows, doors, and roofing are complete.",
143
- "HVAC, electrical, and plumbing are fully installed.",
144
- "Flooring, fixtures, and finishes are nearing completion."
145
- ],
146
- "not_completed": [
147
- "Furniture installation and final touches are pending.",
148
- "Final inspection and approval from authorities are yet to be completed."
149
- ]
150
- },
151
- "Completed": {
152
- "completed": [
153
- "The concrete framework, including columns, beams, and floor slabs, is fully constructed.",
154
- "Exterior walls, windows, and cladding are installed, completing the building's facade.",
155
- "Interior work, including electrical, plumbing, and HVAC systems, is fully implemented.",
156
- "Finishing touches, such as flooring, painting, and fixtures, are completed.",
157
- "All phases of the project are finished, including final inspections and approvals."
158
- ],
159
- "not_completed": [
160
- "There should be no more pending work as the project is fully completed."
161
- ]
162
- }
163
- }
164
-
165
  completed_tasks = completion_details[final_milestone]["completed"]
166
  not_completed_tasks = completion_details[final_milestone]["not_completed"]
167
 
@@ -303,10 +230,10 @@ with gr.Blocks(css="""
303
  """) as demo:
304
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
305
  with gr.Row():
306
- image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
307
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
308
 
309
- submit_button = gr.Button("Process Image")
310
  output_html = gr.HTML(label="Result")
311
  upload_status = gr.Textbox(label="Upload Status")
312
  milestone = gr.Textbox(label="Detected Milestone")
 
45
 
46
  milestones = []
47
  for image in images:
48
+ img = Image.open(image.name) # Access image through the name attribute
49
 
50
  # Check if the image exceeds the size limit (20MB)
51
+ image_size_mb = len(image.read()) / (1024 * 1024)
52
  if image_size_mb > 20:
53
  return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
54
+ if not str(image.name).lower().endswith(('.jpg', '.jpeg', '.png')):
55
  return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
56
 
 
 
 
 
 
 
 
 
57
  # Convert image to base64 before uploading to Salesforce
58
+ with open(image.name, 'rb') as image_file:
59
  image_data = base64.b64encode(image_file.read()).decode('utf-8')
60
 
61
  # Create the ContentVersion record in Salesforce
62
  content_version = {
63
+ 'Title': image.name,
64
+ 'PathOnClient': image.name,
65
  'VersionData': image_data
66
  }
67
 
 
74
  return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
75
 
76
  # Enhanced Milestone Detection
77
+ milestone = detect_milestone(image.name)
78
 
79
  milestones.append(milestone)
80
 
 
89
  }
90
  percent_complete = milestone_completion_map.get(final_milestone, 0)
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  completed_tasks = completion_details[final_milestone]["completed"]
93
  not_completed_tasks = completion_details[final_milestone]["not_completed"]
94
 
 
230
  """) as demo:
231
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
232
  with gr.Row():
233
+ image_input = gr.Files(type="file", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)", multi=True) # Allow multiple uploads
234
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
235
 
236
+ submit_button = gr.Button("Process Images")
237
  output_html = gr.HTML(label="Result")
238
  upload_status = gr.Textbox(label="Upload Status")
239
  milestone = gr.Textbox(label="Detected Milestone")