Spaces:
Sleeping
Sleeping
File size: 9,123 Bytes
bb9e235 7d8ec5e 9eb2e2a 6916a8a 822fee8 5fc5e6a 0258ba7 5fc5e6a 822fee8 19d8d8c c4e3ea5 bb9e235 df97270 3dfd15f bb9e235 0efb9f9 bb9e235 0efb9f9 6916a8a 9c8ba2d 6916a8a bb9e235 6916a8a bb9e235 6916a8a 9c8ba2d 6916a8a 9c8ba2d bb9e235 826aebe 6916a8a 826aebe bd14ff7 826aebe 505497c 826aebe 6916a8a 826aebe bb9e235 7e4e6e0 bb9e235 7e4e6e0 bb9e235 7e4e6e0 826aebe bd14ff7 7e4e6e0 bb9e235 7e4e6e0 45e6457 7e4e6e0 eb71bff bb9e235 eb71bff bb9e235 eb71bff 7e4e6e0 bb9e235 822fee8 c4e3ea5 5fc5e6a 6916a8a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
from transformers import AutoProcessor, AutoModelForImageClassification
from PIL import Image
import gradio as gr
import torch
from datetime import datetime
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PDFImage
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from simple_salesforce import Salesforce
import os
from dotenv import load_dotenv
import base64
import io
import concurrent.futures
# Load environment variables from .env file
load_dotenv()
# Salesforce credentials
SF_USERNAME = os.getenv('SF_USERNAME')
SF_PASSWORD = os.getenv('SF_PASSWORD')
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
# Initialize Salesforce connection
try:
sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN)
except Exception as e:
sf = None
print(f"Failed to connect to Salesforce: {str(e)}")
# Load ViT model and processor (generic ImageNet pretrained)
processor = AutoProcessor.from_pretrained("google/vit-base-patch16-224")
model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")
model.eval()
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# Inference function to classify image and get predicted label
def generate_captions_from_image(image):
if image.mode != "RGB":
image = image.convert("RGB")
inputs = processor(images=image, return_tensors="pt").to(device)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
predicted_label = model.config.id2label[predicted_class_idx]
return predicted_label
# Function to save DPR text to a PDF file
def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
try:
# Create a PDF document
doc = SimpleDocTemplate(filename, pagesize=letter)
styles = getSampleStyleSheet()
# Define custom styles
title_style = ParagraphStyle(
name='Title',
fontSize=16,
leading=20,
alignment=1, # Center
spaceAfter=20,
textColor=colors.black,
fontName='Helvetica-Bold'
)
body_style = ParagraphStyle(
name='Body',
fontSize=12,
leading=14,
spaceAfter=10,
textColor=colors.black,
fontName='Helvetica'
)
# Build the PDF content
flowables = []
# Add title
flowables.append(Paragraph("Daily Progress Report", title_style))
# Split DPR text into lines and add as paragraphs (excluding descriptions for images)
for line in dpr_text.split('\n'):
# Replace problematic characters for PDF
line = line.replace('\u2019', "'").replace('\u2018', "'")
if line.strip():
flowables.append(Paragraph(line, body_style))
else:
flowables.append(Spacer(1, 12))
# Add images and captions in the correct order
for img_path, caption in zip(image_paths, captions):
try:
img = PDFImage(img_path, width=200, height=150) # Adjust image size if needed
flowables.append(img)
description = f"Description: {caption}"
flowables.append(Paragraph(description, body_style))
flowables.append(Spacer(1, 12))
except Exception as e:
flowables.append(Paragraph(f"Error loading image: {str(e)}", body_style))
# Build the PDF
doc.build(flowables)
return f"PDF saved successfully as {filename}", filename
except Exception as e:
return f"Error saving PDF: {str(e)}", None
# Function to upload file to Salesforce as ContentVersion
def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
try:
with open(file_path, 'rb') as f:
file_content = f.read()
file_content_b64 = base64.b64encode(file_content).decode('utf-8')
description = "Daily Progress Report PDF" if file_type == "pdf" else "Site Image"
content_version = sf_connection.ContentVersion.create({
'Title': filename,
'PathOnClient': filename,
'VersionData': file_content_b64,
'Description': description
})
content_version_id = content_version['id']
content_document = sf_connection.query(
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
)
content_document_id = content_document['records'][0]['ContentDocumentId']
content_document_url = f"https://{sf_connection.sf_instance}/sfc/servlet.shepherd/version/download/{content_version_id}"
return content_document_id, content_document_url, f"File {filename} uploaded successfully"
except Exception as e:
return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
# Generate DPR, save PDF, upload to Salesforce
def generate_dpr(files):
dpr_text = []
captions = []
image_paths = []
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
with concurrent.futures.ThreadPoolExecutor() as executor:
results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
for i, file in enumerate(files):
caption = results[i]
captions.append(caption)
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
dpr_text.append(dpr_section)
image_paths.append(file.name)
dpr_output = "\n".join(dpr_text)
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
salesforce_result = ""
pdf_content_document_id = None
pdf_url = None
if sf and pdf_filepath:
try:
report_description = "; ".join(captions)[:255]
dpr_record = sf.Daily_Progress_Reports__c.create({
'Detected_Activities__c': report_description
})
dpr_record_id = dpr_record['id']
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
pdf_filepath, pdf_filename, sf, "pdf"
)
salesforce_result += pdf_upload_result + "\n"
if pdf_content_document_id:
sf.ContentDocumentLink.create({
'ContentDocumentId': pdf_content_document_id,
'LinkedEntityId': dpr_record_id,
'ShareType': 'V'
})
if pdf_url:
sf.Daily_Progress_Reports__c.update(dpr_record_id, {
'PDF_URL__c': pdf_url
})
salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
for file in files:
image_filename = os.path.basename(file.name)
image_content_document_id, image_url, image_upload_result = upload_file_to_salesforce(
file.name, image_filename, sf, "image"
)
if image_content_document_id:
sf.ContentDocumentLink.create({
'ContentDocumentId': image_content_document_id,
'LinkedEntityId': dpr_record_id,
'ShareType': 'V'
})
sf.Daily_Progress_Reports__c.update(dpr_record_id, {
'Site_Images__c': image_content_document_id
})
salesforce_result += image_upload_result + "\n"
except Exception as e:
salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
else:
salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
return (
dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
pdf_filepath
)
iface = gr.Interface(
fn=generate_dpr,
inputs=gr.Files(type="filepath", label="Upload Site Photos"),
outputs=[
gr.Textbox(label="Daily Progress Report"),
gr.File(label="Download PDF")
],
title="Daily Progress Report Generator",
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.",
allow_flagging="never"
)
if __name__ == "__main__":
iface.launch()
|