geethareddy commited on
Commit
e2e5bfd
Β·
verified Β·
1 Parent(s): fa0eabe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -29,7 +29,6 @@ if tokenizer.pad_token is None:
29
  tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
30
  model.config.pad_token_id = tokenizer.pad_token_id
31
 
32
- # Prompt template
33
  PROMPT_TEMPLATE = """You are an AI assistant for construction supervisors. Given the role, project, milestones, and a reflection log, generate:
34
 
35
  1. A Daily Checklist with clear and concise tasks based on the role and milestones.
@@ -98,29 +97,23 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
98
  security_token=os.getenv('SF_SECURITY_TOKEN'),
99
  domain=os.getenv('SF_DOMAIN', 'login')
100
  )
101
-
102
  with open(pdf_path, "rb") as f:
103
  encoded = base64.b64encode(f.read()).decode()
104
-
105
  content = sf.ContentVersion.create({
106
  'Title': pdf_name,
107
  'PathOnClient': pdf_name,
108
  'VersionData': encoded
109
  })
110
-
111
  content_id = content['id']
112
  download_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{content_id}"
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({
125
  'Project_ID__c': project_id_sf,
126
  'Supervisor_ID__c': supervisor_id,
@@ -128,10 +121,8 @@ def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_pa
128
  'Suggested_Tips__c': suggestions,
129
  'Download_Link__c': download_url
130
  })
131
-
132
  return download_url
133
-
134
- except Exception as e:
135
  return ""
136
 
137
  def get_roles_from_salesforce():
@@ -185,7 +176,6 @@ def get_dashboard_data_from_salesforce(supervisor_name, project_id):
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
@@ -196,7 +186,6 @@ def get_dashboard_data_from_salesforce(supervisor_name, project_id):
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'),
@@ -210,7 +199,6 @@ def get_dashboard_data_from_salesforce(supervisor_name, project_id):
210
  def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
211
  if not all([role, supervisor_name, project_id, milestones, reflection]):
212
  return "❗ Please fill all fields.", "", None, ""
213
-
214
  prompt = PROMPT_TEMPLATE.format(role=role, project_id=project_id, milestones=milestones, reflection=reflection)
215
  inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
216
  try:
@@ -291,23 +279,34 @@ def create_interface():
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()
 
29
  tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
30
  model.config.pad_token_id = tokenizer.pad_token_id
31
 
 
32
  PROMPT_TEMPLATE = """You are an AI assistant for construction supervisors. Given the role, project, milestones, and a reflection log, generate:
33
 
34
  1. A Daily Checklist with clear and concise tasks based on the role and milestones.
 
97
  security_token=os.getenv('SF_SECURITY_TOKEN'),
98
  domain=os.getenv('SF_DOMAIN', 'login')
99
  )
 
100
  with open(pdf_path, "rb") as f:
101
  encoded = base64.b64encode(f.read()).decode()
 
102
  content = sf.ContentVersion.create({
103
  'Title': pdf_name,
104
  'PathOnClient': pdf_name,
105
  'VersionData': encoded
106
  })
 
107
  content_id = content['id']
108
  download_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{content_id}"
 
109
  query = sf.query(f"SELECT Id FROM Supervisor__c WHERE Name = '{supervisor_name}' LIMIT 1")
110
  if query['totalSize'] == 0:
111
  return ""
112
  supervisor_id = query['records'][0]['Id']
 
113
  project_query = sf.query(f"SELECT Id FROM Project__c WHERE Name = '{project_id}' LIMIT 1")
114
  if project_query['totalSize'] == 0:
115
  return ""
116
  project_id_sf = project_query['records'][0]['Id']
 
117
  sf.Supervisor_AI_Coaching__c.create({
118
  'Project_ID__c': project_id_sf,
119
  'Supervisor_ID__c': supervisor_id,
 
121
  'Suggested_Tips__c': suggestions,
122
  'Download_Link__c': download_url
123
  })
 
124
  return download_url
125
+ except:
 
126
  return ""
127
 
128
  def get_roles_from_salesforce():
 
176
  security_token=os.getenv('SF_SECURITY_TOKEN'),
177
  domain=os.getenv('SF_DOMAIN', 'login')
178
  )
 
179
  query = f"""
180
  SELECT Daily_Checklist__c, Suggested_Tips__c, Download_Link__c, CreatedDate
181
  FROM Supervisor_AI_Coaching__c
 
186
  result = sf.query(query)
187
  if result['totalSize'] == 0:
188
  return "No dashboard data found.", "", "", ""
 
189
  record = result['records'][0]
190
  return (
191
  record.get('Daily_Checklist__c', 'N/A'),
 
199
  def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
200
  if not all([role, supervisor_name, project_id, milestones, reflection]):
201
  return "❗ Please fill all fields.", "", None, ""
 
202
  prompt = PROMPT_TEMPLATE.format(role=role, project_id=project_id, milestones=milestones, reflection=reflection)
203
  inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
204
  try:
 
279
  refresh.click(fn=lambda: gr.update(choices=get_roles_from_salesforce()), outputs=role)
280
 
281
  with gr.Tab("πŸ“Š Supervisor Dashboard"):
282
+ dash_supervisor = gr.Textbox(label="Supervisor Name", placeholder="e.g., SUP-056")
283
+ dash_project = gr.Textbox(label="Project ID", placeholder="e.g., PROJ-078")
284
+ load_dash = gr.Button("πŸ“₯ Load Dashboard")
285
+ dash_output = gr.HTML()
 
 
 
 
 
 
 
286
 
287
+ def show_dashboard_html(sup_name, proj_id):
288
+ checklist, suggestions, link, date = get_dashboard_data_from_salesforce(sup_name, proj_id)
289
+ link_html = f'<a href="{link}" target="_blank">Download PDF</a>' if link else "No report available"
290
+ html_card = f"""
291
+ <div style='border:1px solid #ccc; border-radius:10px; padding:20px; background-color:#f9f9f9'>
292
+ <h3>πŸ“‹ <u>Dashboard Summary</u></h3>
293
+ <p><b>Supervisor:</b> {sup_name}</p>
294
+ <p><b>Project ID:</b> {proj_id}</p>
295
+ <p><b>πŸ—“οΈ Last Updated:</b> {date}</p>
296
+ <hr>
297
+ <h4>βœ… <u>Daily Checklist</u></h4>
298
+ <pre style='background:#eef; padding:10px; border-radius:5px;'>{checklist}</pre>
299
+ <h4>πŸ’‘ <u>Focus Suggestions</u></h4>
300
+ <pre style='background:#ffe; padding:10px; border-radius:5px;'>{suggestions}</pre>
301
+ <p>{link_html}</p>
302
+ </div>
303
+ """
304
+ return html_card
305
+
306
+ load_dash.click(fn=show_dashboard_html, inputs=[dash_supervisor, dash_project], outputs=dash_output)
307
 
308
  return demo
309
 
310
  if __name__ == "__main__":
311
  app = create_interface()
312
+ app.launch()