Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile
|
| 2 |
-
import requests
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
from PIL import Image
|
| 5 |
-
import torch
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
from reportlab.lib.pagesizes import letter
|
| 9 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PDFImage
|
|
@@ -19,8 +17,6 @@ import concurrent.futures
|
|
| 19 |
# Load environment variables from .env file
|
| 20 |
load_dotenv()
|
| 21 |
|
| 22 |
-
app = FastAPI()
|
| 23 |
-
|
| 24 |
# Salesforce credentials
|
| 25 |
SF_USERNAME = os.getenv('SF_USERNAME')
|
| 26 |
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
|
@@ -40,7 +36,7 @@ model.eval()
|
|
| 40 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
model.to(device)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
def generate_captions_from_image(image):
|
| 45 |
if image.mode != "RGB":
|
| 46 |
image = image.convert("RGB")
|
|
@@ -55,48 +51,99 @@ def generate_captions_from_image(image):
|
|
| 55 |
|
| 56 |
return caption
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
@app.post("/predict/")
|
| 62 |
-
async def predict(image: UploadFile = File(...)):
|
| 63 |
try:
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
-
return
|
| 80 |
|
| 81 |
-
# Function to
|
| 82 |
-
def
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
response = requests.post(HUGGING_FACE_ENDPOINT, files=files)
|
| 99 |
-
return response
|
| 100 |
|
| 101 |
# Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
| 102 |
def generate_dpr(files):
|
|
@@ -118,6 +165,8 @@ def generate_dpr(files):
|
|
| 118 |
|
| 119 |
# Generate DPR section for this image with dynamic caption
|
| 120 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
|
|
|
|
|
|
| 121 |
dpr_text.append(dpr_section)
|
| 122 |
|
| 123 |
# Save image path for embedding in the report
|
|
@@ -204,66 +253,6 @@ def generate_dpr(files):
|
|
| 204 |
pdf_filepath
|
| 205 |
)
|
| 206 |
|
| 207 |
-
# Function to save DPR text to a PDF file
|
| 208 |
-
def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
|
| 209 |
-
try:
|
| 210 |
-
# Create a PDF document
|
| 211 |
-
doc = SimpleDocTemplate(filename, pagesize=letter)
|
| 212 |
-
styles = getSampleStyleSheet()
|
| 213 |
-
|
| 214 |
-
# Define custom styles
|
| 215 |
-
title_style = ParagraphStyle(
|
| 216 |
-
name='Title',
|
| 217 |
-
fontSize=16,
|
| 218 |
-
leading=20,
|
| 219 |
-
alignment=1, # Center
|
| 220 |
-
spaceAfter=20,
|
| 221 |
-
textColor=colors.black,
|
| 222 |
-
fontName='Helvetica-Bold'
|
| 223 |
-
)
|
| 224 |
-
body_style = ParagraphStyle(
|
| 225 |
-
name='Body',
|
| 226 |
-
fontSize=12,
|
| 227 |
-
leading=14,
|
| 228 |
-
spaceAfter=10,
|
| 229 |
-
textColor=colors.black,
|
| 230 |
-
fontName='Helvetica'
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
# Build the PDF content
|
| 234 |
-
flowables = []
|
| 235 |
-
|
| 236 |
-
# Add title
|
| 237 |
-
flowables.append(Paragraph("Daily Progress Report", title_style))
|
| 238 |
-
|
| 239 |
-
# Split DPR text into lines and add as paragraphs (excluding descriptions for images)
|
| 240 |
-
for line in dpr_text.split('\n'):
|
| 241 |
-
# Replace problematic characters for PDF
|
| 242 |
-
line = line.replace('\u2019', "'").replace('\u2018', "'")
|
| 243 |
-
if line.strip():
|
| 244 |
-
flowables.append(Paragraph(line, body_style))
|
| 245 |
-
else:
|
| 246 |
-
flowables.append(Spacer(1, 12))
|
| 247 |
-
|
| 248 |
-
# Add images and captions in the correct order (no need to add description to dpr_text again)
|
| 249 |
-
for img_path, caption in zip(image_paths, captions):
|
| 250 |
-
try:
|
| 251 |
-
# Add image first
|
| 252 |
-
img = PDFImage(img_path, width=200, height=150) # Adjust image size if needed
|
| 253 |
-
flowables.append(img)
|
| 254 |
-
# Add description below the image
|
| 255 |
-
description = f"Description: {caption}"
|
| 256 |
-
flowables.append(Paragraph(description, body_style))
|
| 257 |
-
flowables.append(Spacer(1, 12)) # Add some space between images
|
| 258 |
-
except Exception as e:
|
| 259 |
-
flowables.append(Paragraph(f"Error loading image: {str(e)}", body_style))
|
| 260 |
-
|
| 261 |
-
# Build the PDF
|
| 262 |
-
doc.build(flowables)
|
| 263 |
-
return f"PDF saved successfully as {filename}", filename
|
| 264 |
-
except Exception as e:
|
| 265 |
-
return f"Error saving PDF: {str(e)}", None
|
| 266 |
-
|
| 267 |
# Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
|
| 268 |
iface = gr.Interface(
|
| 269 |
fn=generate_dpr,
|
|
@@ -278,5 +267,4 @@ iface = gr.Interface(
|
|
| 278 |
)
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 17 |
# Load environment variables from .env file
|
| 18 |
load_dotenv()
|
| 19 |
|
|
|
|
|
|
|
| 20 |
# Salesforce credentials
|
| 21 |
SF_USERNAME = os.getenv('SF_USERNAME')
|
| 22 |
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
|
|
|
| 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")
|
|
|
|
| 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
|
| 62 |
+
title_style = ParagraphStyle(
|
| 63 |
+
name='Title',
|
| 64 |
+
fontSize=16,
|
| 65 |
+
leading=20,
|
| 66 |
+
alignment=1, # Center
|
| 67 |
+
spaceAfter=20,
|
| 68 |
+
textColor=colors.black,
|
| 69 |
+
fontName='Helvetica-Bold'
|
| 70 |
+
)
|
| 71 |
+
body_style = ParagraphStyle(
|
| 72 |
+
name='Body',
|
| 73 |
+
fontSize=12,
|
| 74 |
+
leading=14,
|
| 75 |
+
spaceAfter=10,
|
| 76 |
+
textColor=colors.black,
|
| 77 |
+
fontName='Helvetica'
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Build the PDF content
|
| 81 |
+
flowables = []
|
| 82 |
+
|
| 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
|
| 134 |
+
content_version_id = content_version['id']
|
| 135 |
+
content_document = sf_connection.query(
|
| 136 |
+
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
|
| 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):
|
|
|
|
| 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
|
|
|
|
| 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,
|
|
|
|
| 267 |
)
|
| 268 |
|
| 269 |
if __name__ == "__main__":
|
| 270 |
+
iface.launch()
|
|
|