Rekham1110 commited on
Commit
84496c9
·
verified ·
1 Parent(s): 245f291

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -0
app.py CHANGED
@@ -2,8 +2,204 @@ import streamlit as st
2
  import requests
3
  import os
4
  from PIL import Image
 
 
 
 
 
5
  import numpy as np
6
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  from simple_salesforce import Salesforceimport streamlit as st
8
  import requests
9
  import os
 
2
  import requests
3
  import os
4
  from PIL import Image
5
+ import numpy as np```python
6
+ import streamlit as st
7
+ import requests
8
+ import os
9
+ from PIL import Image
10
  import numpy as np
11
  from transformers import pipeline
12
+ from simple_salesforce import Salesforce
13
+ import io
14
+ import time
15
+ from dotenv import load_dotenv
16
+
17
+ # Load environment variables from .env file
18
+ load_dotenv()
19
+
20
+ # Set up page title and layout
21
+ st.title("Construction Project Progress Tracker")
22
+ st.write("Upload a photo of your construction site, and the AI will tell you the progress!")
23
+
24
+ # Salesforce connection using .env variables
25
+ try:
26
+ sf = Salesforce(
27
+ username=os.getenv('SALESFORCE_USERNAME'),
28
+ password=os.getenv('SALESFORCE_PASSWORD'),
29
+ security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'),
30
+ domain=os.getenv('SALESFORCE_DOMAIN')
31
+ )
32
+ except Exception as e:
33
+ st.error(f"Failed to connect to Salesforce: {str(e)}")
34
+ st.stop()
35
+
36
+ # Hugging Face model (placeholder; replace with your custom-trained model)
37
+ # Using a demo image classification model for now
38
+ model = pipeline("image-classification", model="microsoft/resnet-50")
39
+
40
+ # Function to validate photo size (< 20MB)
41
+ def validate_photo_size(image_file):
42
+ max_size_mb = 20
43
+ image_file.seek(0, os.SEEK_END)
44
+ file_size_mb = image_file.tell() / (1024 * 1024) # Convert bytes to MB
45
+ image_file.seek(0) # Reset file pointer
46
+ return file_size_mb <= max_size_mb
47
+
48
+ # Function to process image with AI and predict milestone
49
+ def predict_milestone(image):
50
+ try:
51
+ # Simulate AI processing time (ensure < 5 seconds)
52
+ start_time = time.time()
53
+
54
+ # Process image with Hugging Face model
55
+ predictions = model(image)
56
+
57
+ # Placeholder logic: Map model output to construction milestones
58
+ # Replace with actual milestone mapping based on your trained model
59
+ milestone = predictions[0]["label"] # Example: "positive" -> "Walls Erected"
60
+ confidence = predictions[0]["score"]
61
+
62
+ # Map model output to construction milestones (customize this)
63
+ milestone_map = {
64
+ "positive": "Walls Erected",
65
+ "negative": "Foundation Completed",
66
+ # Add more mappings based on your model
67
+ }
68
+ completion_map = {
69
+ "positive": 60.00, # Example: Walls = 60% complete
70
+ "negative": 20.00, # Example: Foundation = 20% complete
71
+ }
72
+
73
+ predicted_milestone = milestone_map.get(milestone, "Unknown Milestone")
74
+ completion_percentage = completion_map.get(milestone, 0.00)
75
+
76
+ processing_time = time.time() - start_time
77
+ if processing_time > 5:
78
+ return None, None, "AI took too long to process (> 5 seconds)."
79
+
80
+ return predicted_milestone, completion_percentage, None
81
+ except Exception as e:
82
+ return None, None, f"AI failed to process the image: {str(e)}"
83
+
84
+ # Function to upload image to Salesforce and get a URL
85
+ def upload_image_to_salesforce(image_file, project_id):
86
+ try:
87
+ # Placeholder: Simulate uploading image to Salesforce ContentVersion
88
+ # Replace with actual Salesforce file upload API call
89
+ # Example: Upload to ContentVersion and get ContentDocumentLink
90
+ image_url = f"https://your-salesforce-instance.com/file/{project_id}.jpg" # Simulated URL
91
+ return image_url, None
92
+ except Exception as e:
93
+ return None, f"Failed to upload image to Salesforce: {str(e)}"
94
+
95
+ # Function to update Salesforce Construction_Project__c object
96
+ def update_salesforce_record(project_id, milestone, percentage, image_url, status, comments):
97
+ try:
98
+ # Query to check if the project exists
99
+ query = f"SELECT Id FROM Construction_Project__c WHERE Name = '{project_id}'"
100
+ result = sf.query(query)
101
+
102
+ if result['totalSize'] == 0:
103
+ return f"No project found with ID: {project_id}"
104
+
105
+ record_id = result['records'][0]['Id']
106
+
107
+ # Update the record
108
+ sf.Construction_Project__c.update(record_id, {
109
+ 'Current_Milestone__c': milestone,
110
+ 'Completion_Percentage__c': percentage,
111
+ 'Last_Updated_Image__c': image_url,
112
+ 'Last_Updated_On__c': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
113
+ 'Upload_Status__c': status,
114
+ 'Comments__c': comments
115
+ })
116
+ return None
117
+ except Exception as e:
118
+ return f"Failed to update Salesforce: {str(e)}"
119
+
120
+ # Streamlit UI
121
+ with st.form(key="photo_upload_form"):
122
+ project_id = st.text_input("Enter Project ID (e.g., Sunshine Apartments)", "Sunshine Apartments")
123
+ uploaded_file = st.file_uploader("Upload a Construction Photo", type=["jpg", "jpeg", "png"])
124
+ submit_button = st.form_submit_button(label="Upload and Analyze")
125
+
126
+ if submit_button and uploaded_file is not None:
127
+ # Validate photo size
128
+ if not validate_photo_size(uploaded_file):
129
+ st.error("Photo is too large! Please upload a photo smaller than 20MB.")
130
+ else:
131
+ # Display the uploaded image
132
+ image = Image.open(uploaded_file)
133
+ st.image(image, caption="Uploaded Construction Photo", use_column_width=True)
134
+
135
+ # Process the image with AI
136
+ milestone, percentage, error = predict_milestone(image)
137
+
138
+ if error:
139
+ st.error(error)
140
+ # Update Salesforce with failure status
141
+ error_message = update_salesforce_record(
142
+ project_id=project_id,
143
+ milestone=None,
144
+ percentage=0.00,
145
+ image_url=None,
146
+ status="Failure",
147
+ comments=error
148
+ )
149
+ if error_message:
150
+ st.error(error_message)
151
+ else:
152
+ # Upload image to Salesforce
153
+ image_url, upload_error = upload_image_to_salesforce(uploaded_file, project_id)
154
+
155
+ if upload_error:
156
+ st.error(upload_error)
157
+ # Update Salesforce with failure status
158
+ error_message = update_salesforce_record(
159
+ project_id=project_id,
160
+ milestone=milestone,
161
+ percentage=percentage,
162
+ image_url=None,
163
+ status="Failure",
164
+ comments=upload_error
165
+ )
166
+ if error_message:
167
+ st.error(error_message)
168
+ else:
169
+ # Update Salesforce with success
170
+ st.success(f"AI Result: Milestone = {milestone}, Completion = {percentage}%")
171
+ error_message = update_salesforce_record(
172
+ project_id=project_id,
173
+ milestone=milestone,
174
+ percentage=percentage,
175
+ image_url=image_url,
176
+ status="Success",
177
+ comments="Photo processed successfully"
178
+ )
179
+ if error_message:
180
+ st.error(error_message)
181
+ else:
182
+ st.success("Progress saved to Salesforce!")
183
+ else:
184
+ st.info("Please upload a photo to analyze.")
185
+ ```
186
+
187
+ ---
188
+
189
+ #### **2. requirements.txt**
190
+
191
+ This lists all the Python libraries needed to run the app, including `torch` for Hugging Face and `python-dotenv` for the `.env` file.
192
+
193
+ <xaiArtifact artifact_id="9acd8b9b-5c22-4fef-a4cd-74849c7b0075" artifact_version_id="afecf7ad-ee42-4754-a130-3b34b8d1a974" title="requirements.txt" contentType="text/plain">
194
+ streamlit==1.39.0
195
+ transformers==4.44.2
196
+ simple-salesforce==1.12.6
197
+ pillow==10.4.0
198
+ numpy==1.26.4
199
+ requests==2.32.3
200
+ torch==2.4.1
201
+ python-dotenv==1.0.1
202
+ from transformers import pipeline
203
  from simple_salesforce import Salesforceimport streamlit as st
204
  import requests
205
  import os