Rammohan0504 commited on
Commit
4f0c2dd
·
verified ·
1 Parent(s): bb9e235

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -38
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import AutoProcessor, AutoModelForImageClassification
2
  from PIL import Image
3
  import gradio as gr
4
  import torch
@@ -12,6 +12,7 @@ import os
12
  from dotenv import load_dotenv
13
  import base64
14
  import io
 
15
  import concurrent.futures
16
 
17
  # Load environment variables from .env file
@@ -29,24 +30,27 @@ except Exception as e:
29
  sf = None
30
  print(f"Failed to connect to Salesforce: {str(e)}")
31
 
32
- # Load ViT model and processor (generic ImageNet pretrained)
33
- processor = AutoProcessor.from_pretrained("google/vit-base-patch16-224")
34
- model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")
35
  model.eval()
36
  device = "cuda" if torch.cuda.is_available() else "cpu"
37
  model.to(device)
38
 
39
- # Inference function to classify image and get predicted label
40
  def generate_captions_from_image(image):
41
  if image.mode != "RGB":
42
  image = image.convert("RGB")
43
- inputs = processor(images=image, return_tensors="pt").to(device)
44
- with torch.no_grad():
45
- outputs = model(**inputs)
46
- logits = outputs.logits
47
- predicted_class_idx = logits.argmax(-1).item()
48
- predicted_label = model.config.id2label[predicted_class_idx]
49
- return predicted_label
 
 
 
50
 
51
  # Function to save DPR text to a PDF file
52
  def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
@@ -89,14 +93,16 @@ def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
89
  else:
90
  flowables.append(Spacer(1, 12))
91
 
92
- # Add images and captions in the correct order
93
  for img_path, caption in zip(image_paths, captions):
94
  try:
 
95
  img = PDFImage(img_path, width=200, height=150) # Adjust image size if needed
96
  flowables.append(img)
 
97
  description = f"Description: {caption}"
98
  flowables.append(Paragraph(description, body_style))
99
- flowables.append(Spacer(1, 12))
100
  except Exception as e:
101
  flowables.append(Paragraph(f"Error loading image: {str(e)}", body_style))
102
 
@@ -106,15 +112,18 @@ def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
106
  except Exception as e:
107
  return f"Error saving PDF: {str(e)}", None
108
 
109
- # Function to upload file to Salesforce as ContentVersion
110
  def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
111
  try:
 
112
  with open(file_path, 'rb') as f:
113
  file_content = f.read()
114
  file_content_b64 = base64.b64encode(file_content).decode('utf-8')
115
 
 
116
  description = "Daily Progress Report PDF" if file_type == "pdf" else "Site Image"
117
 
 
118
  content_version = sf_connection.ContentVersion.create({
119
  'Title': filename,
120
  'PathOnClient': filename,
@@ -122,60 +131,68 @@ def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
122
  'Description': description
123
  })
124
 
 
125
  content_version_id = content_version['id']
126
  content_document = sf_connection.query(
127
  f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
128
  )
129
  content_document_id = content_document['records'][0]['ContentDocumentId']
130
 
 
131
  content_document_url = f"https://{sf_connection.sf_instance}/sfc/servlet.shepherd/version/download/{content_version_id}"
132
-
133
  return content_document_id, content_document_url, f"File {filename} uploaded successfully"
134
  except Exception as e:
135
  return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
136
 
137
- # Generate DPR, save PDF, upload to Salesforce
138
  def generate_dpr(files):
139
  dpr_text = []
140
  captions = []
141
  image_paths = []
142
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
143
 
 
144
  dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
145
 
 
146
  with concurrent.futures.ThreadPoolExecutor() as executor:
147
  results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
148
 
149
  for i, file in enumerate(files):
150
  caption = results[i]
151
  captions.append(caption)
 
 
152
  dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
153
  dpr_text.append(dpr_section)
 
 
154
  image_paths.append(file.name)
155
 
 
156
  dpr_output = "\n".join(dpr_text)
 
 
157
  pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
158
 
 
159
  pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
160
 
161
- salesforce_result = ""
162
- pdf_content_document_id = None
163
- pdf_url = None
164
-
165
  if sf and pdf_filepath:
166
  try:
167
- report_description = "; ".join(captions)[:255]
 
168
  dpr_record = sf.Daily_Progress_Reports__c.create({
169
- 'Detected_Activities__c': report_description
170
  })
171
  dpr_record_id = dpr_record['id']
172
- salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
173
 
 
174
  pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
175
  pdf_filepath, pdf_filename, sf, "pdf"
176
  )
177
- salesforce_result += pdf_upload_result + "\n"
178
 
 
179
  if pdf_content_document_id:
180
  sf.ContentDocumentLink.create({
181
  'ContentDocumentId': pdf_content_document_id,
@@ -183,38 +200,53 @@ def generate_dpr(files):
183
  'ShareType': 'V'
184
  })
185
 
 
186
  if pdf_url:
187
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
188
- 'PDF_URL__c': pdf_url
189
  })
190
- salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
191
 
 
192
  for file in files:
193
  image_filename = os.path.basename(file.name)
194
  image_content_document_id, image_url, image_upload_result = upload_file_to_salesforce(
195
  file.name, image_filename, sf, "image"
196
  )
 
197
  if image_content_document_id:
 
198
  sf.ContentDocumentLink.create({
199
  'ContentDocumentId': image_content_document_id,
200
- 'LinkedEntityId': dpr_record_id,
201
- 'ShareType': 'V'
202
  })
 
 
203
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
204
- 'Site_Images__c': image_content_document_id
205
  })
206
- salesforce_result += image_upload_result + "\n"
207
 
208
  except Exception as e:
209
- salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
210
- else:
211
- salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
212
 
213
- return (
214
- dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
215
- pdf_filepath
216
- )
 
 
 
 
 
 
 
 
 
 
 
 
217
 
 
218
  iface = gr.Interface(
219
  fn=generate_dpr,
220
  inputs=gr.Files(type="filepath", label="Upload Site Photos"),
 
1
+ from transformers import BlipProcessor, BlipForConditionalGeneration
2
  from PIL import Image
3
  import gradio as gr
4
  import torch
 
12
  from dotenv import load_dotenv
13
  import base64
14
  import io
15
+ import shutil
16
  import concurrent.futures
17
 
18
  # Load environment variables from .env file
 
30
  sf = None
31
  print(f"Failed to connect to Salesforce: {str(e)}")
32
 
33
+ # Load BLIP model and processor
34
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
35
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
36
  model.eval()
37
  device = "cuda" if torch.cuda.is_available() else "cpu"
38
  model.to(device)
39
 
40
+ # Inference function to generate captions dynamically based on image content
41
  def generate_captions_from_image(image):
42
  if image.mode != "RGB":
43
  image = image.convert("RGB")
44
+
45
+ # Resize image for faster processing (use smaller resolution to speed up inference)
46
+ image = image.resize((320, 320)) # Reduced size for faster processing
47
+
48
+ # Preprocess the image and generate a caption
49
+ inputs = processor(image, return_tensors="pt").to(device, torch.float16)
50
+ output = model.generate(**inputs, max_new_tokens=50)
51
+ caption = processor.decode(output[0], skip_special_tokens=True)
52
+
53
+ return caption
54
 
55
  # Function to save DPR text to a PDF file
56
  def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
 
93
  else:
94
  flowables.append(Spacer(1, 12))
95
 
96
+ # Add images and captions in the correct order (no need to add description to dpr_text again)
97
  for img_path, caption in zip(image_paths, captions):
98
  try:
99
+ # Add image first
100
  img = PDFImage(img_path, width=200, height=150) # Adjust image size if needed
101
  flowables.append(img)
102
+ # Add description below the image
103
  description = f"Description: {caption}"
104
  flowables.append(Paragraph(description, body_style))
105
+ flowables.append(Spacer(1, 12)) # Add some space between images
106
  except Exception as e:
107
  flowables.append(Paragraph(f"Error loading image: {str(e)}", body_style))
108
 
 
112
  except Exception as e:
113
  return f"Error saving PDF: {str(e)}", None
114
 
115
+ # Function to upload a file to Salesforce as ContentVersion
116
  def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
117
  try:
118
+ # Read file content and encode in base64
119
  with open(file_path, 'rb') as f:
120
  file_content = f.read()
121
  file_content_b64 = base64.b64encode(file_content).decode('utf-8')
122
 
123
+ # Set description based on file type
124
  description = "Daily Progress Report PDF" if file_type == "pdf" else "Site Image"
125
 
126
+ # Create ContentVersion
127
  content_version = sf_connection.ContentVersion.create({
128
  'Title': filename,
129
  'PathOnClient': filename,
 
131
  'Description': description
132
  })
133
 
134
+ # Get ContentDocumentId
135
  content_version_id = content_version['id']
136
  content_document = sf_connection.query(
137
  f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
138
  )
139
  content_document_id = content_document['records'][0]['ContentDocumentId']
140
 
141
+ # Generate a valid Salesforce URL for the ContentDocument
142
  content_document_url = f"https://{sf_connection.sf_instance}/sfc/servlet.shepherd/version/download/{content_version_id}"
 
143
  return content_document_id, content_document_url, f"File {filename} uploaded successfully"
144
  except Exception as e:
145
  return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
146
 
147
+ # Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
148
  def generate_dpr(files):
149
  dpr_text = []
150
  captions = []
151
  image_paths = []
152
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
153
 
154
+ # Add header to the DPR
155
  dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
156
 
157
+ # Process images in parallel for faster performance
158
  with concurrent.futures.ThreadPoolExecutor() as executor:
159
  results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
160
 
161
  for i, file in enumerate(files):
162
  caption = results[i]
163
  captions.append(caption)
164
+
165
+ # Generate DPR section for this image with dynamic caption
166
  dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
167
  dpr_text.append(dpr_section)
168
+
169
+ # Save image path for embedding in the report
170
  image_paths.append(file.name)
171
 
172
+ # Combine DPR text
173
  dpr_output = "\n".join(dpr_text)
174
+
175
+ # Generate PDF filename with timestamp
176
  pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
177
 
178
+ # Save DPR text to PDF
179
  pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
180
 
 
 
 
 
181
  if sf and pdf_filepath:
182
  try:
183
+ # Create Daily_Progress_Reports__c record
184
+ report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
185
  dpr_record = sf.Daily_Progress_Reports__c.create({
186
+ 'Detected_Activities__c': report_description # Store in Detected_Activities__c field
187
  })
188
  dpr_record_id = dpr_record['id']
 
189
 
190
+ # Upload PDF to Salesforce
191
  pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
192
  pdf_filepath, pdf_filename, sf, "pdf"
193
  )
 
194
 
195
+ # Link PDF to DPR record
196
  if pdf_content_document_id:
197
  sf.ContentDocumentLink.create({
198
  'ContentDocumentId': pdf_content_document_id,
 
200
  'ShareType': 'V'
201
  })
202
 
203
+ # Update the DPR record with the PDF URL
204
  if pdf_url:
205
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
206
+ 'PDF_URL__c': pdf_url # Storing the PDF URL correctly
207
  })
 
208
 
209
+ # Upload images to Salesforce and link them to DPR record
210
  for file in files:
211
  image_filename = os.path.basename(file.name)
212
  image_content_document_id, image_url, image_upload_result = upload_file_to_salesforce(
213
  file.name, image_filename, sf, "image"
214
  )
215
+
216
  if image_content_document_id:
217
+ # Link image to the Daily Progress Report record (DPR) using ContentDocumentLink
218
  sf.ContentDocumentLink.create({
219
  'ContentDocumentId': image_content_document_id,
220
+ 'LinkedEntityId': dpr_record_id, # Link image to DPR record
221
+ 'ShareType': 'V' # 'V' means Viewer access
222
  })
223
+
224
+ # Now, update the DPR record with the ContentDocumentId in the Site_Images field (if it's a text or URL field)
225
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
226
+ 'Site_Images__c': image_content_document_id # Storing the ContentDocumentId directly
227
  })
 
228
 
229
  except Exception as e:
230
+ pass # No output for Salesforce errors now
 
 
231
 
232
+ # Return the PDF file for Gradio download (using shutil to copy and return the file)
233
+ if pdf_filepath:
234
+ # Copy the PDF file to a temporary directory for Gradio to serve it
235
+ temp_pdf_path = "/tmp/" + os.path.basename(pdf_filepath)
236
+ shutil.copy(pdf_filepath, temp_pdf_path)
237
+
238
+ # Only return the DPR output and the PDF file path, excluding Salesforce upload details
239
+ return (
240
+ dpr_output + f"\n\n{pdf_result}", # Removed Salesforce upload status
241
+ temp_pdf_path # Returning the file path for download
242
+ )
243
+ else:
244
+ return (
245
+ dpr_output + f"\n\n{pdf_result}", # Removed Salesforce upload status
246
+ None
247
+ )
248
 
249
+ # Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
250
  iface = gr.Interface(
251
  fn=generate_dpr,
252
  inputs=gr.Files(type="filepath", label="Upload Site Photos"),