Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 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
|
|
@@ -90,42 +91,60 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 90 |
def upload_image_and_get_url(image_path):
|
| 91 |
"""
|
| 92 |
Upload the image to the uploads directory in Hugging Face Space and return a public URL.
|
|
|
|
| 93 |
"""
|
| 94 |
import shutil
|
| 95 |
import os
|
| 96 |
from datetime import datetime
|
| 97 |
import uuid
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# Create the uploads directory if it doesn't exist
|
| 105 |
try:
|
| 106 |
os.makedirs(public_dir, exist_ok=True)
|
| 107 |
-
# Ensure the directory is writable
|
| 108 |
os.chmod(public_dir, 0o775)
|
| 109 |
logger.info(f"Created or verified uploads directory at: {public_dir}")
|
| 110 |
except Exception as e:
|
| 111 |
logger.error(f"Failed to create uploads directory: {e}")
|
| 112 |
raise
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
unique_filename = f"{uuid.uuid4().hex}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 116 |
destination_path = os.path.join(public_dir, unique_filename)
|
| 117 |
-
|
| 118 |
-
# Copy the image to the uploads directory
|
| 119 |
try:
|
| 120 |
-
shutil.
|
| 121 |
-
# Ensure the file is readable
|
| 122 |
os.chmod(destination_path, 0o664)
|
| 123 |
-
logger.info(f"Successfully
|
| 124 |
except Exception as e:
|
| 125 |
-
logger.error(f"Failed to
|
| 126 |
raise
|
| 127 |
|
| 128 |
-
# Construct the public URL using the HF_SPACE_URL from .env
|
| 129 |
space_url = os.environ.get('HF_SPACE_URL', 'https://huggingface.co/spaces/pavansuresh/Smartlab_tech')
|
| 130 |
public_url = f"{space_url.rstrip('/')}/uploads/{unique_filename}"
|
| 131 |
logger.info(f"Generated public URL: {public_url}")
|
|
@@ -157,7 +176,7 @@ def save_record_to_salesforce(annotated_image_url, coverage_percent, original_im
|
|
| 157 |
'Name': record_name,
|
| 158 |
'Annotated_Image__c': annotated_image_url,
|
| 159 |
'Coverage_Percentage__c': round(coverage_percent, 2),
|
| 160 |
-
'Original_Image__c': original_image_url,
|
| 161 |
'Compliance_Status__c': compliance_status,
|
| 162 |
'Technician_ID__c': technician_id,
|
| 163 |
'Verified_On__c': datetime.utcnow().isoformat()
|
|
|
|
| 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
|
|
|
|
| 91 |
def upload_image_and_get_url(image_path):
|
| 92 |
"""
|
| 93 |
Upload the image to the uploads directory in Hugging Face Space and return a public URL.
|
| 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 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 105 |
+
logger.info(f"Created temporary directory at: {temp_dir}")
|
| 106 |
+
except Exception as e:
|
| 107 |
+
logger.error(f"Failed to create temporary directory {temp_dir}: {e}")
|
| 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 |
+
temp_path = os.path.join(temp_dir, unique_filename)
|
| 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 |
+
shutil.move(temp_path, destination_path)
|
|
|
|
| 141 |
os.chmod(destination_path, 0o664)
|
| 142 |
+
logger.info(f"Successfully moved image to: {destination_path}")
|
| 143 |
except Exception as e:
|
| 144 |
+
logger.error(f"Failed to move image to {destination_path}: {e}")
|
| 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}")
|
|
|
|
| 176 |
'Name': record_name,
|
| 177 |
'Annotated_Image__c': annotated_image_url,
|
| 178 |
'Coverage_Percentage__c': round(coverage_percent, 2),
|
| 179 |
+
'Original_Image__c': original_image_url,
|
| 180 |
'Compliance_Status__c': compliance_status,
|
| 181 |
'Technician_ID__c': technician_id,
|
| 182 |
'Verified_On__c': datetime.utcnow().isoformat()
|