Rekham1110 commited on
Commit
0fa2f2b
·
verified ·
1 Parent(s): 9afe9d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -82
app.py CHANGED
@@ -1,67 +1,9 @@
1
- import gradio as gr
2
- from PIL import Image
3
- import os
4
- from dotenv import load_dotenv
5
- from simple_salesforce import Salesforce
6
  from datetime import datetime
7
- import hashlib
8
- import shutil
9
- import base64
10
  import pytz
11
 
12
- # Load environment variables
13
- load_dotenv()
14
- 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,
26
- password=SF_PASSWORD,
27
- security_token=SF_SECURITY_TOKEN,
28
- domain='login'
29
- )
30
- except Exception as e:
31
- print(f"Salesforce connection failed: {str(e)}")
32
- raise
33
-
34
- # Valid milestones
35
- VALID_MILESTONES = ["Foundation", "Walls Erected", "Planning", "Completed"]
36
-
37
  # Adjust the timezone to your local timezone (replace 'Asia/Kolkata' with your timezone if needed)
38
  local_timezone = pytz.timezone("Asia/Kolkata")
39
 
40
- # Deterministic AI prediction with fixed confidence and percent
41
- def mock_ai_model(image):
42
- img = image.convert("RGB")
43
- max_size = 1024
44
- img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
45
-
46
- img_bytes = img.tobytes()
47
- img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
48
-
49
- milestone_index = img_hash % len(VALID_MILESTONES)
50
- milestone = VALID_MILESTONES[milestone_index]
51
-
52
- milestone_completion_map = {
53
- "Planning": 10,
54
- "Foundation": 30,
55
- "Walls Erected": 50,
56
- "Completed": 100,
57
- }
58
- completion_percent = milestone_completion_map.get(milestone, 0)
59
-
60
- confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
61
- confidence_score = round(confidence_raw, 2)
62
-
63
- return milestone, completion_percent, confidence_score
64
-
65
  # Image processing and Salesforce upload
66
  def process_image(image, project_name):
67
  try:
@@ -108,14 +50,14 @@ def process_image(image, project_name):
108
  milestone, percent_complete, confidence_score = mock_ai_model(img)
109
 
110
  # Adjust the current time to local timezone
111
- local_time = datetime.now(local_timezone).strftime("%Y-%m-%d %H:%M:%S")
112
 
113
  # Create the Salesforce record with the image URL and AI prediction
114
  record = {
115
  "Name__c": project_name,
116
  "Current_Milestone__c": milestone,
117
  "Completion_Percentage__c": percent_complete,
118
- "Last_Updated_On__c": local_time, # Use the adjusted local time
119
  "Upload_Status__c": "Success",
120
  "Comments__c": f"{milestone} with {confidence_score*100}% confidence", # Removed "AI Prediction:"
121
  "Last_Updated_Image__c": file_url
@@ -137,25 +79,3 @@ def process_image(image, project_name):
137
 
138
  except Exception as e:
139
  return f"Error: {str(e)}", "Failure", "", "", 0
140
-
141
- # Gradio UI
142
- with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial;} .title {color: #2c3e50; font-size: 24px; text-align: center;}") as demo:
143
- gr.Markdown("<h1 class='title'>Construction Milestone Detector</h1>")
144
- with gr.Row():
145
- image_input = gr.Image(type="filepath", label="Upload Construction Site Photo (JPG/PNG, ≤ 20MB)")
146
- project_name_input = gr.Textbox(label="Project Name (Required)", placeholder="e.g. Project_12345")
147
-
148
- submit_button = gr.Button("Process Image")
149
- output_text = gr.Textbox(label="Result")
150
- upload_status = gr.Textbox(label="Upload Status")
151
- milestone = gr.Textbox(label="Detected Milestone")
152
- confidence = gr.Textbox(label="Confidence Score")
153
- progress = gr.Slider(0, 100, label="Completion Percentage", interactive=False, value=0)
154
-
155
- submit_button.click(
156
- fn=process_image,
157
- inputs=[image_input, project_name_input],
158
- outputs=[output_text, upload_status, milestone, confidence, progress]
159
- )
160
-
161
- demo.launch(share=True)
 
 
 
 
 
 
1
  from datetime import datetime
 
 
 
2
  import pytz
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  # Adjust the timezone to your local timezone (replace 'Asia/Kolkata' with your timezone if needed)
5
  local_timezone = pytz.timezone("Asia/Kolkata")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # Image processing and Salesforce upload
8
  def process_image(image, project_name):
9
  try:
 
50
  milestone, percent_complete, confidence_score = mock_ai_model(img)
51
 
52
  # Adjust the current time to local timezone
53
+ local_time = datetime.now(local_timezone).strftime("%Y-%m-%dT%H:%M:%SZ") # Updated format for Salesforce
54
 
55
  # Create the Salesforce record with the image URL and AI prediction
56
  record = {
57
  "Name__c": project_name,
58
  "Current_Milestone__c": milestone,
59
  "Completion_Percentage__c": percent_complete,
60
+ "Last_Updated_On__c": local_time, # Correct format for Salesforce
61
  "Upload_Status__c": "Success",
62
  "Comments__c": f"{milestone} with {confidence_score*100}% confidence", # Removed "AI Prediction:"
63
  "Last_Updated_Image__c": file_url
 
79
 
80
  except Exception as e:
81
  return f"Error: {str(e)}", "Failure", "", "", 0