Rammohan0504 commited on
Commit
07b6838
·
verified ·
1 Parent(s): dee34df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -176
app.py CHANGED
@@ -1,18 +1,17 @@
 
 
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
7
  from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PDFImage
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()
@@ -29,21 +28,45 @@ except Exception as e:
29
  sf = None
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
45
- image = image.resize((640, 640))
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)
@@ -51,11 +74,14 @@ def generate_captions_from_image(image):
51
 
52
  return caption
53
 
54
- # Function to save DPR text to a PDF file
55
- def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
56
  try:
 
 
 
57
  # Create a PDF document
58
- doc = SimpleDocTemplate(filename, pagesize=letter)
59
  styles = getSampleStyleSheet()
60
 
61
  # Define custom styles
@@ -83,51 +109,33 @@ def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
83
  # Add title
84
  flowables.append(Paragraph("Daily Progress Report", title_style))
85
 
86
- # Split DPR text into lines and add as paragraphs (excluding descriptions for images)
87
- for line in dpr_text.split('\n'):
88
- # Replace problematic characters for PDF
89
- line = line.replace('\u2019', "'").replace('\u2018', "'")
90
- if line.strip():
91
- flowables.append(Paragraph(line, body_style))
92
- else:
93
- flowables.append(Spacer(1, 12))
94
-
95
- # Add images and captions in the correct order (no need to add description to dpr_text again)
96
- for img_path, caption in zip(image_paths, captions):
97
- try:
98
- # Add image first
99
- img = PDFImage(img_path, width=200, height=150) # Adjust image size if needed
100
- flowables.append(img)
101
- # Add description below the image
102
- description = f"Description: {caption}"
103
- flowables.append(Paragraph(description, body_style))
104
- flowables.append(Spacer(1, 12)) # Add some space between images
105
- except Exception as e:
106
- flowables.append(Paragraph(f"Error loading image: {str(e)}", body_style))
107
 
108
  # Build the PDF
109
  doc.build(flowables)
110
- return f"PDF saved successfully as {filename}", filename
 
111
  except Exception as e:
112
- return f"Error saving PDF: {str(e)}", None
 
113
 
114
- # Function to upload a file to Salesforce as ContentVersion
115
- def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
116
  try:
117
  # Read file content and encode in base64
118
- with open(file_path, 'rb') as f:
119
  file_content = f.read()
120
  file_content_b64 = base64.b64encode(file_content).decode('utf-8')
121
 
122
- # Set description based on file type
123
- description = "Daily Progress Report PDF" if file_type == "pdf" else "Site Image"
124
-
125
  # Create ContentVersion
126
  content_version = sf_connection.ContentVersion.create({
127
- 'Title': filename,
128
- 'PathOnClient': filename,
129
  'VersionData': file_content_b64,
130
- 'Description': description
131
  })
132
 
133
  # Get ContentDocumentId
@@ -137,134 +145,14 @@ def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
137
  )
138
  content_document_id = content_document['records'][0]['ContentDocumentId']
139
 
140
- # Generate a valid Salesforce URL for the ContentDocument
141
- content_document_url = f"https://{sf_connection.sf_instance}.salesforce.com/{content_document_id}"
142
-
143
- # Ensure the link is valid
144
- return content_document_id, content_document_url, f"File {filename} uploaded successfully"
145
  except Exception as e:
146
- return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
147
-
148
- # Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
149
- def generate_dpr(files):
150
- dpr_text = []
151
- captions = []
152
- image_paths = []
153
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
154
-
155
- # Add header to the DPR
156
- dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
157
-
158
- # Process images in parallel for faster performance
159
- with concurrent.futures.ThreadPoolExecutor() as executor:
160
- results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
161
-
162
- for i, file in enumerate(files):
163
- caption = results[i]
164
- captions.append(caption)
165
-
166
- # Generate DPR section for this image with dynamic caption
167
- dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
168
- # Remove the description from the dpr_text section
169
- # No need to add it again as the image and caption will be inserted in the PDF
170
- dpr_text.append(dpr_section)
171
-
172
- # Save image path for embedding in the report
173
- image_paths.append(file.name)
174
-
175
- # Combine DPR text (no redundant description here)
176
- dpr_output = "\n".join(dpr_text)
177
-
178
- # Generate PDF filename with timestamp
179
- pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
180
-
181
- # Save DPR text to PDF
182
- pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
183
-
184
- # Salesforce upload
185
- salesforce_result = ""
186
- pdf_content_document_id = None
187
- pdf_url = None
188
- image_content_document_ids = []
189
-
190
- if sf and pdf_filepath:
191
- try:
192
- # Create Daily_Progress_Reports__c record
193
- report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
194
- dpr_record = sf.Daily_Progress_Reports__c.create({
195
- 'Detected_Activities__c': report_description # Store in Detected_Activities__c field
196
- })
197
- dpr_record_id = dpr_record['id']
198
- salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
199
-
200
- # Upload PDF to Salesforce
201
- pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
202
- pdf_filepath, pdf_filename, sf, "pdf"
203
- )
204
- salesforce_result += pdf_upload_result + "\n"
205
-
206
- # Link PDF to DPR record
207
- if pdf_content_document_id:
208
- sf.ContentDocumentLink.create({
209
- 'ContentDocumentId': pdf_content_document_id,
210
- 'LinkedEntityId': dpr_record_id,
211
- 'ShareType': 'V'
212
- })
213
-
214
- # Update the DPR record with the PDF URL
215
- if pdf_url:
216
- sf.Daily_Progress_Reports__c.update(dpr_record_id, {
217
- 'PDF_URL__c': pdf_url # Storing the PDF URL correctly
218
- })
219
- salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
220
-
221
- # Upload images to Salesforce and create Site_Images__c records
222
- for file in files:
223
- image_filename = os.path.basename(file.name)
224
- image_content_document_id, image_upload_result = upload_file_to_salesforce(
225
- file.name, image_filename, sf, "image"
226
- )
227
- if image_content_document_id:
228
- image_content_document_ids.append(image_content_document_id)
229
-
230
- # Create Site_Images__c record and link to DPR
231
- site_image_record = sf.Site_Images__c.create({
232
- 'Image__c': image_content_document_id,
233
- 'Related_Report__c': dpr_record_id # Link image to DPR record
234
- })
235
- salesforce_result += image_upload_result + "\n"
236
-
237
- # Link image to DPR record
238
- if image_content_document_id:
239
- sf.ContentDocumentLink.create({
240
- 'ContentDocumentId': image_content_document_id,
241
- 'LinkedEntityId': dpr_record_id,
242
- 'ShareType': 'V'
243
- })
244
-
245
- except Exception as e:
246
- salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
247
- else:
248
- salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
249
-
250
- # Return DPR text, PDF file, and Salesforce upload status
251
- return (
252
- dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
253
- pdf_filepath
254
- )
255
 
256
- # Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
257
- iface = gr.Interface(
258
- fn=generate_dpr,
259
- inputs=gr.Files(type="filepath", label="Upload Site Photos"),
260
- outputs=[
261
- gr.Textbox(label="Daily Progress Report"),
262
- gr.File(label="Download PDF")
263
- ],
264
- title="Daily Progress Report Generator",
265
- 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.",
266
- allow_flagging="never"
267
- )
268
 
 
269
  if __name__ == "__main__":
270
- iface.launch()
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ import requests
3
  from transformers import BlipProcessor, BlipForConditionalGeneration
4
  from PIL import Image
5
+ import io
6
+ import base64
7
+ import os
8
+ from dotenv import load_dotenv
9
+ from simple_salesforce import Salesforce
10
  from reportlab.lib.pagesizes import letter
11
  from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PDFImage
12
  from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
13
  from reportlab.lib import colors
14
+ from datetime import datetime
 
 
 
 
 
15
 
16
  # Load environment variables from .env file
17
  load_dotenv()
 
28
  sf = None
29
  print(f"Failed to connect to Salesforce: {str(e)}")
30
 
31
+ # Initialize Hugging Face model and processor
32
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
33
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
34
  model.eval()
35
  device = "cuda" if torch.cuda.is_available() else "cpu"
36
  model.to(device)
37
 
38
+ # Define FastAPI instance
39
+ app = FastAPI()
40
+
41
+ # Endpoint for image upload and caption generation
42
+ @app.post("/predict/")
43
+ async def predict(file: UploadFile = File(...)):
44
+ image = Image.open(file.file)
45
+
46
+ # Generate caption using Hugging Face model
47
+ caption = generate_captions_from_image(image)
48
+
49
+ # Save the image to a file
50
+ file_path = f"./uploaded_images/{file.filename}"
51
+ image.save(file_path)
52
+
53
+ # Save the daily report as a PDF
54
+ pdf_filename = save_dpr_to_pdf(caption, file_path)
55
+
56
+ # Upload to Salesforce
57
+ if sf:
58
+ salesforce_result = upload_file_to_salesforce(pdf_filename, sf)
59
+ else:
60
+ salesforce_result = "Salesforce connection is not available."
61
+
62
+ return {"caption": caption, "pdf_filename": pdf_filename, "salesforce_result": salesforce_result}
63
+
64
+
65
+ # Function to generate captions from an image
66
  def generate_captions_from_image(image):
67
  if image.mode != "RGB":
68
  image = image.convert("RGB")
69
 
 
 
 
70
  # Preprocess the image and generate a caption
71
  inputs = processor(image, return_tensors="pt").to(device, torch.float16)
72
  output = model.generate(**inputs, max_new_tokens=50)
 
74
 
75
  return caption
76
 
77
+ # Function to save Daily Progress Report to a PDF file
78
+ def save_dpr_to_pdf(caption, image_path):
79
  try:
80
+ # PDF filename
81
+ pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
82
+
83
  # Create a PDF document
84
+ doc = SimpleDocTemplate(pdf_filename, pagesize=letter)
85
  styles = getSampleStyleSheet()
86
 
87
  # Define custom styles
 
109
  # Add title
110
  flowables.append(Paragraph("Daily Progress Report", title_style))
111
 
112
+ # Add image and caption
113
+ flowables.append(PDFImage(image_path, width=200, height=150)) # Adjust image size
114
+ flowables.append(Paragraph(f"Description: {caption}", body_style))
115
+ flowables.append(Spacer(1, 12)) # Add some space between images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  # Build the PDF
118
  doc.build(flowables)
119
+
120
+ return pdf_filename
121
  except Exception as e:
122
+ print(f"Error saving PDF: {str(e)}")
123
+ return None
124
 
125
+ # Function to upload the file to Salesforce
126
+ def upload_file_to_salesforce(pdf_filename, sf_connection):
127
  try:
128
  # Read file content and encode in base64
129
+ with open(pdf_filename, 'rb') as f:
130
  file_content = f.read()
131
  file_content_b64 = base64.b64encode(file_content).decode('utf-8')
132
 
 
 
 
133
  # Create ContentVersion
134
  content_version = sf_connection.ContentVersion.create({
135
+ 'Title': pdf_filename,
136
+ 'PathOnClient': pdf_filename,
137
  'VersionData': file_content_b64,
138
+ 'Description': "Daily Progress Report PDF"
139
  })
140
 
141
  # Get ContentDocumentId
 
145
  )
146
  content_document_id = content_document['records'][0]['ContentDocumentId']
147
 
148
+ # Create the record link in Salesforce
149
+ return f"File {pdf_filename} uploaded successfully with ContentDocumentId: {content_document_id}"
 
 
 
150
  except Exception as e:
151
+ print(f"Error uploading to Salesforce: {str(e)}")
152
+ return f"Error uploading {pdf_filename} to Salesforce: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ # To run the app
156
  if __name__ == "__main__":
157
+ import uvicorn
158
+ uvicorn.run(app, host="0.0.0.0", port=8000)