Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,7 @@ except Exception as e:
|
|
| 35 |
print(f"Salesforce connection failed: {str(e)}")
|
| 36 |
raise
|
| 37 |
|
| 38 |
-
# Load pre-trained model and processor
|
| 39 |
model_name = "google/vit-base-patch16-224" # Placeholder; replace with "nasreshsuguru/construction-milestone-detector" if trained
|
| 40 |
processor = ViTImageProcessor.from_pretrained(model_name)
|
| 41 |
model = ViTForImageClassification.from_pretrained(model_name)
|
|
@@ -48,18 +48,18 @@ def process_image(image, project_name):
|
|
| 48 |
try:
|
| 49 |
# Validate inputs
|
| 50 |
if image is None:
|
| 51 |
-
return "Error: Please upload an image to proceed.", "Pending", "",
|
| 52 |
if not project_name:
|
| 53 |
-
return "Error: Please enter a project name to proceed.", "Pending", "",
|
| 54 |
if not re.match(r'^[a-zA-Z0-9\s-]+$', project_name):
|
| 55 |
-
return "Error: Project name must be alphanumeric (letters, numbers, spaces, or hyphens).", "Pending", "",
|
| 56 |
|
| 57 |
# Validate image size and type
|
| 58 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 59 |
if image_size_mb > 20:
|
| 60 |
-
return "Error: Image size exceeds 20MB.", "Failure", "",
|
| 61 |
if not image.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 62 |
-
return "Error: Only JPG/PNG images are supported.", "Failure", "",
|
| 63 |
|
| 64 |
# Preprocess image
|
| 65 |
img = Image.open(image).convert("RGB")
|
|
@@ -80,21 +80,18 @@ def process_image(image, project_name):
|
|
| 80 |
|
| 81 |
# Map indices to labels
|
| 82 |
predicted_idx = top_indices[0]
|
| 83 |
-
confidence = top_probs[0]
|
| 84 |
milestone = model.config.id2label.get(predicted_idx, "Unknown Milestone")
|
| 85 |
-
completion_percent = min(max(int(confidence * 100), 0), 100)
|
| 86 |
|
| 87 |
# Format top predictions
|
| 88 |
-
prediction_details = "\n".join([f"{model.config.id2label.get(idx, 'Unknown')}: {prob:.2f}" for idx, prob in zip(top_indices, top_probs)])
|
| 89 |
|
| 90 |
# Update Salesforce record
|
| 91 |
record = {
|
| 92 |
"Name": project_name,
|
| 93 |
"Current_Milestone__c": milestone,
|
| 94 |
-
"Completion_Percentage__c": completion_percent,
|
| 95 |
"Last_Updated_On__c": datetime.now().isoformat(),
|
| 96 |
"Upload_Status__c": "Success",
|
| 97 |
-
"Comments__c": f"AI
|
| 98 |
"Version__c": 1
|
| 99 |
}
|
| 100 |
|
|
@@ -110,19 +107,17 @@ def process_image(image, project_name):
|
|
| 110 |
else:
|
| 111 |
sf.Construction_Project__c.create(record)
|
| 112 |
except Exception as e:
|
| 113 |
-
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "",
|
| 114 |
|
| 115 |
return (
|
| 116 |
-
f"Success: Milestone: {milestone}
|
| 117 |
"Success",
|
| 118 |
milestone,
|
| 119 |
-
completion_percent,
|
| 120 |
-
confidence,
|
| 121 |
prediction_details
|
| 122 |
)
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
-
return f"Error: {str(e)}", "Failure", "",
|
| 126 |
|
| 127 |
# Gradio interface
|
| 128 |
with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial;} .title {color: #2c3e50; font-size: 24px; text-align: center;}") as demo:
|
|
@@ -133,8 +128,6 @@ with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: A
|
|
| 133 |
output_text = gr.Textbox(label="Result")
|
| 134 |
upload_status = gr.Textbox(label="Upload Status")
|
| 135 |
milestone = gr.Textbox(label="Detected Milestone")
|
| 136 |
-
completion_percent = gr.Slider(0, 100, label="Completion Percentage (%)", interactive=False)
|
| 137 |
-
confidence_score = gr.Slider(0, 1, label="Confidence Score", interactive=False)
|
| 138 |
prediction_details = gr.Textbox(label="Top Predictions")
|
| 139 |
progress = gr.Slider(0, 100, label="Processing Progress", interactive=False, value=0)
|
| 140 |
|
|
@@ -150,11 +143,10 @@ with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: A
|
|
| 150 |
).then(
|
| 151 |
fn=process_image,
|
| 152 |
inputs=[image_input, project_name],
|
| 153 |
-
outputs=[output_text, upload_status, milestone,
|
| 154 |
).then(
|
| 155 |
fn=complete_progress,
|
| 156 |
outputs=progress
|
| 157 |
)
|
| 158 |
|
| 159 |
-
if __name__ == "__main__":
|
| 160 |
-
demo.launch()
|
|
|
|
| 35 |
print(f"Salesforce connection failed: {str(e)}")
|
| 36 |
raise
|
| 37 |
|
| 38 |
+
# Load pre-trained model and processor
|
| 39 |
model_name = "google/vit-base-patch16-224" # Placeholder; replace with "nasreshsuguru/construction-milestone-detector" if trained
|
| 40 |
processor = ViTImageProcessor.from_pretrained(model_name)
|
| 41 |
model = ViTForImageClassification.from_pretrained(model_name)
|
|
|
|
| 48 |
try:
|
| 49 |
# Validate inputs
|
| 50 |
if image is None:
|
| 51 |
+
return "Error: Please upload an image to proceed.", "Pending", "", ""
|
| 52 |
if not project_name:
|
| 53 |
+
return "Error: Please enter a project name to proceed.", "Pending", "", ""
|
| 54 |
if not re.match(r'^[a-zA-Z0-9\s-]+$', project_name):
|
| 55 |
+
return "Error: Project name must be alphanumeric (letters, numbers, spaces, or hyphens).", "Pending", "", ""
|
| 56 |
|
| 57 |
# Validate image size and type
|
| 58 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 59 |
if image_size_mb > 20:
|
| 60 |
+
return "Error: Image size exceeds 20MB.", "Failure", "", ""
|
| 61 |
if not image.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 62 |
+
return "Error: Only JPG/PNG images are supported.", "Failure", "", ""
|
| 63 |
|
| 64 |
# Preprocess image
|
| 65 |
img = Image.open(image).convert("RGB")
|
|
|
|
| 80 |
|
| 81 |
# Map indices to labels
|
| 82 |
predicted_idx = top_indices[0]
|
|
|
|
| 83 |
milestone = model.config.id2label.get(predicted_idx, "Unknown Milestone")
|
|
|
|
| 84 |
|
| 85 |
# Format top predictions
|
| 86 |
+
prediction_details = "\n".join([f"{model.config.id2label.get(idx, 'Unknown Milestone')}: {prob:.2f}" for idx, prob in zip(top_indices, top_probs)])
|
| 87 |
|
| 88 |
# Update Salesforce record
|
| 89 |
record = {
|
| 90 |
"Name": project_name,
|
| 91 |
"Current_Milestone__c": milestone,
|
|
|
|
| 92 |
"Last_Updated_On__c": datetime.now().isoformat(),
|
| 93 |
"Upload_Status__c": "Success",
|
| 94 |
+
"Comments__c": f"AI Prediction: {milestone}",
|
| 95 |
"Version__c": 1
|
| 96 |
}
|
| 97 |
|
|
|
|
| 107 |
else:
|
| 108 |
sf.Construction_Project__c.create(record)
|
| 109 |
except Exception as e:
|
| 110 |
+
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", prediction_details
|
| 111 |
|
| 112 |
return (
|
| 113 |
+
f"Success: Milestone: {milestone}",
|
| 114 |
"Success",
|
| 115 |
milestone,
|
|
|
|
|
|
|
| 116 |
prediction_details
|
| 117 |
)
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
+
return f"Error: {str(e)}", "Failure", "", ""
|
| 121 |
|
| 122 |
# Gradio interface
|
| 123 |
with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial;} .title {color: #2c3e50; font-size: 24px; text-align: center;}") as demo:
|
|
|
|
| 128 |
output_text = gr.Textbox(label="Result")
|
| 129 |
upload_status = gr.Textbox(label="Upload Status")
|
| 130 |
milestone = gr.Textbox(label="Detected Milestone")
|
|
|
|
|
|
|
| 131 |
prediction_details = gr.Textbox(label="Top Predictions")
|
| 132 |
progress = gr.Slider(0, 100, label="Processing Progress", interactive=False, value=0)
|
| 133 |
|
|
|
|
| 143 |
).then(
|
| 144 |
fn=process_image,
|
| 145 |
inputs=[image_input, project_name],
|
| 146 |
+
outputs=[output_text, upload_status, milestone, prediction_details]
|
| 147 |
).then(
|
| 148 |
fn=complete_progress,
|
| 149 |
outputs=progress
|
| 150 |
)
|
| 151 |
|
| 152 |
+
if __name__ == "__main__":
|
|
|