Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import numpy as np
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
-
import shutil
|
| 8 |
from paddleocr import PaddleOCR
|
| 9 |
import time
|
| 10 |
from simple_salesforce import Salesforce
|
|
@@ -88,69 +87,66 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 88 |
|
| 89 |
pdf.output(output_path)
|
| 90 |
|
| 91 |
-
|
|
|
|
| 92 |
"""
|
| 93 |
-
Upload the image to
|
| 94 |
-
Use /tmp as a temporary storage location, then copy to the persistent uploads folder.
|
| 95 |
"""
|
| 96 |
-
import shutil
|
| 97 |
-
import os
|
| 98 |
-
from datetime import datetime
|
| 99 |
-
import uuid
|
| 100 |
-
|
| 101 |
-
# Step 1: Write to /tmp (guaranteed writable in Hugging Face Spaces)
|
| 102 |
-
temp_dir = "/tmp/uploads"
|
| 103 |
try:
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
-
logger.error(f"
|
| 108 |
raise
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# Generate a unique filename to avoid conflicts
|
| 111 |
unique_filename = f"{uuid.uuid4().hex}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# Copy the image to the temporary directory
|
| 115 |
-
try:
|
| 116 |
-
shutil.copy(image_path, temp_path)
|
| 117 |
-
os.chmod(temp_path, 0o664)
|
| 118 |
-
logger.info(f"Successfully copied image to temporary path: {temp_path}")
|
| 119 |
-
except Exception as e:
|
| 120 |
-
logger.error(f"Failed to copy image to {temp_path}: {e}")
|
| 121 |
-
raise
|
| 122 |
-
|
| 123 |
-
# Step 2: Define the uploads directory in the Space's repository root
|
| 124 |
-
# The repository root is typically the current working directory in Hugging Face Spaces
|
| 125 |
-
repo_root = os.getcwd() # Should be the root of the Space (e.g., where app.py is)
|
| 126 |
-
public_dir = os.path.join(repo_root, "uploads")
|
| 127 |
-
|
| 128 |
-
# Create the uploads directory if it doesn't exist
|
| 129 |
-
try:
|
| 130 |
-
os.makedirs(public_dir, exist_ok=True)
|
| 131 |
-
os.chmod(public_dir, 0o775)
|
| 132 |
-
logger.info(f"Created or verified uploads directory at: {public_dir}")
|
| 133 |
-
except Exception as e:
|
| 134 |
-
logger.error(f"Failed to create uploads directory: {e}")
|
| 135 |
-
raise
|
| 136 |
-
|
| 137 |
-
# Step 3: Copy the file from /tmp to the persistent uploads folder
|
| 138 |
-
destination_path = os.path.join(public_dir, unique_filename)
|
| 139 |
try:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
logger.info(f"Successfully moved image to: {destination_path}")
|
| 143 |
except Exception as e:
|
| 144 |
-
logger.error(f"Failed to
|
| 145 |
raise
|
| 146 |
|
| 147 |
-
# Step 4: Construct the public URL using the HF_SPACE_URL from .env
|
| 148 |
-
space_url = os.environ.get('HF_SPACE_URL', 'https://huggingface.co/spaces/pavansuresh/Smartlab_tech')
|
| 149 |
-
public_url = f"{space_url.rstrip('/')}/uploads/{unique_filename}"
|
| 150 |
-
logger.info(f"Generated public URL: {public_url}")
|
| 151 |
-
|
| 152 |
-
return public_url
|
| 153 |
-
|
| 154 |
def save_record_to_salesforce(annotated_image_url, coverage_percent, original_image_pil, compliance_threshold=80):
|
| 155 |
sf = Salesforce(
|
| 156 |
username=os.environ['SF_USERNAME'],
|
|
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
import tempfile
|
| 6 |
import os
|
|
|
|
| 7 |
from paddleocr import PaddleOCR
|
| 8 |
import time
|
| 9 |
from simple_salesforce import Salesforce
|
|
|
|
| 87 |
|
| 88 |
pdf.output(output_path)
|
| 89 |
|
| 90 |
+
# New function to upload image to Salesforce and get URL (adapted from reference code)
|
| 91 |
+
def upload_image_to_salesforce(image_path, image_name, record_id=None):
|
| 92 |
"""
|
| 93 |
+
Upload the image to Salesforce as a ContentVersion and return a public URL.
|
|
|
|
| 94 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
+
sf = Salesforce(
|
| 97 |
+
username=os.environ['SF_USERNAME'],
|
| 98 |
+
password=os.environ['SF_PASSWORD'],
|
| 99 |
+
security_token=os.environ['SF_SECURITY_TOKEN'],
|
| 100 |
+
domain=os.environ.get('SF_DOMAIN', 'login')
|
| 101 |
+
)
|
| 102 |
+
logger.debug(f"Uploading image {image_name} for record ID: {record_id}")
|
| 103 |
+
|
| 104 |
+
# Read the image file and encode it as base64
|
| 105 |
+
with open(image_path, "rb") as f:
|
| 106 |
+
image_data = f.read()
|
| 107 |
+
|
| 108 |
+
encoded_image_data = base64.b64encode(image_data).decode('utf-8')
|
| 109 |
+
|
| 110 |
+
# Create a ContentVersion in Salesforce
|
| 111 |
+
content_version_data = {
|
| 112 |
+
"Title": image_name,
|
| 113 |
+
"PathOnClient": image_name,
|
| 114 |
+
"VersionData": encoded_image_data,
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
if record_id:
|
| 118 |
+
content_version_data["FirstPublishLocationId"] = record_id
|
| 119 |
+
|
| 120 |
+
content_version = sf.ContentVersion.create(content_version_data)
|
| 121 |
+
content_version_id = content_version["id"]
|
| 122 |
+
logger.info(f"Image uploaded to Salesforce with ContentVersion ID: {content_version_id}")
|
| 123 |
+
|
| 124 |
+
# Generate the public URL for the image
|
| 125 |
+
image_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{content_version_id}"
|
| 126 |
+
logger.debug(f"Generated image URL: {image_url}")
|
| 127 |
+
return image_url
|
| 128 |
except Exception as e:
|
| 129 |
+
logger.error(f"Error uploading image to Salesforce: {str(e)}", exc_info=True)
|
| 130 |
raise
|
| 131 |
|
| 132 |
+
def upload_image_and_get_url(image_path):
|
| 133 |
+
"""
|
| 134 |
+
Upload the image to Salesforce and return a public URL.
|
| 135 |
+
"""
|
| 136 |
+
from datetime import datetime
|
| 137 |
+
import uuid
|
| 138 |
+
|
| 139 |
# Generate a unique filename to avoid conflicts
|
| 140 |
unique_filename = f"{uuid.uuid4().hex}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 141 |
+
|
| 142 |
+
# Upload the image to Salesforce and get the URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
try:
|
| 144 |
+
image_url = upload_image_to_salesforce(image_path, unique_filename)
|
| 145 |
+
return image_url
|
|
|
|
| 146 |
except Exception as e:
|
| 147 |
+
logger.error(f"Failed to upload image to Salesforce: {e}")
|
| 148 |
raise
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def save_record_to_salesforce(annotated_image_url, coverage_percent, original_image_pil, compliance_threshold=80):
|
| 151 |
sf = Salesforce(
|
| 152 |
username=os.environ['SF_USERNAME'],
|