geethareddy commited on
Commit
9f3752a
Β·
verified Β·
1 Parent(s): c61ab1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -20
app.py CHANGED
@@ -63,10 +63,9 @@ def clean_text_for_pdf(text):
63
  def save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions):
64
  now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
65
  filename = f"report_{supervisor_name}_{project_id}_{now}.pdf"
66
- file_path = f"./reports/{filename}" # Save file in './reports' directory
67
  os.makedirs("reports", exist_ok=True)
68
 
69
- # Generate PDF content
70
  pdf = FPDF()
71
  pdf.add_page()
72
  pdf.set_font("Arial", 'B', 14)
@@ -87,13 +86,13 @@ def save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions
87
  pdf.set_font("Arial", size=12)
88
  for line in suggestions.split("\n"):
89
  pdf.multi_cell(0, 10, clean_text_for_pdf(line))
90
- pdf.output(file_path) # Save the PDF to the file path
91
 
92
- # Copy the file to /tmp/ (Gradio accessible location)
93
- temp_pdf_path = "/tmp/" + os.path.basename(file_path) # Use /tmp/ directory for Gradio access
94
  shutil.copy(file_path, temp_pdf_path)
95
 
96
- return temp_pdf_path # Return the path to Gradio
97
 
98
  # Upload to Salesforce and update record with the generated URL
99
  def upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_path, pdf_name):
@@ -205,25 +204,54 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
205
  if not all([role, supervisor_name, project_id, milestones, reflection]):
206
  return "❗ Please fill all fields.", "", None, ""
207
 
208
- prompt = f"Generate a Daily Checklist and Suggestions based on the inputs."
209
- checklist = "- Perform daily safety inspection"
210
- suggestions = "- Monitor team coordination\n- Review safety protocols with the team"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
- # Save the checklist and suggestions to a PDF and copy to /tmp/ directory for download
213
- pdf_path = save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions)
214
-
215
- # Return the file path for Gradio's download button
216
  return checklist, suggestions, pdf_path, pdf_path
217
 
218
  # Gradio Interface
219
  def create_interface():
220
- with gr.Blocks() as demo:
 
221
  gr.Markdown("## 🧠 AI-Powered Supervisor Assistant")
222
 
223
  with gr.Row():
224
- role = gr.Dropdown(choices=["Supervisor", "Manager"], label="Role")
225
- supervisor_name = gr.Textbox(label="Supervisor Name")
226
- project_id = gr.Textbox(label="Project ID")
227
 
228
  milestones = gr.Textbox(label="Milestones (comma-separated KPIs)")
229
  reflection = gr.Textbox(label="Reflection Log", lines=4)
@@ -232,19 +260,35 @@ def create_interface():
232
  generate = gr.Button("Generate")
233
  clear = gr.Button("Clear")
234
  refresh = gr.Button("πŸ”„ Refresh Roles")
 
235
 
236
  checklist_output = gr.Textbox(label="βœ… Daily Checklist")
237
  suggestions_output = gr.Textbox(label="πŸ’‘ Focus Suggestions")
238
- download_button = gr.File(label="⬇ Download Report") # For downloading the generated report
 
 
 
 
 
 
 
 
 
239
 
240
- generate.click(fn=generate_outputs,
241
  inputs=[role, supervisor_name, project_id, milestones, reflection],
242
- outputs=[checklist_output, suggestions_output, download_button, download_button])
243
 
244
  clear.click(fn=lambda: ("", "", "", "", ""),
245
  inputs=None,
246
  outputs=[role, supervisor_name, project_id, milestones, reflection])
247
 
 
 
 
 
 
 
248
  return demo
249
 
250
  if __name__ == "__main__":
 
63
  def save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions):
64
  now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
65
  filename = f"report_{supervisor_name}_{project_id}_{now}.pdf"
66
+ file_path = f"./reports/{filename}"
67
  os.makedirs("reports", exist_ok=True)
68
 
 
69
  pdf = FPDF()
70
  pdf.add_page()
71
  pdf.set_font("Arial", 'B', 14)
 
86
  pdf.set_font("Arial", size=12)
87
  for line in suggestions.split("\n"):
88
  pdf.multi_cell(0, 10, clean_text_for_pdf(line))
89
+ pdf.output(file_path)
90
 
91
+ # Copy the file to a temporary directory for Gradio to access
92
+ temp_pdf_path = "/tmp/" + os.path.basename(file_path) # Use /tmp/ or current directory for Gradio
93
  shutil.copy(file_path, temp_pdf_path)
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):
 
204
  if not all([role, supervisor_name, project_id, milestones, reflection]):
205
  return "❗ Please fill all fields.", "", None, ""
206
 
207
+ prompt = PROMPT_TEMPLATE.format(role=role, project_id=project_id, milestones=milestones, reflection=reflection)
208
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
209
+ try:
210
+ with torch.no_grad():
211
+ outputs = model.generate(
212
+ inputs['input_ids'],
213
+ max_new_tokens=150,
214
+ no_repeat_ngram_size=2,
215
+ do_sample=True,
216
+ top_p=0.9,
217
+ temperature=0.7,
218
+ pad_token_id=tokenizer.pad_token_id
219
+ )
220
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
221
+ except Exception as e:
222
+ print(f"⚠️ Generation error: {e}")
223
+ return "", "", None, ""
224
+
225
+ def extract_between(text, start, end):
226
+ s = text.find(start)
227
+ e = text.find(end, s) if end else len(text)
228
+ return text[s + len(start):e].strip() if s != -1 else ""
229
+
230
+ checklist = extract_between(result, "Checklist:\n", "Suggestions:")
231
+ suggestions = extract_between(result, "Suggestions:\n", None)
232
+
233
+ if not checklist.strip():
234
+ checklist = "- Perform daily safety inspection"
235
+ if not suggestions.strip():
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
+ if pdf_url:
241
+ suggestions += f"\n\nπŸ”— [Download PDF Report]({pdf_url})"
242
 
 
 
 
 
243
  return checklist, suggestions, pdf_path, pdf_path
244
 
245
  # Gradio Interface
246
  def create_interface():
247
+ roles = get_roles_from_salesforce()
248
+ with gr.Blocks(theme="soft") as demo:
249
  gr.Markdown("## 🧠 AI-Powered Supervisor Assistant")
250
 
251
  with gr.Row():
252
+ role = gr.Dropdown(choices=roles, label="Role")
253
+ supervisor_name = gr.Dropdown(choices=[], label="Supervisor Name")
254
+ project_id = gr.Textbox(label="Project ID", interactive=False)
255
 
256
  milestones = gr.Textbox(label="Milestones (comma-separated KPIs)")
257
  reflection = gr.Textbox(label="Reflection Log", lines=4)
 
260
  generate = gr.Button("Generate")
261
  clear = gr.Button("Clear")
262
  refresh = gr.Button("πŸ”„ Refresh Roles")
263
+ dashboard_btn = gr.Button("Dashboard")
264
 
265
  checklist_output = gr.Textbox(label="βœ… Daily Checklist")
266
  suggestions_output = gr.Textbox(label="πŸ’‘ Focus Suggestions")
267
+ download_button = gr.File(label="⬇ Download Report")
268
+ pdf_link = gr.HTML()
269
+ dashboard_link = gr.HTML()
270
+
271
+ role.change(fn=lambda r: gr.update(choices=get_supervisor_name_by_role(r)), inputs=role, outputs=supervisor_name)
272
+ supervisor_name.change(fn=get_projects_for_supervisor, inputs=supervisor_name, outputs=project_id)
273
+
274
+ def handle_generate(role, supervisor_name, project_id, milestones, reflection):
275
+ checklist, suggestions, pdf_path, _ = generate_outputs(role, supervisor_name, project_id, milestones, reflection)
276
+ return checklist, suggestions, pdf_path, pdf_path
277
 
278
+ generate.click(fn=handle_generate,
279
  inputs=[role, supervisor_name, project_id, milestones, reflection],
280
+ outputs=[checklist_output, suggestions_output, download_button, pdf_link])
281
 
282
  clear.click(fn=lambda: ("", "", "", "", ""),
283
  inputs=None,
284
  outputs=[role, supervisor_name, project_id, milestones, reflection])
285
 
286
+ refresh.click(fn=lambda: gr.update(choices=get_roles_from_salesforce()), outputs=role)
287
+
288
+ dashboard_btn.click(fn=open_dashboard,
289
+ inputs=[role, supervisor_name, project_id],
290
+ outputs=dashboard_link)
291
+
292
  return demo
293
 
294
  if __name__ == "__main__":