Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,35 +22,30 @@ try:
|
|
| 22 |
username=SF_USERNAME,
|
| 23 |
password=SF_PASSWORD,
|
| 24 |
security_token=SF_SECURITY_TOKEN,
|
| 25 |
-
domain='login' # Use 'test' for sandbox
|
| 26 |
)
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Salesforce connection failed: {str(e)}")
|
| 29 |
raise
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
"In Progress": "In_Progress",
|
| 34 |
-
"Started": "In_Progress",
|
| 35 |
-
"Completed": "Completed",
|
| 36 |
-
"Planning": "Planning"
|
| 37 |
-
}
|
| 38 |
|
| 39 |
-
# Mock AI model for milestone detection
|
| 40 |
def mock_ai_model(image):
|
|
|
|
| 41 |
img = image.convert("RGB")
|
| 42 |
max_size = 1024
|
| 43 |
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
completion_percent = 30
|
| 49 |
confidence_score = round(random.uniform(0.85, 0.95), 2)
|
| 50 |
|
| 51 |
return milestone, completion_percent, confidence_score
|
| 52 |
|
| 53 |
-
# Function for Gradio UI
|
| 54 |
def process_image(image):
|
| 55 |
try:
|
| 56 |
if image is None:
|
|
@@ -64,7 +59,9 @@ def process_image(image):
|
|
| 64 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 65 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 66 |
|
|
|
|
| 67 |
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
|
|
|
| 68 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 69 |
|
| 70 |
record = {
|
|
@@ -92,7 +89,7 @@ def process_image(image):
|
|
| 92 |
except Exception as e:
|
| 93 |
return f"Error: {str(e)}", "Failure", "", "", 0
|
| 94 |
|
| 95 |
-
# Gradio interface
|
| 96 |
with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial;} .title {color: #2c3e50; font-size: 24px; text-align: center;}") as demo:
|
| 97 |
gr.Markdown("<h1 class='title'>Construction Milestone Detector</h1>")
|
| 98 |
image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)")
|
|
@@ -109,5 +106,4 @@ with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: A
|
|
| 109 |
outputs=[output_text, upload_status, milestone, confidence, progress]
|
| 110 |
)
|
| 111 |
|
| 112 |
-
# Launch the Gradio app
|
| 113 |
demo.launch(share=False)
|
|
|
|
| 22 |
username=SF_USERNAME,
|
| 23 |
password=SF_PASSWORD,
|
| 24 |
security_token=SF_SECURITY_TOKEN,
|
| 25 |
+
domain='login' # Use 'test' for sandbox
|
| 26 |
)
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Salesforce connection failed: {str(e)}")
|
| 29 |
raise
|
| 30 |
|
| 31 |
+
# ✅ Valid milestone values from Salesforce picklist
|
| 32 |
+
VALID_MILESTONES = ["Foundation", "Walls Erected", "Planning", "Completed"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Mock AI model for milestone detection (since we can't train a real model here)
|
| 35 |
def mock_ai_model(image):
|
| 36 |
+
# Preprocessing: Resize, normalize (simulated)
|
| 37 |
img = image.convert("RGB")
|
| 38 |
max_size = 1024
|
| 39 |
img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 40 |
|
| 41 |
+
# Simulate milestone detection by picking a random valid value
|
| 42 |
+
milestone = random.choice(VALID_MILESTONES)
|
| 43 |
+
completion_percent = random.choice([10, 30, 50, 80, 100]) # Just for variety
|
|
|
|
| 44 |
confidence_score = round(random.uniform(0.85, 0.95), 2)
|
| 45 |
|
| 46 |
return milestone, completion_percent, confidence_score
|
| 47 |
|
| 48 |
+
# Function for Gradio UI to process the image
|
| 49 |
def process_image(image):
|
| 50 |
try:
|
| 51 |
if image is None:
|
|
|
|
| 59 |
if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 60 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
| 61 |
|
| 62 |
+
# Run mock AI model
|
| 63 |
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 64 |
+
|
| 65 |
unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 66 |
|
| 67 |
record = {
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
return f"Error: {str(e)}", "Failure", "", "", 0
|
| 91 |
|
| 92 |
+
# Gradio interface for testing
|
| 93 |
with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial;} .title {color: #2c3e50; font-size: 24px; text-align: center;}") as demo:
|
| 94 |
gr.Markdown("<h1 class='title'>Construction Milestone Detector</h1>")
|
| 95 |
image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)")
|
|
|
|
| 106 |
outputs=[output_text, upload_status, milestone, confidence, progress]
|
| 107 |
)
|
| 108 |
|
|
|
|
| 109 |
demo.launch(share=False)
|