sanjaybhargavneela1929 commited on
Commit
14d9397
Β·
verified Β·
1 Parent(s): 4769ceb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -6,10 +6,10 @@ from simple_salesforce import Salesforce
6
  from dotenv import load_dotenv
7
  import base64
8
 
9
- # Load environment variables
10
  load_dotenv()
11
 
12
- # Get Salesforce credentials and config
13
  SF_USERNAME = os.getenv("SF_USERNAME")
14
  SF_PASSWORD = os.getenv("SF_PASSWORD")
15
  SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
@@ -19,7 +19,7 @@ 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
 
22
- # Validate required values
23
  required = ["SF_USERNAME", "SF_PASSWORD", "SF_SECURITY_TOKEN", "SF_RECORD_ID"]
24
  missing = [var for var in required if not os.getenv(var)]
25
  if missing:
@@ -36,45 +36,45 @@ sf = Salesforce(
36
  domain=domain
37
  )
38
 
39
- # Load Hugging Face model (demo model)
40
  classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
41
 
42
- # Function to process PDF
43
  def process_resume(file):
44
  try:
45
  record_id = SF_RECORD_ID
46
 
47
  # Extract text from PDF
48
  with pdfplumber.open(file.name) as pdf:
49
- text = "\n".join([page.extract_text() or "" for page in pdf.pages])
50
 
51
- if not text.strip():
52
- return "❌ No text found in uploaded PDF."
53
 
54
- # Run AI model
55
- result = classifier(text[:1000])
56
  label = result[0]['label']
57
  score = round(float(result[0]['score']) * 100, 2)
58
  summary = f"Predicted Label: {label}\nSuitability Score: {score:.2f}"
59
 
60
- # Encode PDF to base64
61
  with open(file.name, "rb") as f:
62
  encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
63
 
64
- # Upload as ContentVersion
65
- version_result = sf.ContentVersion.create({
66
  "Title": "Resume",
67
  "PathOnClient": file.name,
68
  "VersionData": encoded_pdf
69
  })
70
 
71
- version_id = version_result["id"]
72
-
73
- # Get ContentDocumentId
74
- query_result = sf.query(f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{version_id}'")
 
75
  content_doc_id = query_result["records"][0]["ContentDocumentId"]
76
 
77
- # Link file to record
78
  sf.ContentDocumentLink.create({
79
  "ContentDocumentId": content_doc_id,
80
  "LinkedEntityId": record_id,
@@ -82,25 +82,25 @@ def process_resume(file):
82
  "Visibility": "AllUsers"
83
  })
84
 
85
- # Construct download link
86
- download_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/document/download/{content_doc_id}"
87
 
88
- # Update record with score + link
89
  sf.__getattr__(SF_OBJECT_NAME).update(record_id, {
90
  SF_SCORE_FIELD: score,
91
- SF_LINK_FIELD: download_url
92
  })
93
 
94
- return f"{summary}\n\nβœ… Score and file uploaded to Salesforce.\nπŸ“Ž [Download Resume]({download_url})"
95
 
96
  except Exception as e:
97
  return f"❌ Error: {str(e)}"
98
 
99
- # Gradio UI
100
  gr.Interface(
101
  fn=process_resume,
102
- inputs=gr.File(label="Upload Resume PDF", file_types=[".pdf"]),
103
  outputs="text",
104
  title="LIC Resume AI Scorer",
105
- description="Upload a resume PDF. It will be scored and stored in Salesforce."
106
- ).launch(share=False)
 
6
  from dotenv import load_dotenv
7
  import base64
8
 
9
+ # Load environment variables from .env
10
  load_dotenv()
11
 
12
+ # Salesforce credentials
13
  SF_USERNAME = os.getenv("SF_USERNAME")
14
  SF_PASSWORD = os.getenv("SF_PASSWORD")
15
  SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
 
19
  SF_LINK_FIELD = os.getenv("SF_RESUME_FIELD_LINK", "Resume_File_Link__c")
20
  SF_RECORD_ID = os.getenv("SF_RECORD_ID")
21
 
22
+ # Validate required credentials
23
  required = ["SF_USERNAME", "SF_PASSWORD", "SF_SECURITY_TOKEN", "SF_RECORD_ID"]
24
  missing = [var for var in required if not os.getenv(var)]
25
  if missing:
 
36
  domain=domain
37
  )
38
 
39
+ # Load Hugging Face model
40
  classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
41
 
 
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."
52
 
53
+ # Call Hugging Face model
54
+ result = classifier(extracted_text[:1000])
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({
65
  "Title": "Resume",
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,
 
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
 
96
  except Exception as e:
97
  return f"❌ Error: {str(e)}"
98
 
99
+ # Gradio Interface
100
  gr.Interface(
101
  fn=process_resume,
102
+ inputs=gr.File(label="Upload Resume (PDF)", file_types=[".pdf"]),
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)