Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,23 @@ from PIL import Image
|
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Load BLIP model and processor
|
8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
@@ -23,6 +40,51 @@ def generate_captions_from_image(image):
|
|
23 |
|
24 |
return caption
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Function to generate the daily progress report (DPR) text
|
27 |
def generate_dpr(files):
|
28 |
dpr_text = []
|
@@ -46,8 +108,24 @@ def generate_dpr(files):
|
|
46 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
47 |
dpr_text.append(dpr_section)
|
48 |
|
49 |
-
#
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# Gradio interface for uploading multiple files and displaying the text-based DPR
|
53 |
iface = gr.Interface(
|
|
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
from datetime import datetime
|
6 |
+
from fpdf import FPDF
|
7 |
+
from simple_salesforce import Salesforce
|
8 |
+
import requests
|
9 |
+
import json
|
10 |
+
import os
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
|
13 |
+
# Load environment variables from .env file
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
# Salesforce credentials
|
17 |
+
SF_USERNAME = os.getenv('SF_USERNAME')
|
18 |
+
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
19 |
+
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
|
20 |
+
|
21 |
+
# Initialize Salesforce connection
|
22 |
+
sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN)
|
23 |
|
24 |
# Load BLIP model and processor
|
25 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
40 |
|
41 |
return caption
|
42 |
|
43 |
+
# Function to create PDF and upload to Salesforce
|
44 |
+
def create_and_upload_pdf(dpr_content):
|
45 |
+
# Create PDF instance using FPDF
|
46 |
+
pdf = FPDF()
|
47 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
48 |
+
pdf.add_page()
|
49 |
+
|
50 |
+
# Set title and content for the PDF
|
51 |
+
pdf.set_font("Arial", size=12)
|
52 |
+
pdf.cell(200, 10, txt="Daily Progress Report", ln=True, align='C')
|
53 |
+
pdf.ln(10) # Add space between lines
|
54 |
+
|
55 |
+
# Add the content of the DPR text to the PDF
|
56 |
+
pdf.multi_cell(0, 10, dpr_content)
|
57 |
+
|
58 |
+
# Save PDF to a file (temporary storage)
|
59 |
+
pdf_output_path = "/tmp/dpr_report.pdf"
|
60 |
+
pdf.output(pdf_output_path)
|
61 |
+
|
62 |
+
# Upload PDF to Salesforce as ContentVersion
|
63 |
+
with open(pdf_output_path, 'rb') as pdf_file:
|
64 |
+
pdf_data = pdf_file.read()
|
65 |
+
|
66 |
+
# Prepare request to Salesforce
|
67 |
+
content_version_payload = {
|
68 |
+
"Title": "Daily Progress Report",
|
69 |
+
"PathOnClient": "DPR_Report.pdf",
|
70 |
+
"VersionData": pdf_data
|
71 |
+
}
|
72 |
+
|
73 |
+
content_version_url = f"{sf.base_url}/services/data/v50.0/sobjects/ContentVersion/"
|
74 |
+
headers = {
|
75 |
+
"Authorization": f"Bearer {sf.session_id}",
|
76 |
+
"Content-Type": "application/json"
|
77 |
+
}
|
78 |
+
|
79 |
+
response = requests.post(content_version_url, headers=headers, files={"VersionData": pdf_data}, data=json.dumps(content_version_payload))
|
80 |
+
|
81 |
+
if response.status_code == 201:
|
82 |
+
content_version = response.json()
|
83 |
+
pdf_url = f"/sfc/servlet.shepherd/version/download/{content_version['Id']}"
|
84 |
+
return pdf_url
|
85 |
+
else:
|
86 |
+
raise Exception("Error uploading PDF to Salesforce: " + response.text)
|
87 |
+
|
88 |
# Function to generate the daily progress report (DPR) text
|
89 |
def generate_dpr(files):
|
90 |
dpr_text = []
|
|
|
108 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
109 |
dpr_text.append(dpr_section)
|
110 |
|
111 |
+
# Join the DPR sections to form the complete report
|
112 |
+
dpr_content = "\n".join(dpr_text)
|
113 |
+
|
114 |
+
# Create and upload the PDF for this report in Salesforce
|
115 |
+
pdf_url = create_and_upload_pdf(dpr_content)
|
116 |
+
|
117 |
+
# Create a new Salesforce record for this report
|
118 |
+
new_report_data = {
|
119 |
+
"Report_Date__c": current_time, # Set Report Date to current time
|
120 |
+
"Detected_Activities__c": dpr_content, # Set the activities field to the generated text
|
121 |
+
"Photo_Uploads__c": ", ".join([file.name for file in files]), # Upload file names as Photo Uploads
|
122 |
+
"PDF_URL__c": pdf_url # Set the PDF URL field with the generated PDF link
|
123 |
+
}
|
124 |
+
|
125 |
+
# Insert new Daily Progress Report record into Salesforce
|
126 |
+
new_report = sf.Daily_Progress_Reports__c.create(new_report_data)
|
127 |
+
|
128 |
+
return f"New report created in Salesforce with PDF URL: {pdf_url}"
|
129 |
|
130 |
# Gradio interface for uploading multiple files and displaying the text-based DPR
|
131 |
iface = gr.Interface(
|