Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,26 @@ 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 |
|
11 |
# Load BLIP model and processor
|
12 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
@@ -70,13 +90,41 @@ def save_dpr_to_pdf(dpr_text, filename):
|
|
70 |
|
71 |
# Build the PDF
|
72 |
doc.build(flowables)
|
73 |
-
return f"PDF saved successfully as {filename}"
|
74 |
except Exception as e:
|
75 |
-
return f"Error saving PDF: {str(e)}"
|
76 |
|
77 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
def generate_dpr(files):
|
79 |
dpr_text = []
|
|
|
80 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
81 |
|
82 |
# Add header to the DPR
|
@@ -85,13 +133,14 @@ def generate_dpr(files):
|
|
85 |
# Process each uploaded file (image)
|
86 |
for file in files:
|
87 |
# Open the image from file path
|
88 |
-
image = Image.open(file.name)
|
89 |
|
90 |
if image.mode != "RGB":
|
91 |
image = image.convert("RGB")
|
92 |
|
93 |
# Dynamically generate a caption based on the image
|
94 |
caption = generate_captions_from_image(image)
|
|
|
95 |
|
96 |
# Generate DPR section for this image with dynamic caption
|
97 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
@@ -104,22 +153,73 @@ def generate_dpr(files):
|
|
104 |
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
105 |
|
106 |
# Save DPR text to PDF
|
107 |
-
pdf_result = save_dpr_to_pdf(dpr_output, pdf_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
# Return
|
110 |
-
return
|
|
|
|
|
|
|
111 |
|
112 |
# Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
|
113 |
iface = gr.Interface(
|
114 |
fn=generate_dpr,
|
115 |
-
inputs=gr.Files(type="filepath", label="Upload Site Photos"),
|
116 |
outputs=[
|
117 |
-
gr.Textbox(label="Daily Progress Report"),
|
118 |
-
gr.File(label="Download PDF")
|
119 |
],
|
120 |
title="Daily Progress Report Generator",
|
121 |
-
description="Upload up to 10 site photos. The AI model will
|
122 |
-
allow_flagging="never"
|
123 |
)
|
124 |
|
125 |
if __name__ == "__main__":
|
|
|
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
|
11 |
+
import os
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
import base64
|
14 |
+
import io
|
15 |
+
|
16 |
+
# Load environment variables from .env file
|
17 |
+
load_dotenv()
|
18 |
+
|
19 |
+
# Salesforce credentials
|
20 |
+
SF_USERNAME = os.getenv('SF_USERNAME')
|
21 |
+
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
22 |
+
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
|
23 |
+
|
24 |
+
# Initialize Salesforce connection
|
25 |
+
try:
|
26 |
+
sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN)
|
27 |
+
except Exception as e:
|
28 |
+
sf = None
|
29 |
+
print(f"Failed to connect to Salesforce: {str(e)}")
|
30 |
|
31 |
# Load BLIP model and processor
|
32 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
90 |
|
91 |
# Build the PDF
|
92 |
doc.build(flowables)
|
93 |
+
return f"PDF saved successfully as {filename}", filename
|
94 |
except Exception as e:
|
95 |
+
return f"Error saving PDF: {str(e)}", None
|
96 |
|
97 |
+
# Function to upload a file to Salesforce as ContentVersion
|
98 |
+
def upload_file_to_salesforce(file_path, filename, sf_connection, field_name):
|
99 |
+
try:
|
100 |
+
# Read file content and encode in base64
|
101 |
+
with open(file_path, 'rb') as f:
|
102 |
+
file_content = f.read()
|
103 |
+
file_content_b64 = base64.b64encode(file_content).decode('utf-8')
|
104 |
+
|
105 |
+
# Create ContentVersion
|
106 |
+
content_version = sf_connection.ContentVersion.create({
|
107 |
+
'Title': filename,
|
108 |
+
'PathOnClient': filename,
|
109 |
+
'VersionData': file_content_b64,
|
110 |
+
'Description': f'Uploaded for {field_name}'
|
111 |
+
})
|
112 |
+
|
113 |
+
# Get ContentDocumentId
|
114 |
+
content_version_id = content_version['id']
|
115 |
+
content_document = sf_connection.query(
|
116 |
+
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
|
117 |
+
)
|
118 |
+
content_document_id = content_document['records'][0]['ContentDocumentId']
|
119 |
+
|
120 |
+
return content_document_id, f"File {filename} uploaded successfully for {field_name}"
|
121 |
+
except Exception as e:
|
122 |
+
return None, f"Error uploading {filename} to Salesforce for {field_name}: {str(e)}"
|
123 |
+
|
124 |
+
# Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
125 |
def generate_dpr(files):
|
126 |
dpr_text = []
|
127 |
+
captions = []
|
128 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
129 |
|
130 |
# Add header to the DPR
|
|
|
133 |
# Process each uploaded file (image)
|
134 |
for file in files:
|
135 |
# Open the image from file path
|
136 |
+
image = Image.open(file.name)
|
137 |
|
138 |
if image.mode != "RGB":
|
139 |
image = image.convert("RGB")
|
140 |
|
141 |
# Dynamically generate a caption based on the image
|
142 |
caption = generate_captions_from_image(image)
|
143 |
+
captions.append(caption)
|
144 |
|
145 |
# Generate DPR section for this image with dynamic caption
|
146 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
|
|
153 |
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
154 |
|
155 |
# Save DPR text to PDF
|
156 |
+
pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, pdf_filename)
|
157 |
+
|
158 |
+
# Salesforce upload
|
159 |
+
salesforce_result = ""
|
160 |
+
if sf and pdf_filepath:
|
161 |
+
try:
|
162 |
+
# Create Daily_Progress_Reports__c record
|
163 |
+
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
164 |
+
dpr_record = sf.Daily_Progress_Reports__c.create({
|
165 |
+
'Report_Date__c': current_time,
|
166 |
+
'Report_Description__c': report_description
|
167 |
+
})
|
168 |
+
dpr_record_id = dpr_record['id']
|
169 |
+
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
170 |
+
|
171 |
+
# Upload PDF to Salesforce for Report_PDF__c
|
172 |
+
pdf_content_document_id, pdf_upload_result = upload_file_to_salesforce(
|
173 |
+
pdf_filepath, pdf_filename, sf, 'Report_PDF__c'
|
174 |
+
)
|
175 |
+
salesforce_result += pdf_upload_result + "\n"
|
176 |
+
|
177 |
+
# Link PDF to DPR record
|
178 |
+
if pdf_content_document_id:
|
179 |
+
sf.ContentDocumentLink.create({
|
180 |
+
'ContentDocumentId': pdf_content_document_id,
|
181 |
+
'LinkedEntityId': dpr_record_id,
|
182 |
+
'ShareType': 'V'
|
183 |
+
})
|
184 |
+
|
185 |
+
# Upload images to Salesforce for Site_Images__c
|
186 |
+
for file in files:
|
187 |
+
image_filename = os.path.basename(file.name)
|
188 |
+
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
189 |
+
file.name, image_filename, sf, 'Site_Images__c'
|
190 |
+
)
|
191 |
+
salesforce_result += image_upload_result + "\n"
|
192 |
+
|
193 |
+
# Link image to DPR record
|
194 |
+
if image_content_document_id:
|
195 |
+
sf.ContentDocumentLink.create({
|
196 |
+
'ContentDocumentId': image_content_document_id,
|
197 |
+
'LinkedEntityId': dpr_record_id,
|
198 |
+
'ShareType': 'V'
|
199 |
+
})
|
200 |
+
|
201 |
+
except Exception as e:
|
202 |
+
salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
|
203 |
+
else:
|
204 |
+
salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
|
205 |
|
206 |
+
# Return DPR text, PDF file, and Salesforce upload status
|
207 |
+
return (
|
208 |
+
dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
|
209 |
+
pdf_filepath
|
210 |
+
)
|
211 |
|
212 |
# Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
|
213 |
iface = gr.Interface(
|
214 |
fn=generate_dpr,
|
215 |
+
inputs=gr.Files(type="filepath", label="Upload Site Photos"),
|
216 |
outputs=[
|
217 |
+
gr.Textbox(label="Daily Progress Report"),
|
218 |
+
gr.File(label="Download PDF")
|
219 |
],
|
220 |
title="Daily Progress Report Generator",
|
221 |
+
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 to Report_PDF__c and images to Site_Images__c in Salesforce under Daily_Progress_Reports__c. Download the PDF locally if needed.",
|
222 |
+
allow_flagging="never"
|
223 |
)
|
224 |
|
225 |
if __name__ == "__main__":
|