chandra7799 commited on
Commit
c003b06
·
verified ·
1 Parent(s): cacdbb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -51,18 +51,17 @@ def detect_milestone_from_image(image_path):
51
  def process_image(images, project_name):
52
  try:
53
  if not images or len(images) == 0:
54
- return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0, None # Added None for image output
55
 
56
  milestones = []
57
- last_image_path = None # Track the last saved image path
58
  for image in images:
59
  img = Image.open(image)
60
 
61
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
62
  if image_size_mb > 20:
63
- return "Error: One or more images exceed 20MB.", "Failure", "", "", 0, None
64
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
65
- return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0, None
66
 
67
  upload_dir = "public_uploads"
68
  os.makedirs(upload_dir, exist_ok=True)
@@ -70,7 +69,6 @@ def process_image(images, project_name):
70
  image_filename = f"{unique_id}_{os.path.basename(image)}"
71
  saved_image_path = os.path.join(upload_dir, image_filename)
72
  shutil.copy(image, saved_image_path)
73
- last_image_path = saved_image_path # Update last image path
74
 
75
  with open(saved_image_path, 'rb') as image_file:
76
  image_data = base64.b64encode(image_file.read()).decode('utf-8')
@@ -85,7 +83,7 @@ def process_image(images, project_name):
85
  content_version_id = content_version_result['id']
86
  file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
87
  except Exception as e:
88
- return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0, None
89
 
90
  milestone = detect_milestone_from_image(saved_image_path)
91
  milestones.append(milestone)
@@ -115,25 +113,25 @@ def process_image(images, project_name):
115
  try:
116
  sf.Construction__c.create(record)
117
  except Exception as e:
118
- return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0, None
119
-
120
- # Return last image path for display (filename not shown in UI)
121
- return (
122
- f"<h3 style='color:green;'>Milestone: {final_milestone}</h3>",
123
- "Success",
124
- final_milestone,
125
- f"{percent_complete}%",
126
- last_image_path # Added to return the last image for display
127
- )
128
 
129
  except Exception as e:
130
- return f"Error: {str(e)}", "Failure", "", "", "0%", None
131
 
132
- # Gradio UI with added image output
133
  with gr.Blocks(css="") as demo:
134
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
135
  with gr.Row():
136
- image_input = gr.Files(type="filepath", label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)")
 
 
 
 
 
 
 
137
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
138
 
139
  submit_button = gr.Button("Process Image")
@@ -141,12 +139,11 @@ with gr.Blocks(css="") as demo:
141
  upload_status = gr.Textbox(label="Upload Status")
142
  milestone = gr.Textbox(label="Detected Milestone")
143
  progress = gr.Textbox(label="Completion Percentage", interactive=False)
144
- image_output = gr.Image(label="Uploaded Image", interactive=False) # Added to display image
145
 
146
  submit_button.click(
147
  fn=process_image,
148
  inputs=[image_input, project_name_input],
149
- outputs=[output_html, upload_status, milestone, progress, image_output] # Added image_output
150
  )
151
 
152
  demo.launch(share=True)
 
51
  def process_image(images, project_name):
52
  try:
53
  if not images or len(images) == 0:
54
+ return "Error: Please upload at least one image to proceed.", "Pending", "", "", 0
55
 
56
  milestones = []
 
57
  for image in images:
58
  img = Image.open(image)
59
 
60
  image_size_mb = os.path.getsize(image) / (1024 * 1024)
61
  if image_size_mb > 20:
62
+ return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
63
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
64
+ return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
65
 
66
  upload_dir = "public_uploads"
67
  os.makedirs(upload_dir, exist_ok=True)
 
69
  image_filename = f"{unique_id}_{os.path.basename(image)}"
70
  saved_image_path = os.path.join(upload_dir, image_filename)
71
  shutil.copy(image, saved_image_path)
 
72
 
73
  with open(saved_image_path, 'rb') as image_file:
74
  image_data = base64.b64encode(image_file.read()).decode('utf-8')
 
83
  content_version_id = content_version_result['id']
84
  file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
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)
 
113
  try:
114
  sf.Construction__c.create(record)
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 with modified input to show image preview
124
  with gr.Blocks(css="") as demo:
125
  gr.Markdown("<h1 class='title'>Construction Progress Analyzer</h1>")
126
  with gr.Row():
127
+ # Changed from gr.Files to gr.Gallery for visual image preview
128
+ image_input = gr.Gallery(
129
+ label="Upload Construction Site Photos (JPG/PNG, ≤ 20MB)",
130
+ type="filepath",
131
+ interactive=True,
132
+ show_download_button=False, # Avoid showing filenames in UI
133
+ show_share_button=False
134
+ )
135
  project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
136
 
137
  submit_button = gr.Button("Process Image")
 
139
  upload_status = gr.Textbox(label="Upload Status")
140
  milestone = gr.Textbox(label="Detected Milestone")
141
  progress = gr.Textbox(label="Completion Percentage", interactive=False)
 
142
 
143
  submit_button.click(
144
  fn=process_image,
145
  inputs=[image_input, project_name_input],
146
+ outputs=[output_html, upload_status, milestone, progress]
147
  )
148
 
149
  demo.launch(share=True)