Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,11 +15,9 @@ SF_USERNAME = os.getenv("SF_USERNAME")
|
|
| 15 |
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
| 16 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 17 |
|
| 18 |
-
# Validate Salesforce credentials
|
| 19 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
|
| 20 |
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN in environment variables.")
|
| 21 |
|
| 22 |
-
# Initialize Salesforce connection
|
| 23 |
try:
|
| 24 |
sf = Salesforce(
|
| 25 |
username=SF_USERNAME,
|
|
@@ -31,13 +29,9 @@ except Exception as e:
|
|
| 31 |
print(f"Salesforce connection failed: {str(e)}")
|
| 32 |
raise
|
| 33 |
|
| 34 |
-
# Valid milestones
|
| 35 |
VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
|
| 36 |
-
|
| 37 |
-
# Adjust the timezone to your local timezone
|
| 38 |
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 39 |
|
| 40 |
-
# Image processing and Salesforce upload
|
| 41 |
def process_image(images, project_name):
|
| 42 |
try:
|
| 43 |
if not images or len(images) == 0:
|
|
@@ -46,15 +40,13 @@ def process_image(images, project_name):
|
|
| 46 |
milestones = []
|
| 47 |
for image in images:
|
| 48 |
img = Image.open(image)
|
| 49 |
-
|
| 50 |
-
# Check if the image exceeds the size limit (20MB)
|
| 51 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 52 |
if image_size_mb > 20:
|
| 53 |
return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
|
| 54 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 55 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 56 |
|
| 57 |
-
# Save image to public folder temporarily before uploading to Salesforce
|
| 58 |
upload_dir = "public_uploads"
|
| 59 |
os.makedirs(upload_dir, exist_ok=True)
|
| 60 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
@@ -62,38 +54,40 @@ def process_image(images, project_name):
|
|
| 62 |
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 63 |
shutil.copy(image, saved_image_path)
|
| 64 |
|
| 65 |
-
# Convert image to base64 before uploading to Salesforce
|
| 66 |
with open(saved_image_path, 'rb') as image_file:
|
| 67 |
image_data = base64.b64encode(image_file.read()).decode('utf-8')
|
| 68 |
|
| 69 |
-
# Create the ContentVersion record in Salesforce
|
| 70 |
-
content_version = {
|
| 71 |
-
'Title': image_filename,
|
| 72 |
-
'PathOnClient': saved_image_path,
|
| 73 |
-
'VersionData': image_data
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
# Upload the file to Salesforce
|
| 77 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
content_version_result = sf.ContentVersion.create(content_version)
|
| 79 |
content_version_id = content_version_result['id']
|
| 80 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 81 |
except Exception as e:
|
| 82 |
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
-
# Default milestone detection logic (mock AI model or image-based hash)
|
| 89 |
img_bytes = img.tobytes()
|
| 90 |
img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
|
| 91 |
milestone_index = img_hash % len(VALID_MILESTONES)
|
| 92 |
milestone = VALID_MILESTONES[milestone_index]
|
|
|
|
| 93 |
|
| 94 |
milestones.append(milestone)
|
| 95 |
|
| 96 |
-
# Determine overall milestone (most advanced detected)
|
| 97 |
final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
|
| 98 |
milestone_completion_map = {
|
| 99 |
"Planning": 10,
|
|
@@ -224,7 +218,7 @@ def process_image(images, project_name):
|
|
| 224 |
except Exception as e:
|
| 225 |
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 226 |
|
| 227 |
-
#
|
| 228 |
with gr.Blocks(css="""
|
| 229 |
.gradio-container {
|
| 230 |
background-color: #f0f4f8;
|
|
|
|
| 15 |
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
| 16 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 17 |
|
|
|
|
| 18 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN]):
|
| 19 |
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD, and SF_SECURITY_TOKEN in environment variables.")
|
| 20 |
|
|
|
|
| 21 |
try:
|
| 22 |
sf = Salesforce(
|
| 23 |
username=SF_USERNAME,
|
|
|
|
| 29 |
print(f"Salesforce connection failed: {str(e)}")
|
| 30 |
raise
|
| 31 |
|
|
|
|
| 32 |
VALID_MILESTONES = ["Planning", "Foundation", "Walls Erected", "Completed"]
|
|
|
|
|
|
|
| 33 |
local_timezone = pytz.timezone("Asia/Kolkata")
|
| 34 |
|
|
|
|
| 35 |
def process_image(images, project_name):
|
| 36 |
try:
|
| 37 |
if not images or len(images) == 0:
|
|
|
|
| 40 |
milestones = []
|
| 41 |
for image in images:
|
| 42 |
img = Image.open(image)
|
| 43 |
+
|
|
|
|
| 44 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 45 |
if image_size_mb > 20:
|
| 46 |
return "Error: One or more images exceed 20MB.", "Failure", "", "", 0
|
| 47 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 48 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 49 |
|
|
|
|
| 50 |
upload_dir = "public_uploads"
|
| 51 |
os.makedirs(upload_dir, exist_ok=True)
|
| 52 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
|
|
| 54 |
saved_image_path = os.path.join(upload_dir, image_filename)
|
| 55 |
shutil.copy(image, saved_image_path)
|
| 56 |
|
|
|
|
| 57 |
with open(saved_image_path, 'rb') as image_file:
|
| 58 |
image_data = base64.b64encode(image_file.read()).decode('utf-8')
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
+
content_version = {
|
| 62 |
+
'Title': image_filename,
|
| 63 |
+
'PathOnClient': saved_image_path,
|
| 64 |
+
'VersionData': image_data
|
| 65 |
+
}
|
| 66 |
content_version_result = sf.ContentVersion.create(content_version)
|
| 67 |
content_version_id = content_version_result['id']
|
| 68 |
file_url = f"https://sathkruthatechsolutionspri8-dev-ed.develop.lightning.force.com/{content_version_id}"
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
|
| 71 |
|
| 72 |
+
# 👇 UPDATED milestone detection based on filename
|
| 73 |
+
filename = os.path.basename(image).lower()
|
| 74 |
+
if "completed" in filename or "finished" in filename:
|
| 75 |
+
milestone = "Completed"
|
| 76 |
+
elif "walls" in filename or "erected" in filename:
|
| 77 |
+
milestone = "Walls Erected"
|
| 78 |
+
elif "foundation" in filename:
|
| 79 |
+
milestone = "Foundation"
|
| 80 |
+
elif "planning" in filename or "plan" in filename:
|
| 81 |
+
milestone = "Planning"
|
| 82 |
else:
|
|
|
|
| 83 |
img_bytes = img.tobytes()
|
| 84 |
img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
|
| 85 |
milestone_index = img_hash % len(VALID_MILESTONES)
|
| 86 |
milestone = VALID_MILESTONES[milestone_index]
|
| 87 |
+
# 👆 END update
|
| 88 |
|
| 89 |
milestones.append(milestone)
|
| 90 |
|
|
|
|
| 91 |
final_milestone = max(set(milestones), key=milestones.count) if milestones else "Planning"
|
| 92 |
milestone_completion_map = {
|
| 93 |
"Planning": 10,
|
|
|
|
| 218 |
except Exception as e:
|
| 219 |
return f"Error: {str(e)}", "Failure", "", "", "0%"
|
| 220 |
|
| 221 |
+
# UI
|
| 222 |
with gr.Blocks(css="""
|
| 223 |
.gradio-container {
|
| 224 |
background-color: #f0f4f8;
|