Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
import requests
|
3 |
-
|
4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
from PIL import Image
|
6 |
-
import gradio as gr
|
7 |
import torch
|
|
|
8 |
from datetime import datetime
|
9 |
from reportlab.lib.pagesizes import letter
|
10 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image as PDFImage
|
@@ -22,7 +21,6 @@ load_dotenv()
|
|
22 |
|
23 |
app = FastAPI()
|
24 |
|
25 |
-
|
26 |
# Salesforce credentials
|
27 |
SF_USERNAME = os.getenv('SF_USERNAME')
|
28 |
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
@@ -42,35 +40,54 @@ model.eval()
|
|
42 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
43 |
model.to(device)
|
44 |
|
|
|
|
|
45 |
|
46 |
-
# FastAPI endpoint to handle image upload and caption generation
|
47 |
@app.post("/predict/")
|
48 |
async def predict(image: UploadFile = File(...)):
|
49 |
try:
|
50 |
# Read the image from the request
|
51 |
image_bytes = await image.read()
|
52 |
-
image = Image.open(BytesIO(image_bytes))
|
53 |
-
|
54 |
-
#
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
except Exception as e:
|
58 |
return {"error": str(e)}
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
def generate_captions_from_image(image):
|
63 |
if image.mode != "RGB":
|
64 |
image = image.convert("RGB")
|
65 |
|
66 |
# Resize image for faster processing
|
67 |
image = image.resize((640, 640))
|
68 |
|
69 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
71 |
output = model.generate(**inputs, max_new_tokens=50)
|
72 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
73 |
-
|
74 |
return caption
|
75 |
|
76 |
# Function to save DPR text to a PDF file
|
@@ -133,148 +150,6 @@ def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
|
|
133 |
except Exception as e:
|
134 |
return f"Error saving PDF: {str(e)}", None
|
135 |
|
136 |
-
# Function to upload a file to Salesforce as ContentVersion
|
137 |
-
def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
|
138 |
-
try:
|
139 |
-
# Read file content and encode in base64
|
140 |
-
with open(file_path, 'rb') as f:
|
141 |
-
file_content = f.read()
|
142 |
-
file_content_b64 = base64.b64encode(file_content).decode('utf-8')
|
143 |
-
|
144 |
-
# Set description based on file type
|
145 |
-
description = "Daily Progress Report PDF" if file_type == "pdf" else "Site Image"
|
146 |
-
|
147 |
-
# Create ContentVersion
|
148 |
-
content_version = sf_connection.ContentVersion.create({
|
149 |
-
'Title': filename,
|
150 |
-
'PathOnClient': filename,
|
151 |
-
'VersionData': file_content_b64,
|
152 |
-
'Description': description
|
153 |
-
})
|
154 |
-
|
155 |
-
# Get ContentDocumentId
|
156 |
-
content_version_id = content_version['id']
|
157 |
-
content_document = sf_connection.query(
|
158 |
-
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
|
159 |
-
)
|
160 |
-
content_document_id = content_document['records'][0]['ContentDocumentId']
|
161 |
-
|
162 |
-
# Generate a valid Salesforce URL for the ContentDocument
|
163 |
-
content_document_url = f"https://{sf_connection.sf_instance}.salesforce.com/{content_document_id}"
|
164 |
-
|
165 |
-
# Ensure the link is valid
|
166 |
-
return content_document_id, content_document_url, f"File {filename} uploaded successfully"
|
167 |
-
except Exception as e:
|
168 |
-
return None, None, f"Error uploading {filename} to Salesforce: {str(e)}"
|
169 |
-
|
170 |
-
# Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
171 |
-
def generate_dpr(files):
|
172 |
-
dpr_text = []
|
173 |
-
captions = []
|
174 |
-
image_paths = []
|
175 |
-
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
176 |
-
|
177 |
-
# Add header to the DPR
|
178 |
-
dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
|
179 |
-
|
180 |
-
# Process images in parallel for faster performance
|
181 |
-
with concurrent.futures.ThreadPoolExecutor() as executor:
|
182 |
-
results = list(executor.map(lambda file: generate_captions_from_image(Image.open(file.name)), files))
|
183 |
-
|
184 |
-
for i, file in enumerate(files):
|
185 |
-
caption = results[i]
|
186 |
-
captions.append(caption)
|
187 |
-
|
188 |
-
# Generate DPR section for this image with dynamic caption
|
189 |
-
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
190 |
-
# Remove the description from the dpr_text section
|
191 |
-
# No need to add it again as the image and caption will be inserted in the PDF
|
192 |
-
dpr_text.append(dpr_section)
|
193 |
-
|
194 |
-
# Save image path for embedding in the report
|
195 |
-
image_paths.append(file.name)
|
196 |
-
|
197 |
-
# Combine DPR text (no redundant description here)
|
198 |
-
dpr_output = "\n".join(dpr_text)
|
199 |
-
|
200 |
-
# Generate PDF filename with timestamp
|
201 |
-
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
202 |
-
|
203 |
-
# Save DPR text to PDF
|
204 |
-
pdf_result, pdf_filepath = save_dpr_to_pdf(dpr_output, image_paths, captions, pdf_filename)
|
205 |
-
|
206 |
-
# Salesforce upload
|
207 |
-
salesforce_result = ""
|
208 |
-
pdf_content_document_id = None
|
209 |
-
pdf_url = None
|
210 |
-
image_content_document_ids = []
|
211 |
-
|
212 |
-
if sf and pdf_filepath:
|
213 |
-
try:
|
214 |
-
# Create Daily_Progress_Reports__c record
|
215 |
-
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
216 |
-
dpr_record = sf.Daily_Progress_Reports__c.create({
|
217 |
-
'Detected_Activities__c': report_description # Store in Detected_Activities__c field
|
218 |
-
})
|
219 |
-
dpr_record_id = dpr_record['id']
|
220 |
-
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
221 |
-
|
222 |
-
# Upload PDF to Salesforce
|
223 |
-
pdf_content_document_id, pdf_url, pdf_upload_result = upload_file_to_salesforce(
|
224 |
-
pdf_filepath, pdf_filename, sf, "pdf"
|
225 |
-
)
|
226 |
-
salesforce_result += pdf_upload_result + "\n"
|
227 |
-
|
228 |
-
# Link PDF to DPR record
|
229 |
-
if pdf_content_document_id:
|
230 |
-
sf.ContentDocumentLink.create({
|
231 |
-
'ContentDocumentId': pdf_content_document_id,
|
232 |
-
'LinkedEntityId': dpr_record_id,
|
233 |
-
'ShareType': 'V'
|
234 |
-
})
|
235 |
-
|
236 |
-
# Update the DPR record with the PDF URL
|
237 |
-
if pdf_url:
|
238 |
-
sf.Daily_Progress_Reports__c.update(dpr_record_id, {
|
239 |
-
'PDF_URL__c': pdf_url # Storing the PDF URL correctly
|
240 |
-
})
|
241 |
-
salesforce_result += f"Updated PDF URL for record ID {dpr_record_id}\n"
|
242 |
-
|
243 |
-
# Upload images to Salesforce and create Site_Images__c records
|
244 |
-
for file in files:
|
245 |
-
image_filename = os.path.basename(file.name)
|
246 |
-
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
247 |
-
file.name, image_filename, sf, "image"
|
248 |
-
)
|
249 |
-
if image_content_document_id:
|
250 |
-
image_content_document_ids.append(image_content_document_id)
|
251 |
-
|
252 |
-
# Create Site_Images__c record and link to DPR
|
253 |
-
site_image_record = sf.Site_Images__c.create({
|
254 |
-
'Image__c': image_content_document_id,
|
255 |
-
'Related_Report__c': dpr_record_id # Link image to DPR record
|
256 |
-
})
|
257 |
-
salesforce_result += image_upload_result + "\n"
|
258 |
-
|
259 |
-
# Link image to DPR record
|
260 |
-
if image_content_document_id:
|
261 |
-
sf.ContentDocumentLink.create({
|
262 |
-
'ContentDocumentId': image_content_document_id,
|
263 |
-
'LinkedEntityId': dpr_record_id,
|
264 |
-
'ShareType': 'V'
|
265 |
-
})
|
266 |
-
|
267 |
-
except Exception as e:
|
268 |
-
salesforce_result += f"Error interacting with Salesforce: {str(e)}\n"
|
269 |
-
else:
|
270 |
-
salesforce_result = "Salesforce connection not available or PDF generation failed.\n"
|
271 |
-
|
272 |
-
# Return DPR text, PDF file, and Salesforce upload status
|
273 |
-
return (
|
274 |
-
dpr_output + f"\n\n{pdf_result}\n\nSalesforce Upload Status:\n{salesforce_result}",
|
275 |
-
pdf_filepath
|
276 |
-
)
|
277 |
-
|
278 |
# Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
|
279 |
iface = gr.Interface(
|
280 |
fn=generate_dpr,
|
@@ -289,6 +164,5 @@ iface = gr.Interface(
|
|
289 |
)
|
290 |
|
291 |
if __name__ == "__main__":
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
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
|
|
|
21 |
|
22 |
app = FastAPI()
|
23 |
|
|
|
24 |
# Salesforce credentials
|
25 |
SF_USERNAME = os.getenv('SF_USERNAME')
|
26 |
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
|
|
40 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
41 |
model.to(device)
|
42 |
|
43 |
+
# FastAPI endpoint to handle image upload and forward it to Hugging Face API for caption generation
|
44 |
+
HUGGING_FACE_ENDPOINT = 'https://huggingface.co/spaces/Rammohan0504/DPR-4/predict'
|
45 |
|
|
|
46 |
@app.post("/predict/")
|
47 |
async def predict(image: UploadFile = File(...)):
|
48 |
try:
|
49 |
# Read the image from the request
|
50 |
image_bytes = await image.read()
|
51 |
+
image = Image.open(io.BytesIO(image_bytes))
|
52 |
+
|
53 |
+
# Forward the image to Hugging Face endpoint
|
54 |
+
response = forward_image_to_huggingface(image)
|
55 |
+
|
56 |
+
# Check the response from Hugging Face
|
57 |
+
if response.status_code == 200:
|
58 |
+
result = response.json()
|
59 |
+
caption = result.get("caption", "No caption found.")
|
60 |
+
return {"caption": caption}
|
61 |
+
else:
|
62 |
+
return {"error": f"Failed to get prediction from Hugging Face Space. Status code: {response.status_code}"}
|
63 |
except Exception as e:
|
64 |
return {"error": str(e)}
|
65 |
|
66 |
+
# Function to forward the image to Hugging Face API
|
67 |
+
def forward_image_to_huggingface(image: Image):
|
|
|
68 |
if image.mode != "RGB":
|
69 |
image = image.convert("RGB")
|
70 |
|
71 |
# Resize image for faster processing
|
72 |
image = image.resize((640, 640))
|
73 |
|
74 |
+
# Convert image to bytes for API request
|
75 |
+
img_byte_arr = io.BytesIO()
|
76 |
+
image.save(img_byte_arr, format='JPEG')
|
77 |
+
img_byte_arr = img_byte_arr.getvalue()
|
78 |
+
|
79 |
+
# Create the payload to send to Hugging Face (it expects a file)
|
80 |
+
files = {'file': ('image.jpg', img_byte_arr, 'image/jpeg')}
|
81 |
+
|
82 |
+
# Make the POST request to Hugging Face Space
|
83 |
+
response = requests.post(HUGGING_FACE_ENDPOINT, files=files)
|
84 |
+
return response
|
85 |
+
|
86 |
+
# Inference function to generate captions dynamically based on image content
|
87 |
+
def generate_captions_from_image(image):
|
88 |
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
89 |
output = model.generate(**inputs, max_new_tokens=50)
|
90 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
|
|
91 |
return caption
|
92 |
|
93 |
# Function to save DPR text to a PDF file
|
|
|
150 |
except Exception as e:
|
151 |
return f"Error saving PDF: {str(e)}", None
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
# Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
|
154 |
iface = gr.Interface(
|
155 |
fn=generate_dpr,
|
|
|
164 |
)
|
165 |
|
166 |
if __name__ == "__main__":
|
167 |
+
iface.launch()
|
168 |
+
|
|