Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ 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
|
8 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
9 |
from reportlab.lib import colors
|
10 |
from simple_salesforce import Salesforce
|
@@ -52,7 +52,7 @@ def generate_captions_from_image(image):
|
|
52 |
return caption
|
53 |
|
54 |
# Function to save DPR text to a PDF file
|
55 |
-
def save_dpr_to_pdf(dpr_text, filename):
|
56 |
try:
|
57 |
# Create a PDF document
|
58 |
doc = SimpleDocTemplate(filename, pagesize=letter)
|
@@ -92,6 +92,14 @@ def save_dpr_to_pdf(dpr_text, filename):
|
|
92 |
else:
|
93 |
flowables.append(Spacer(1, 12))
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
# Build the PDF
|
96 |
doc.build(flowables)
|
97 |
return f"PDF saved successfully as {filename}", filename
|
@@ -135,6 +143,7 @@ def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
|
|
135 |
def generate_dpr(files):
|
136 |
dpr_text = []
|
137 |
captions = []
|
|
|
138 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
139 |
|
140 |
# Add header to the DPR
|
@@ -151,6 +160,9 @@ def generate_dpr(files):
|
|
151 |
# Generate DPR section for this image with dynamic caption
|
152 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
153 |
dpr_text.append(dpr_section)
|
|
|
|
|
|
|
154 |
|
155 |
# Combine DPR text
|
156 |
dpr_output = "\n".join(dpr_text)
|
@@ -159,7 +171,7 @@ def generate_dpr(files):
|
|
159 |
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
160 |
|
161 |
# Save DPR text to PDF
|
162 |
-
pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, pdf_filename)
|
163 |
|
164 |
# Salesforce upload
|
165 |
salesforce_result = ""
|
@@ -171,7 +183,6 @@ def generate_dpr(files):
|
|
171 |
try:
|
172 |
# Create Daily_Progress_Reports__c record
|
173 |
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
174 |
-
# The fix: Assign the report_description to 'Detected_Activities__c' instead of 'Report_Description__c'
|
175 |
dpr_record = sf.Daily_Progress_Reports__c.create({
|
176 |
'Detected_Activities__c': report_description # Store in Detected_Activities__c field
|
177 |
})
|
@@ -199,7 +210,7 @@ def generate_dpr(files):
|
|
199 |
})
|
200 |
salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
|
201 |
|
202 |
-
# Upload images to Salesforce
|
203 |
for file in files:
|
204 |
image_filename = os.path.basename(file.name)
|
205 |
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
@@ -207,6 +218,12 @@ def generate_dpr(files):
|
|
207 |
)
|
208 |
if image_content_document_id:
|
209 |
image_content_document_ids.append(image_content_document_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
salesforce_result += image_upload_result + "\n"
|
211 |
|
212 |
# Link image to DPR record
|
|
|
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
|
|
|
52 |
return caption
|
53 |
|
54 |
# Function to save DPR text to a PDF file
|
55 |
+
def save_dpr_to_pdf(dpr_text, image_paths, filename):
|
56 |
try:
|
57 |
# Create a PDF document
|
58 |
doc = SimpleDocTemplate(filename, pagesize=letter)
|
|
|
92 |
else:
|
93 |
flowables.append(Spacer(1, 12))
|
94 |
|
95 |
+
# Add images to the report
|
96 |
+
for img_path in image_paths:
|
97 |
+
try:
|
98 |
+
img = PDFImage(img_path, width=200, height=150) # Adjust image size if needed
|
99 |
+
flowables.append(img)
|
100 |
+
except Exception as e:
|
101 |
+
flowables.append(Paragraph(f"Error loading image: {str(e)}", body_style))
|
102 |
+
|
103 |
# Build the PDF
|
104 |
doc.build(flowables)
|
105 |
return f"PDF saved successfully as {filename}", filename
|
|
|
143 |
def generate_dpr(files):
|
144 |
dpr_text = []
|
145 |
captions = []
|
146 |
+
image_paths = []
|
147 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
148 |
|
149 |
# Add header to the DPR
|
|
|
160 |
# Generate DPR section for this image with dynamic caption
|
161 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
162 |
dpr_text.append(dpr_section)
|
163 |
+
|
164 |
+
# Save image path for embedding in the report
|
165 |
+
image_paths.append(file.name)
|
166 |
|
167 |
# Combine DPR text
|
168 |
dpr_output = "\n".join(dpr_text)
|
|
|
171 |
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
172 |
|
173 |
# Save DPR text to PDF
|
174 |
+
pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, pdf_filename)
|
175 |
|
176 |
# Salesforce upload
|
177 |
salesforce_result = ""
|
|
|
183 |
try:
|
184 |
# Create Daily_Progress_Reports__c record
|
185 |
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
|
|
186 |
dpr_record = sf.Daily_Progress_Reports__c.create({
|
187 |
'Detected_Activities__c': report_description # Store in Detected_Activities__c field
|
188 |
})
|
|
|
210 |
})
|
211 |
salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
|
212 |
|
213 |
+
# Upload images to Salesforce and create Site_Images__c records
|
214 |
for file in files:
|
215 |
image_filename = os.path.basename(file.name)
|
216 |
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
|
|
218 |
)
|
219 |
if image_content_document_id:
|
220 |
image_content_document_ids.append(image_content_document_id)
|
221 |
+
|
222 |
+
# Create Site_Images__c record
|
223 |
+
site_image_record = sf.Site_Images__c.create({
|
224 |
+
'Image__c': image_content_document_id,
|
225 |
+
'Related_Report__c': dpr_record_id # Link image to DPR record
|
226 |
+
})
|
227 |
salesforce_result += image_upload_result + "\n"
|
228 |
|
229 |
# Link image to DPR record
|