dschandra commited on
Commit
72911c7
Β·
verified Β·
1 Parent(s): 14d9397

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -15
app.py CHANGED
@@ -15,7 +15,7 @@ SF_PASSWORD = os.getenv("SF_PASSWORD")
15
  SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
16
  SF_LOGIN_URL = os.getenv("SF_LOGIN_URL", "https://login.salesforce.com")
17
  SF_OBJECT_NAME = os.getenv("SF_OBJECT_NAME", "Agent_Prospect__c")
18
- SF_SCORE_FIELD = os.getenv("SF_SCORE_FIELD", "Suitability_Score__c")
19
  SF_LINK_FIELD = os.getenv("SF_RESUME_FIELD_LINK", "Resume_File_Link__c")
20
  SF_RECORD_ID = os.getenv("SF_RECORD_ID")
21
 
@@ -29,12 +29,17 @@ if missing:
29
  domain = "login" if "login" in SF_LOGIN_URL else "test"
30
 
31
  # Connect to Salesforce
32
- sf = Salesforce(
33
- username=SF_USERNAME,
34
- password=SF_PASSWORD,
35
- security_token=SF_SECURITY_TOKEN,
36
- domain=domain
37
- )
 
 
 
 
 
38
 
39
  # Load Hugging Face model
40
  classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
@@ -42,10 +47,12 @@ classifier = pipeline("text-classification", model="nlptown/bert-base-multilingu
42
  def process_resume(file):
43
  try:
44
  record_id = SF_RECORD_ID
 
45
 
46
  # Extract text from PDF
47
  with pdfplumber.open(file.name) as pdf:
48
  extracted_text = "\n".join([page.extract_text() or "" for page in pdf.pages])
 
49
 
50
  if not extracted_text.strip():
51
  return "❌ No extractable text found in the PDF."
@@ -55,10 +62,12 @@ def process_resume(file):
55
  label = result[0]['label']
56
  score = round(float(result[0]['score']) * 100, 2)
57
  summary = f"Predicted Label: {label}\nSuitability Score: {score:.2f}"
 
58
 
59
  # Encode PDF in base64
60
  with open(file.name, "rb") as f:
61
  encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
 
62
 
63
  # Upload file as ContentVersion
64
  content_result = sf.ContentVersion.create({
@@ -66,30 +75,42 @@ def process_resume(file):
66
  "PathOnClient": file.name,
67
  "VersionData": encoded_pdf
68
  })
 
 
69
 
70
  # Get ContentDocumentId from ContentVersion
71
- version_id = content_result.get("id")
72
  query_result = sf.query(
73
  f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{version_id}'"
74
  )
 
 
 
75
  content_doc_id = query_result["records"][0]["ContentDocumentId"]
 
76
 
77
  # Link file to Salesforce record
78
- sf.ContentDocumentLink.create({
79
- "ContentDocumentId": content_doc_id,
80
- "LinkedEntityId": record_id,
81
- "ShareType": "V",
82
- "Visibility": "AllUsers"
83
- })
 
 
 
 
 
84
 
85
  # Create download link
86
  download_link = f"https://{sf.sf_instance}/sfc/servlet.shepherd/document/download/{content_doc_id}"
 
87
 
88
  # Update record with score and link
89
  sf.__getattr__(SF_OBJECT_NAME).update(record_id, {
90
  SF_SCORE_FIELD: score,
91
  SF_LINK_FIELD: download_link
92
  })
 
93
 
94
  return f"{summary}\n\nβœ… Score and resume uploaded to Salesforce.\nπŸ“Ž [Download Resume]({download_link})"
95
 
@@ -103,4 +124,4 @@ gr.Interface(
103
  outputs="text",
104
  title="LIC Resume AI Scorer",
105
  description="Upload a resume PDF. It will be scored and stored in Salesforce automatically."
106
- ).launch(share=False)
 
15
  SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
16
  SF_LOGIN_URL = os.getenv("SF_LOGIN_URL", "https://login.salesforce.com")
17
  SF_OBJECT_NAME = os.getenv("SF_OBJECT_NAME", "Agent_Prospect__c")
18
+ SF_SCORE_FIELD = os.getenv("SF_SCORE_FIELD", "Suitability_Srode__c")
19
  SF_LINK_FIELD = os.getenv("SF_RESUME_FIELD_LINK", "Resume_File_Link__c")
20
  SF_RECORD_ID = os.getenv("SF_RECORD_ID")
21
 
 
29
  domain = "login" if "login" in SF_LOGIN_URL else "test"
30
 
31
  # Connect to Salesforce
32
+ try:
33
+ sf = Salesforce(
34
+ username=SF_USERNAME,
35
+ password=SF_PASSWORD,
36
+ security_token=SF_SECURITY_TOKEN,
37
+ domain=domain,
38
+ version="59.0"
39
+ )
40
+ print("Salesforce connection successful.")
41
+ except Exception as e:
42
+ raise ValueError(f"Failed to connect to Salesforce: {str(e)}")
43
 
44
  # Load Hugging Face model
45
  classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
 
47
  def process_resume(file):
48
  try:
49
  record_id = SF_RECORD_ID
50
+ print(f"Processing resume for record ID: {record_id}")
51
 
52
  # Extract text from PDF
53
  with pdfplumber.open(file.name) as pdf:
54
  extracted_text = "\n".join([page.extract_text() or "" for page in pdf.pages])
55
+ print(f"Extracted text length: {len(extracted_text)} characters")
56
 
57
  if not extracted_text.strip():
58
  return "❌ No extractable text found in the PDF."
 
62
  label = result[0]['label']
63
  score = round(float(result[0]['score']) * 100, 2)
64
  summary = f"Predicted Label: {label}\nSuitability Score: {score:.2f}"
65
+ print(f"Classifier result: {summary}")
66
 
67
  # Encode PDF in base64
68
  with open(file.name, "rb") as f:
69
  encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
70
+ print(f"Encoded PDF size: {len(encoded_pdf)}")
71
 
72
  # Upload file as ContentVersion
73
  content_result = sf.ContentVersion.create({
 
75
  "PathOnClient": file.name,
76
  "VersionData": encoded_pdf
77
  })
78
+ version_id = content_result.get("id")
79
+ print(f"ContentVersion created: {version_id}")
80
 
81
  # Get ContentDocumentId from ContentVersion
 
82
  query_result = sf.query(
83
  f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{version_id}'"
84
  )
85
+ print(f"Query result: {query_result}")
86
+ if not query_result["records"]:
87
+ return "❌ Failed to retrieve ContentDocumentId."
88
  content_doc_id = query_result["records"][0]["ContentDocumentId"]
89
+ print(f"ContentDocumentId: {content_doc_id}")
90
 
91
  # Link file to Salesforce record
92
+ try:
93
+ sf.ContentDocumentLink.create({
94
+ "ContentDocumentId": content_doc_id,
95
+ "LinkedEntityId": record_id,
96
+ "ShareType": "V",
97
+ "Visibility": "AllUsers"
98
+ })
99
+ print("ContentDocumentLink created successfully.")
100
+ except Exception as e:
101
+ print(f"ContentDocumentLink Error: {str(e)}")
102
+ raise
103
 
104
  # Create download link
105
  download_link = f"https://{sf.sf_instance}/sfc/servlet.shepherd/document/download/{content_doc_id}"
106
+ print(f"Download link: {download_link}")
107
 
108
  # Update record with score and link
109
  sf.__getattr__(SF_OBJECT_NAME).update(record_id, {
110
  SF_SCORE_FIELD: score,
111
  SF_LINK_FIELD: download_link
112
  })
113
+ print("Salesforce record updated successfully.")
114
 
115
  return f"{summary}\n\nβœ… Score and resume uploaded to Salesforce.\nπŸ“Ž [Download Resume]({download_link})"
116
 
 
124
  outputs="text",
125
  title="LIC Resume AI Scorer",
126
  description="Upload a resume PDF. It will be scored and stored in Salesforce automatically."
127
+ ).launch(share=False)