Dineshpopuri commited on
Commit
84c6769
·
verified ·
1 Parent(s): 16e19c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -9,6 +9,7 @@ from huggingface_hub import InferenceClient
9
  from retry import retry
10
  import time
11
  from datetime import datetime
 
12
 
13
  # Print statement to confirm script initialization
14
  print("Starting Project Closure Readiness Evaluator app...")
@@ -147,21 +148,7 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
147
  # Explicitly convert escalated to lowercase boolean string for Escalation_Flag__c
148
  escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
149
 
150
- # Handle the PDF URL for Client_PDF_Pack_URL__c
151
- pdf_url = ""
152
- if pdf_path:
153
- # In Hugging Face Spaces, the pdf_path is a local filepath (e.g., readiness_report.pdf)
154
- # Gradio may serve this file, but we don't have a direct public URL
155
- # For demonstration, we'll construct a placeholder URL assuming the file is hosted
156
- # In a real production environment, you'd need to upload the PDF to a public location (e.g., S3)
157
- logging.info(f"PDF path provided: {pdf_path}")
158
- # Simulate a URL for Hugging Face Spaces (this is a placeholder and may not work)
159
- # Replace 'your-huggingface-space-url' with the actual Space URL if known
160
- pdf_url = f"https://your-huggingface-space-url.hf.space/file={pdf_path}"
161
- logging.warning("Using a placeholder URL for Client_PDF_Pack_URL__c. In a production environment, upload the PDF to a publicly accessible location (e.g., AWS S3) and use that URL.")
162
- logging.warning(f"Generated placeholder URL: {pdf_url}")
163
-
164
- # Create a record in the custom object Project_Closure_Handover__c
165
  record = {
166
  "Readiness_Score__c": score,
167
  "Checklist_Summary__c": checklist_summary,
@@ -169,7 +156,7 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
169
  "Status__c": status,
170
  "Summarized_Missing_Items__c": summarized_missing,
171
  "Alert_Sent__c": alert_sent,
172
- "Client_PDF_Pack_URL__c": pdf_url, # Use the placeholder URL
173
  "Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
174
  "Escalation_Flag__c": escalation_flag,
175
  "Evaluated_At__c": evaluated_at,
@@ -182,13 +169,36 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
182
  logging.debug(f"Attempting to create Salesforce record in Project_Closure_Handover__c with data: {record}")
183
  result = sf.Project_Closure_Handover__c.create(record)
184
  logging.info(f"Successfully created Salesforce record: {result}")
185
- logging.info(f"Record ID: {result.get('id')}")
186
- return f"Record created successfully. Record ID: {result.get('id')}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  except SalesforceError as e:
188
  logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
189
  logging.error("Possible issues: Object permissions, field-level security, validation rules, or required fields.")
190
  logging.error("Check the following in your Salesforce org:")
191
  logging.error("- Ensure the user has Create permission on Project_Closure_Handover__c.")
 
192
  logging.error("- Verify field-level security for all fields in the record.")
193
  logging.error("- Check for validation rules or required fields that might be failing.")
194
  return f"Salesforce error: {str(e)}"
 
9
  from retry import retry
10
  import time
11
  from datetime import datetime
12
+ import base64
13
 
14
  # Print statement to confirm script initialization
15
  print("Starting Project Closure Readiness Evaluator app...")
 
148
  # Explicitly convert escalated to lowercase boolean string for Escalation_Flag__c
149
  escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
150
 
151
+ # Create the record in Project_Closure_Handover__c without the PDF URL for now
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  record = {
153
  "Readiness_Score__c": score,
154
  "Checklist_Summary__c": checklist_summary,
 
156
  "Status__c": status,
157
  "Summarized_Missing_Items__c": summarized_missing,
158
  "Alert_Sent__c": alert_sent,
159
+ "Client_PDF_Pack_URL__c": "PDF attached to this record", # Placeholder message
160
  "Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
161
  "Escalation_Flag__c": escalation_flag,
162
  "Evaluated_At__c": evaluated_at,
 
169
  logging.debug(f"Attempting to create Salesforce record in Project_Closure_Handover__c with data: {record}")
170
  result = sf.Project_Closure_Handover__c.create(record)
171
  logging.info(f"Successfully created Salesforce record: {result}")
172
+ record_id = result.get('id')
173
+ logging.info(f"Record ID: {record_id}")
174
+
175
+ # Attach the PDF to the record if pdf_path exists
176
+ if pdf_path and os.path.exists(pdf_path):
177
+ logging.info(f"Attaching PDF to Salesforce record ID: {record_id}")
178
+ with open(pdf_path, "rb") as pdf_file:
179
+ pdf_content = pdf_file.read()
180
+ pdf_base64 = base64.b64encode(pdf_content).decode('utf-8')
181
+
182
+ attachment = {
183
+ "ParentId": record_id,
184
+ "Name": "Readiness_Report.pdf",
185
+ "Body": pdf_base64,
186
+ "ContentType": "application/pdf"
187
+ }
188
+
189
+ attachment_result = sf.Attachment.create(attachment)
190
+ logging.info(f"Successfully attached PDF to record: {attachment_result}")
191
+ logging.info(f"Attachment ID: {attachment_result.get('id')}")
192
+ else:
193
+ logging.warning(f"No PDF file found at {pdf_path}. Skipping attachment.")
194
+
195
+ return f"Record created successfully. Record ID: {record_id}. PDF attached to the record."
196
  except SalesforceError as e:
197
  logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
198
  logging.error("Possible issues: Object permissions, field-level security, validation rules, or required fields.")
199
  logging.error("Check the following in your Salesforce org:")
200
  logging.error("- Ensure the user has Create permission on Project_Closure_Handover__c.")
201
+ logging.error("- Ensure the user has permission to create Attachments.")
202
  logging.error("- Verify field-level security for all fields in the record.")
203
  logging.error("- Check for validation rules or required fields that might be failing.")
204
  return f"Salesforce error: {str(e)}"