Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -113,16 +113,12 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
|
|
113 |
|
114 |
query = sf.query(f"SELECT Id FROM Supervisor__c WHERE Name = '{supervisor_name}' LIMIT 1")
|
115 |
if query['totalSize'] == 0:
|
116 |
-
print("⚠️ Supervisor not found!")
|
117 |
return ""
|
118 |
-
|
119 |
supervisor_id = query['records'][0]['Id']
|
120 |
|
121 |
project_query = sf.query(f"SELECT Id FROM Project__c WHERE Name = '{project_id}' LIMIT 1")
|
122 |
if project_query['totalSize'] == 0:
|
123 |
-
print(f"⚠️ Project '{project_id}' not found in Salesforce!")
|
124 |
return ""
|
125 |
-
|
126 |
project_id_sf = project_query['records'][0]['Id']
|
127 |
|
128 |
sf.Supervisor_AI_Coaching__c.create({
|
@@ -136,7 +132,6 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
|
|
136 |
return download_url
|
137 |
|
138 |
except Exception as e:
|
139 |
-
print(f"⚠️ Upload error: {e}")
|
140 |
return ""
|
141 |
|
142 |
def get_roles_from_salesforce():
|
@@ -149,8 +144,7 @@ def get_roles_from_salesforce():
|
|
149 |
)
|
150 |
result = sf.query("SELECT Role__c FROM Supervisor__c WHERE Role__c != NULL")
|
151 |
return list(set(record['Role__c'] for record in result['records']))
|
152 |
-
except
|
153 |
-
print(f"⚠️ Error fetching roles: {e}")
|
154 |
return []
|
155 |
|
156 |
def get_supervisor_name_by_role(role):
|
@@ -163,8 +157,7 @@ def get_supervisor_name_by_role(role):
|
|
163 |
)
|
164 |
result = sf.query(f"SELECT Name FROM Supervisor__c WHERE Role__c = '{role}'")
|
165 |
return [record['Name'] for record in result['records']]
|
166 |
-
except
|
167 |
-
print(f"⚠️ Error fetching names: {e}")
|
168 |
return []
|
169 |
|
170 |
def get_projects_for_supervisor(supervisor_name):
|
@@ -181,16 +174,38 @@ def get_projects_for_supervisor(supervisor_name):
|
|
181 |
supervisor_id = result['records'][0]['Id']
|
182 |
project_result = sf.query(f"SELECT Name FROM Project__c WHERE Supervisor_ID__c = '{supervisor_id}' LIMIT 1")
|
183 |
return project_result['records'][0]['Name'] if project_result['totalSize'] > 0 else ""
|
184 |
-
except
|
185 |
-
print(f"⚠️ Error fetching project: {e}")
|
186 |
return ""
|
187 |
|
188 |
-
def
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
196 |
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
@@ -210,8 +225,7 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
|
210 |
pad_token_id=tokenizer.pad_token_id
|
211 |
)
|
212 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
213 |
-
except
|
214 |
-
print(f"⚠️ Generation error: {e}")
|
215 |
return "", "", None, ""
|
216 |
|
217 |
def extract_between(text, start, end):
|
@@ -237,52 +251,63 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
|
237 |
|
238 |
def create_interface():
|
239 |
roles = get_roles_from_salesforce()
|
240 |
-
with gr.Blocks(theme="soft",css=".footer { display: none; }") as demo:
|
241 |
gr.Markdown("## 🧠 AI-Powered Supervisor Assistant")
|
242 |
|
243 |
-
with gr.
|
244 |
-
|
245 |
-
|
246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
|
248 |
-
|
249 |
-
|
|
|
|
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
clear = gr.Button("Clear")
|
254 |
-
refresh = gr.Button("🔄 Refresh Roles")
|
255 |
-
dashboard_btn = gr.Button("Dashboard")
|
256 |
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
pdf_link = gr.HTML()
|
261 |
-
dashboard_link = gr.HTML()
|
262 |
|
263 |
-
|
264 |
-
|
|
|
265 |
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
|
270 |
-
|
271 |
-
inputs=[role, supervisor_name, project_id, milestones, reflection],
|
272 |
-
outputs=[checklist_output, suggestions_output, download_button, pdf_link])
|
273 |
|
274 |
-
|
275 |
-
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
-
|
|
|
|
|
279 |
|
280 |
-
|
281 |
-
|
282 |
-
outputs=dashboard_link)
|
283 |
|
284 |
return demo
|
285 |
|
286 |
if __name__ == "__main__":
|
287 |
app = create_interface()
|
288 |
-
app.launch()
|
|
|
113 |
|
114 |
query = sf.query(f"SELECT Id FROM Supervisor__c WHERE Name = '{supervisor_name}' LIMIT 1")
|
115 |
if query['totalSize'] == 0:
|
|
|
116 |
return ""
|
|
|
117 |
supervisor_id = query['records'][0]['Id']
|
118 |
|
119 |
project_query = sf.query(f"SELECT Id FROM Project__c WHERE Name = '{project_id}' LIMIT 1")
|
120 |
if project_query['totalSize'] == 0:
|
|
|
121 |
return ""
|
|
|
122 |
project_id_sf = project_query['records'][0]['Id']
|
123 |
|
124 |
sf.Supervisor_AI_Coaching__c.create({
|
|
|
132 |
return download_url
|
133 |
|
134 |
except Exception as e:
|
|
|
135 |
return ""
|
136 |
|
137 |
def get_roles_from_salesforce():
|
|
|
144 |
)
|
145 |
result = sf.query("SELECT Role__c FROM Supervisor__c WHERE Role__c != NULL")
|
146 |
return list(set(record['Role__c'] for record in result['records']))
|
147 |
+
except:
|
|
|
148 |
return []
|
149 |
|
150 |
def get_supervisor_name_by_role(role):
|
|
|
157 |
)
|
158 |
result = sf.query(f"SELECT Name FROM Supervisor__c WHERE Role__c = '{role}'")
|
159 |
return [record['Name'] for record in result['records']]
|
160 |
+
except:
|
|
|
161 |
return []
|
162 |
|
163 |
def get_projects_for_supervisor(supervisor_name):
|
|
|
174 |
supervisor_id = result['records'][0]['Id']
|
175 |
project_result = sf.query(f"SELECT Name FROM Project__c WHERE Supervisor_ID__c = '{supervisor_id}' LIMIT 1")
|
176 |
return project_result['records'][0]['Name'] if project_result['totalSize'] > 0 else ""
|
177 |
+
except:
|
|
|
178 |
return ""
|
179 |
|
180 |
+
def get_dashboard_data_from_salesforce(supervisor_name, project_id):
|
181 |
+
try:
|
182 |
+
sf = Salesforce(
|
183 |
+
username=os.getenv('SF_USERNAME'),
|
184 |
+
password=os.getenv('SF_PASSWORD'),
|
185 |
+
security_token=os.getenv('SF_SECURITY_TOKEN'),
|
186 |
+
domain=os.getenv('SF_DOMAIN', 'login')
|
187 |
+
)
|
188 |
|
189 |
+
query = f"""
|
190 |
+
SELECT Daily_Checklist__c, Suggested_Tips__c, Download_Link__c, CreatedDate
|
191 |
+
FROM Supervisor_AI_Coaching__c
|
192 |
+
WHERE Supervisor_ID__r.Name = '{supervisor_name}' AND Project_ID__r.Name = '{project_id}'
|
193 |
+
ORDER BY CreatedDate DESC
|
194 |
+
LIMIT 1
|
195 |
+
"""
|
196 |
+
result = sf.query(query)
|
197 |
+
if result['totalSize'] == 0:
|
198 |
+
return "No dashboard data found.", "", "", ""
|
199 |
+
|
200 |
+
record = result['records'][0]
|
201 |
+
return (
|
202 |
+
record.get('Daily_Checklist__c', 'N/A'),
|
203 |
+
record.get('Suggested_Tips__c', 'N/A'),
|
204 |
+
record.get('Download_Link__c', ''),
|
205 |
+
record.get('CreatedDate', 'Unknown')
|
206 |
+
)
|
207 |
+
except Exception as e:
|
208 |
+
return f"Error loading dashboard: {e}", "", "", ""
|
209 |
|
210 |
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
211 |
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
|
|
225 |
pad_token_id=tokenizer.pad_token_id
|
226 |
)
|
227 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
228 |
+
except:
|
|
|
229 |
return "", "", None, ""
|
230 |
|
231 |
def extract_between(text, start, end):
|
|
|
251 |
|
252 |
def create_interface():
|
253 |
roles = get_roles_from_salesforce()
|
254 |
+
with gr.Blocks(theme="soft", css=".footer { display: none; }") as demo:
|
255 |
gr.Markdown("## 🧠 AI-Powered Supervisor Assistant")
|
256 |
|
257 |
+
with gr.Tab("✏️ Generate Checklist"):
|
258 |
+
with gr.Row():
|
259 |
+
role = gr.Dropdown(choices=roles, label="Role")
|
260 |
+
supervisor_name = gr.Dropdown(choices=[], label="Supervisor Name")
|
261 |
+
project_id = gr.Textbox(label="Project ID", interactive=False)
|
262 |
+
|
263 |
+
milestones = gr.Textbox(label="Milestones (comma-separated KPIs)")
|
264 |
+
reflection = gr.Textbox(label="Reflection Log", lines=4)
|
265 |
+
|
266 |
+
with gr.Row():
|
267 |
+
generate = gr.Button("Generate")
|
268 |
+
clear = gr.Button("Clear")
|
269 |
+
refresh = gr.Button("🔄 Refresh Roles")
|
270 |
|
271 |
+
checklist_output = gr.Textbox(label="✅ Daily Checklist")
|
272 |
+
suggestions_output = gr.Textbox(label="💡 Focus Suggestions")
|
273 |
+
download_button = gr.File(label="⬇ Download Report")
|
274 |
+
pdf_link = gr.HTML()
|
275 |
|
276 |
+
role.change(fn=lambda r: gr.update(choices=get_supervisor_name_by_role(r)), inputs=role, outputs=supervisor_name)
|
277 |
+
supervisor_name.change(fn=get_projects_for_supervisor, inputs=supervisor_name, outputs=project_id)
|
|
|
|
|
|
|
278 |
|
279 |
+
def handle_generate(role, supervisor_name, project_id, milestones, reflection):
|
280 |
+
checklist, suggestions, pdf_path, pdf_name = generate_outputs(role, supervisor_name, project_id, milestones, reflection)
|
281 |
+
return checklist, suggestions, pdf_path, pdf_name
|
|
|
|
|
282 |
|
283 |
+
generate.click(fn=handle_generate,
|
284 |
+
inputs=[role, supervisor_name, project_id, milestones, reflection],
|
285 |
+
outputs=[checklist_output, suggestions_output, download_button, pdf_link])
|
286 |
|
287 |
+
clear.click(fn=lambda: ("", "", "", "", ""),
|
288 |
+
inputs=None,
|
289 |
+
outputs=[role, supervisor_name, project_id, milestones, reflection])
|
290 |
|
291 |
+
refresh.click(fn=lambda: gr.update(choices=get_roles_from_salesforce()), outputs=role)
|
|
|
|
|
292 |
|
293 |
+
with gr.Tab("📊 Supervisor Dashboard"):
|
294 |
+
dash_supervisor = gr.Textbox(label="Supervisor Name")
|
295 |
+
dash_project = gr.Textbox(label="Project ID")
|
296 |
+
load_dash = gr.Button("Load Dashboard")
|
297 |
+
dash_checklist = gr.Textbox(label="✅ Last Checklist", lines=6)
|
298 |
+
dash_suggestions = gr.Textbox(label="💡 Suggestions", lines=4)
|
299 |
+
dash_pdf = gr.HTML(label="📄 PDF Link")
|
300 |
+
dash_date = gr.Textbox(label="🕒 Last Updated", interactive=False)
|
301 |
|
302 |
+
def show_dashboard(sup_name, proj_id):
|
303 |
+
checklist, suggestions, link, date = get_dashboard_data_from_salesforce(sup_name, proj_id)
|
304 |
+
return checklist, suggestions, f'<a href="{link}" target="_blank">Download PDF</a>' if link else "No link", date
|
305 |
|
306 |
+
load_dash.click(fn=show_dashboard, inputs=[dash_supervisor, dash_project],
|
307 |
+
outputs=[dash_checklist, dash_suggestions, dash_pdf, dash_date])
|
|
|
308 |
|
309 |
return demo
|
310 |
|
311 |
if __name__ == "__main__":
|
312 |
app = create_interface()
|
313 |
+
app.launch()
|