Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -94,8 +94,8 @@ def save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions
|
|
94 |
|
95 |
return temp_pdf_path, filename
|
96 |
|
97 |
-
#
|
98 |
-
def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_path, pdf_name
|
99 |
try:
|
100 |
sf = Salesforce(
|
101 |
username=os.getenv('SF_USERNAME'),
|
@@ -103,7 +103,7 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
|
|
103 |
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
104 |
domain=os.getenv('SF_DOMAIN', 'login')
|
105 |
)
|
106 |
-
|
107 |
# Read and encode the file as base64
|
108 |
with open(pdf_path, "rb") as f:
|
109 |
encoded = base64.b64encode(f.read()).decode()
|
@@ -123,10 +123,9 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
|
|
123 |
|
124 |
# Query Salesforce to find the specific Supervisor_AI_Coaching__c record
|
125 |
query = sf.query(f"""
|
126 |
-
SELECT Id
|
127 |
-
FROM Supervisor_AI_Coaching__c
|
128 |
WHERE Project_ID__c = '{project_id}'
|
129 |
-
AND
|
130 |
LIMIT 1
|
131 |
""")
|
132 |
|
@@ -134,21 +133,10 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
|
|
134 |
# Get the ID of the Supervisor_AI_Coaching__c record
|
135 |
coaching_id = query['records'][0]['Id']
|
136 |
|
137 |
-
# Update the Supervisor_AI_Coaching__c record with the
|
138 |
sf.Supervisor_AI_Coaching__c.update(coaching_id, {
|
139 |
-
'Daily_Checklist__c': checklist, # Update Daily_Checklist__c with the generated checklist
|
140 |
-
'Suggested_Tips__c': suggestions, # Update Suggested_Tips__c with the generated suggestions
|
141 |
'Download_Link__c': download_url # Update the Download_Link__c field with the URL
|
142 |
})
|
143 |
-
else:
|
144 |
-
# If record doesn't exist, create a new one
|
145 |
-
sf.Supervisor_AI_Coaching__c.create({
|
146 |
-
'Supervisor_Name__c': supervisor_name,
|
147 |
-
'Project_ID__c': project_id,
|
148 |
-
'Daily_Checklist__c': checklist, # Set the Daily_Checklist__c field
|
149 |
-
'Suggested_Tips__c': suggestions, # Set the Suggested_Tips__c field
|
150 |
-
'Download_Link__c': download_url # Set the Download_Link__c field
|
151 |
-
})
|
152 |
|
153 |
return download_url
|
154 |
|
@@ -156,7 +144,62 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
|
|
156 |
print(f"⚠️ Upload error: {e}")
|
157 |
return ""
|
158 |
|
159 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
161 |
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
162 |
return "❗ Please fill all fields.", "", None, ""
|
@@ -193,7 +236,7 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
|
193 |
suggestions = "- Monitor team coordination\n- Review safety protocols with the team"
|
194 |
|
195 |
pdf_path, pdf_name = save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions)
|
196 |
-
pdf_url = upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_path, pdf_name
|
197 |
|
198 |
if pdf_url:
|
199 |
suggestions += f"\n\n🔗 [Download PDF Report]({pdf_url})"
|
@@ -201,21 +244,6 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
|
201 |
# Return the generated checklist, suggestions, and the path to the PDF
|
202 |
return checklist, suggestions, pdf_path, pdf_name
|
203 |
|
204 |
-
# Salesforce helper function to get roles from Salesforce
|
205 |
-
def get_roles_from_salesforce():
|
206 |
-
try:
|
207 |
-
sf = Salesforce(
|
208 |
-
username=os.getenv('SF_USERNAME'),
|
209 |
-
password=os.getenv('SF_PASSWORD'),
|
210 |
-
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
211 |
-
domain=os.getenv('SF_DOMAIN', 'login')
|
212 |
-
)
|
213 |
-
result = sf.query("SELECT Role__c FROM Supervisor__c WHERE Role__c != NULL")
|
214 |
-
return list(set(record['Role__c'] for record in result['records']))
|
215 |
-
except Exception as e:
|
216 |
-
print(f"⚠️ Error fetching roles: {e}")
|
217 |
-
return []
|
218 |
-
|
219 |
# Gradio Interface
|
220 |
def create_interface():
|
221 |
roles = get_roles_from_salesforce()
|
@@ -268,3 +296,5 @@ def create_interface():
|
|
268 |
if __name__ == "__main__":
|
269 |
app = create_interface()
|
270 |
app.launch()
|
|
|
|
|
|
94 |
|
95 |
return temp_pdf_path, filename
|
96 |
|
97 |
+
# Upload to Salesforce and update record with the generated URL
|
98 |
+
def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_path, pdf_name):
|
99 |
try:
|
100 |
sf = Salesforce(
|
101 |
username=os.getenv('SF_USERNAME'),
|
|
|
103 |
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
104 |
domain=os.getenv('SF_DOMAIN', 'login')
|
105 |
)
|
106 |
+
|
107 |
# Read and encode the file as base64
|
108 |
with open(pdf_path, "rb") as f:
|
109 |
encoded = base64.b64encode(f.read()).decode()
|
|
|
123 |
|
124 |
# Query Salesforce to find the specific Supervisor_AI_Coaching__c record
|
125 |
query = sf.query(f"""
|
126 |
+
SELECT Id FROM Supervisor_AI_Coaching__c
|
|
|
127 |
WHERE Project_ID__c = '{project_id}'
|
128 |
+
AND Name = '{supervisor_name}'
|
129 |
LIMIT 1
|
130 |
""")
|
131 |
|
|
|
133 |
# Get the ID of the Supervisor_AI_Coaching__c record
|
134 |
coaching_id = query['records'][0]['Id']
|
135 |
|
136 |
+
# Update the Supervisor_AI_Coaching__c record with the download URL
|
137 |
sf.Supervisor_AI_Coaching__c.update(coaching_id, {
|
|
|
|
|
138 |
'Download_Link__c': download_url # Update the Download_Link__c field with the URL
|
139 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
return download_url
|
142 |
|
|
|
144 |
print(f"⚠️ Upload error: {e}")
|
145 |
return ""
|
146 |
|
147 |
+
# Salesforce helpers
|
148 |
+
def get_roles_from_salesforce():
|
149 |
+
try:
|
150 |
+
sf = Salesforce(
|
151 |
+
username=os.getenv('SF_USERNAME'),
|
152 |
+
password=os.getenv('SF_PASSWORD'),
|
153 |
+
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
154 |
+
domain=os.getenv('SF_DOMAIN', 'login')
|
155 |
+
)
|
156 |
+
result = sf.query("SELECT Role__c FROM Supervisor__c WHERE Role__c != NULL")
|
157 |
+
return list(set(record['Role__c'] for record in result['records']))
|
158 |
+
except Exception as e:
|
159 |
+
print(f"⚠️ Error fetching roles: {e}")
|
160 |
+
return []
|
161 |
+
|
162 |
+
def get_supervisor_name_by_role(role):
|
163 |
+
try:
|
164 |
+
sf = Salesforce(
|
165 |
+
username=os.getenv('SF_USERNAME'),
|
166 |
+
password=os.getenv('SF_PASSWORD'),
|
167 |
+
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
168 |
+
domain=os.getenv('SF_DOMAIN', 'login')
|
169 |
+
)
|
170 |
+
result = sf.query(f"SELECT Name FROM Supervisor__c WHERE Role__c = '{role}'")
|
171 |
+
return [record['Name'] for record in result['records']]
|
172 |
+
except Exception as e:
|
173 |
+
print(f"⚠️ Error fetching names: {e}")
|
174 |
+
return []
|
175 |
+
|
176 |
+
def get_projects_for_supervisor(supervisor_name):
|
177 |
+
try:
|
178 |
+
sf = Salesforce(
|
179 |
+
username=os.getenv('SF_USERNAME'),
|
180 |
+
password=os.getenv('SF_PASSWORD'),
|
181 |
+
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
182 |
+
domain=os.getenv('SF_DOMAIN', 'login')
|
183 |
+
)
|
184 |
+
result = sf.query(f"SELECT Id FROM Supervisor__c WHERE Name = '{supervisor_name}' LIMIT 1")
|
185 |
+
if result['totalSize'] == 0:
|
186 |
+
return ""
|
187 |
+
supervisor_id = result['records'][0]['Id']
|
188 |
+
project_result = sf.query(f"SELECT Name FROM Project__c WHERE Supervisor_ID__c = '{supervisor_id}' LIMIT 1")
|
189 |
+
return project_result['records'][0]['Name'] if project_result['totalSize'] > 0 else ""
|
190 |
+
except Exception as e:
|
191 |
+
print(f"⚠️ Error fetching project: {e}")
|
192 |
+
return ""
|
193 |
+
|
194 |
+
# Generate Salesforce dashboard URL
|
195 |
+
def generate_salesforce_dashboard_url(supervisor_name, project_id):
|
196 |
+
return f"https://aicoachforsitesupervisors-dev-ed--c.develop.vf.force.com/apex/DashboardPage?supervisorName={supervisor_name}&projectId={project_id}"
|
197 |
+
|
198 |
+
def open_dashboard(role, supervisor_name, project_id):
|
199 |
+
url = generate_salesforce_dashboard_url(supervisor_name, project_id)
|
200 |
+
return f'<a href="{url}" target="_blank">Open Salesforce Dashboard</a>'
|
201 |
+
|
202 |
+
# Generate AI output and return the correct file for download
|
203 |
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
204 |
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
205 |
return "❗ Please fill all fields.", "", None, ""
|
|
|
236 |
suggestions = "- Monitor team coordination\n- Review safety protocols with the team"
|
237 |
|
238 |
pdf_path, pdf_name = save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions)
|
239 |
+
pdf_url = upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_path, pdf_name)
|
240 |
|
241 |
if pdf_url:
|
242 |
suggestions += f"\n\n🔗 [Download PDF Report]({pdf_url})"
|
|
|
244 |
# Return the generated checklist, suggestions, and the path to the PDF
|
245 |
return checklist, suggestions, pdf_path, pdf_name
|
246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
# Gradio Interface
|
248 |
def create_interface():
|
249 |
roles = get_roles_from_salesforce()
|
|
|
296 |
if __name__ == "__main__":
|
297 |
app = create_interface()
|
298 |
app.launch()
|
299 |
+
|
300 |
+
|