Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -83,12 +83,111 @@ def forward_image_to_huggingface(image: Image):
|
|
83 |
response = requests.post(HUGGING_FACE_ENDPOINT, files=files)
|
84 |
return response
|
85 |
|
86 |
-
#
|
87 |
-
def
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Function to save DPR text to a PDF file
|
94 |
def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
|
@@ -163,6 +262,8 @@ iface = gr.Interface(
|
|
163 |
allow_flagging="never"
|
164 |
)
|
165 |
|
|
|
|
|
166 |
if __name__ == "__main__":
|
167 |
iface.launch()
|
168 |
|
|
|
83 |
response = requests.post(HUGGING_FACE_ENDPOINT, files=files)
|
84 |
return response
|
85 |
|
86 |
+
# Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
87 |
+
def generate_dpr(files):
|
88 |
+
dpr_text = []
|
89 |
+
captions = []
|
90 |
+
image_paths = []
|
91 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
92 |
+
|
93 |
+
# Add header to the DPR
|
94 |
+
dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
|
95 |
+
|
96 |
+
# Process images in parallel for faster performance
|
97 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
98 |
+
results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
|
99 |
+
|
100 |
+
for i, file in enumerate(files):
|
101 |
+
caption = results[i]
|
102 |
+
captions.append(caption)
|
103 |
+
|
104 |
+
# Generate DPR section for this image with dynamic caption
|
105 |
+
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
106 |
+
dpr_text.append(dpr_section)
|
107 |
+
|
108 |
+
# Save image path for embedding in the report
|
109 |
+
image_paths.append(file.name)
|
110 |
+
|
111 |
+
# Combine DPR text (no redundant description here)
|
112 |
+
dpr_output = "\n".join(dpr_text)
|
113 |
+
|
114 |
+
# Generate PDF filename with timestamp
|
115 |
+
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
116 |
+
|
117 |
+
# Save DPR text to PDF
|
118 |
+
pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
|
119 |
+
|
120 |
+
# Salesforce upload
|
121 |
+
salesforce_result = ""
|
122 |
+
pdf_content_document_id = None
|
123 |
+
pdf_url = None
|
124 |
+
image_content_document_ids = []
|
125 |
+
|
126 |
+
if sf and pdf_filepath:
|
127 |
+
try:
|
128 |
+
# Create Daily_Progress_Reports__c record
|
129 |
+
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
130 |
+
dpr_record = sf.Daily_Progress_Reports__c.create({
|
131 |
+
'Detected_Activities__c': report_description # Store in Detected_Activities__c field
|
132 |
+
})
|
133 |
+
dpr_record_id = dpr_record['id']
|
134 |
+
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
135 |
+
|
136 |
+
# Upload PDF to Salesforce
|
137 |
+
pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
|
138 |
+
pdf_filepath, pdf_filename, sf, "pdf"
|
139 |
+
)
|
140 |
+
salesforce_result += pdf_upload_result + "\n"
|
141 |
+
|
142 |
+
# Link PDF to DPR record
|
143 |
+
if pdf_content_document_id:
|
144 |
+
sf.ContentDocumentLink.create({
|
145 |
+
'ContentDocumentId': pdf_content_document_id,
|
146 |
+
'LinkedEntityId': dpr_record_id,
|
147 |
+
'ShareType': 'V'
|
148 |
+
})
|
149 |
+
|
150 |
+
# Update the DPR record with the PDF URL
|
151 |
+
if pdf_url:
|
152 |
+
sf.Daily_Progress_Reports__c.update(dpr_record_id, {
|
153 |
+
'PDF_URL__c': pdf_url # Storing the PDF URL correctly
|
154 |
+
})
|
155 |
+
salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
|
156 |
+
|
157 |
+
# Upload images to Salesforce and create Site_Images__c records
|
158 |
+
for file in files:
|
159 |
+
image_filename = os.path.basename(file.name)
|
160 |
+
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
161 |
+
file.name, image_filename, sf, "image"
|
162 |
+
)
|
163 |
+
if image_content_document_id:
|
164 |
+
image_content_document_ids.append(image_content_document_id)
|
165 |
+
|
166 |
+
# Create Site_Images__c record and link to DPR
|
167 |
+
site_image_record = sf.Site_Images__c.create({
|
168 |
+
'Image__c': image_content_document_id,
|
169 |
+
'Related_Report__c': dpr_record_id # Link image to DPR record
|
170 |
+
})
|
171 |
+
salesforce_result += image_upload_result + "\n"
|
172 |
+
|
173 |
+
# Link image to DPR record
|
174 |
+
if image_content_document_id:
|
175 |
+
sf.ContentDocumentLink.create({
|
176 |
+
'ContentDocumentId': image_content_document_id,
|
177 |
+
'LinkedEntityId': dpr_record_id,
|
178 |
+
'ShareType': 'V'
|
179 |
+
})
|
180 |
+
|
181 |
+
except Exception as e:
|
182 |
+
salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
|
183 |
+
else:
|
184 |
+
salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
|
185 |
+
|
186 |
+
# Return DPR text, PDF file, and Salesforce upload status
|
187 |
+
return (
|
188 |
+
dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
|
189 |
+
pdf_filepath
|
190 |
+
)
|
191 |
|
192 |
# Function to save DPR text to a PDF file
|
193 |
def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
|
|
|
262 |
allow_flagging="never"
|
263 |
)
|
264 |
|
265 |
+
|
266 |
+
|
267 |
if __name__ == "__main__":
|
268 |
iface.launch()
|
269 |
|