Rammohan0504 commited on
Commit
27d8a02
·
verified ·
1 Parent(s): aadc32d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -37
app.py CHANGED
@@ -1,6 +1,10 @@
1
- from transformers import BlipProcessor, BlipForConditionalGeneration
 
 
 
 
2
  from PIL import Image
3
- import gradio as gr
4
  import torch
5
  from datetime import datetime
6
  from reportlab.lib.pagesizes import letter
@@ -8,11 +12,8 @@ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PD
8
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
9
  from reportlab.lib import colors
10
  from simple_salesforce import Salesforce
11
- 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
18
  load_dotenv()
@@ -30,23 +31,22 @@ except Exception as e:
30
  print(f"Failed to connect to Salesforce: {str(e)}")
31
 
32
  # Load BLIP model and processor
 
33
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
34
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
35
- model.eval()
36
- device = "cuda" if torch.cuda.is_available() else "cpu"
37
- model.to(device)
38
 
39
  # Inference function to generate captions dynamically based on image content
40
  def generate_captions_from_image(image):
41
  if image.mode != "RGB":
42
  image = image.convert("RGB")
43
-
44
- # Resize image for faster processing (use smaller resolution to speed up inference)
45
- image = image.resize((320, 320)) # Reduced size for faster processing
46
 
47
  # Preprocess the image and generate a caption
48
  inputs = processor(image, return_tensors="pt").to(device, torch.float16)
49
- output = model.generate(**inputs, max_new_tokens=50)
50
  caption = processor.decode(output[0], skip_special_tokens=True)
51
 
52
  return caption
@@ -139,9 +139,6 @@ def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
139
 
140
  # Generate a valid Salesforce URL for the ContentDocument
141
  content_document_url = f"https://{sf_connection.sf_instance}/sfc/servlet.shepherd/version/download/{content_version_id}"
142
-
143
-
144
- # Ensure the link is valid
145
  return content_document_id, content_document_url, f"File {filename} uploaded successfully"
146
  except Exception as e:
147
  return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
@@ -180,11 +177,6 @@ def generate_dpr(files):
180
  # Save DPR text to PDF
181
  pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
182
 
183
- salesforce_result = ""
184
- pdf_content_document_id = None
185
- pdf_url = None
186
- image_content_document_ids = []
187
-
188
  if sf and pdf_filepath:
189
  try:
190
  # Create Daily_Progress_Reports__c record
@@ -193,13 +185,11 @@ def generate_dpr(files):
193
  'Detected_Activities__c': report_description # Store in Detected_Activities__c field
194
  })
195
  dpr_record_id = dpr_record['id']
196
- salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
197
 
198
  # Upload PDF to Salesforce
199
  pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
200
  pdf_filepath, pdf_filename, sf, "pdf"
201
  )
202
- salesforce_result += pdf_upload_result + "\n"
203
 
204
  # Link PDF to DPR record
205
  if pdf_content_document_id:
@@ -214,7 +204,6 @@ def generate_dpr(files):
214
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
215
  'PDF_URL__c': pdf_url # Storing the PDF URL correctly
216
  })
217
- salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
218
 
219
  # Upload images to Salesforce and link them to DPR record
220
  for file in files:
@@ -235,31 +224,40 @@ def generate_dpr(files):
235
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
236
  'Site_Images__c': image_content_document_id # Storing the ContentDocumentId directly
237
  })
238
-
239
- salesforce_result += image_upload_result + "\n"
240
 
241
  except Exception as e:
242
- salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
243
- else:
244
- salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
245
 
246
- # Return DPR text, PDF file, and Salesforce upload status
247
- return (
248
- dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
249
- pdf_filepath
250
- )
 
 
 
 
 
 
 
 
 
 
 
 
251
  # Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
252
  iface = gr.Interface(
253
  fn=generate_dpr,
254
  inputs=gr.Files(type="filepath", label="Upload Site Photos"),
255
  outputs=[
256
  gr.Textbox(label="Daily Progress Report"),
257
- gr.File(label="Download PDF")
258
  ],
259
  title="Daily Progress Report Generator",
260
  description="Upload up to 10 site photos. The AI model will generate a text-based Daily Progress Report (DPR), save it as a PDF, and upload the PDF and images to Salesforce under Daily_Progress_Reports__c in the Files related list. Download the PDF locally if needed.",
261
- allow_flagging="never"
 
262
  )
263
 
264
  if __name__ == "__main__":
265
- iface.launch()
 
1
+ import os
2
+ import shutil
3
+ import base64
4
+ import time
5
+ import concurrent.futures
6
  from PIL import Image
7
+ from transformers import BlipProcessor, BlipForConditionalGeneration
8
  import torch
9
  from datetime import datetime
10
  from reportlab.lib.pagesizes import letter
 
12
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
13
  from reportlab.lib import colors
14
  from simple_salesforce import Salesforce
 
15
  from dotenv import load_dotenv
16
+ import gradio as gr
 
 
17
 
18
  # Load environment variables from .env file
19
  load_dotenv()
 
31
  print(f"Failed to connect to Salesforce: {str(e)}")
32
 
33
  # Load BLIP model and processor
34
+ device = "cuda" if torch.cuda.is_available() else "cpu"
35
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
36
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
37
+ model.eval().to(device)
 
 
38
 
39
  # Inference function to generate captions dynamically based on image content
40
  def generate_captions_from_image(image):
41
  if image.mode != "RGB":
42
  image = image.convert("RGB")
43
+
44
+ # Resize for faster processing
45
+ image = image.resize((224, 224)) # Adjust to smaller resolution for faster inference
46
 
47
  # Preprocess the image and generate a caption
48
  inputs = processor(image, return_tensors="pt").to(device, torch.float16)
49
+ output = model.generate(**inputs, max_length=50)
50
  caption = processor.decode(output[0], skip_special_tokens=True)
51
 
52
  return caption
 
139
 
140
  # Generate a valid Salesforce URL for the ContentDocument
141
  content_document_url = f"https://{sf_connection.sf_instance}/sfc/servlet.shepherd/version/download/{content_version_id}"
 
 
 
142
  return content_document_id, content_document_url, f"File {filename} uploaded successfully"
143
  except Exception as e:
144
  return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
 
177
  # Save DPR text to PDF
178
  pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
179
 
 
 
 
 
 
180
  if sf and pdf_filepath:
181
  try:
182
  # Create Daily_Progress_Reports__c record
 
185
  'Detected_Activities__c': report_description # Store in Detected_Activities__c field
186
  })
187
  dpr_record_id = dpr_record['id']
 
188
 
189
  # Upload PDF to Salesforce
190
  pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
191
  pdf_filepath, pdf_filename, sf, "pdf"
192
  )
 
193
 
194
  # Link PDF to DPR record
195
  if pdf_content_document_id:
 
204
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
205
  'PDF_URL__c': pdf_url # Storing the PDF URL correctly
206
  })
 
207
 
208
  # Upload images to Salesforce and link them to DPR record
209
  for file in files:
 
224
  sf.Daily_Progress_Reports__c.update(dpr_record_id, {
225
  'Site_Images__c': image_content_document_id # Storing the ContentDocumentId directly
226
  })
 
 
227
 
228
  except Exception as e:
229
+ pass # No output for Salesforce errors now
 
 
230
 
231
+ # Return the PDF file for Gradio download (using shutil to copy and return the file)
232
+ if pdf_filepath:
233
+ # Copy the PDF file to a temporary directory for Gradio to serve it
234
+ temp_pdf_path = "/tmp/" + os.path.basename(pdf_filepath)
235
+ shutil.copy(pdf_filepath, temp_pdf_path)
236
+
237
+ # Only return the DPR output and the PDF file path, excluding Salesforce upload details
238
+ return (
239
+ dpr_output + f"\n\n{pdf_result}", # Removed Salesforce upload status
240
+ temp_pdf_path # Returning the file path for download
241
+ )
242
+ else:
243
+ return (
244
+ dpr_output + f"\n\n{pdf_result}", # Removed Salesforce upload status
245
+ None
246
+ )
247
+
248
  # Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
249
  iface = gr.Interface(
250
  fn=generate_dpr,
251
  inputs=gr.Files(type="filepath", label="Upload Site Photos"),
252
  outputs=[
253
  gr.Textbox(label="Daily Progress Report"),
254
+ gr.File(label="Download PDF", interactive=False)
255
  ],
256
  title="Daily Progress Report Generator",
257
  description="Upload up to 10 site photos. The AI model will generate a text-based Daily Progress Report (DPR), save it as a PDF, and upload the PDF and images to Salesforce under Daily_Progress_Reports__c in the Files related list. Download the PDF locally if needed.",
258
+ allow_flagging="never",
259
+ css="#gradio-share-link-button-0 { display: none !important; }"
260
  )
261
 
262
  if __name__ == "__main__":
263
+ iface.launch()