File size: 5,177 Bytes
568607d
16edeae
 
 
 
7f9aeaa
16edeae
7f9aeaa
14d9397
7f9aeaa
2086a2f
14d9397
16edeae
 
 
af38c84
16edeae
f8912a5
16edeae
 
14d9397
f8912a5
16edeae
 
af38c84
 
 
 
16edeae
 
72911c7
 
 
 
 
 
 
 
 
 
 
7f9aeaa
14d9397
af38c84
2163f2d
16edeae
e79685f
16edeae
af38c84
14d9397
72911c7
7f9aeaa
14d9397
 
7f9aeaa
14d9397
 
af38c84
 
 
72911c7
7f9aeaa
14d9397
af38c84
 
72911c7
7f9aeaa
14d9397
 
af38c84
 
 
 
72911c7
 
16edeae
14d9397
 
 
 
72911c7
 
 
af38c84
72911c7
16edeae
f8912a5
 
 
 
 
 
 
 
 
 
 
 
 
72911c7
 
 
 
 
 
 
 
 
 
 
16edeae
14d9397
 
72911c7
16edeae
f8912a5
 
 
 
 
 
 
 
 
af38c84
f8912a5
af38c84
4535ae2
af38c84
2086a2f
14d9397
16edeae
 
14d9397
16edeae
 
f8912a5
72911c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import pdfplumber
import gradio as gr
from transformers import pipeline
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import base64

# Load environment variables from .env
load_dotenv()

# Salesforce credentials
SF_USERNAME = os.getenv("SF_USERNAME")
SF_PASSWORD = os.getenv("SF_PASSWORD")
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
SF_LOGIN_URL = os.getenv("SF_LOGIN_URL", "https://login.salesforce.com")
SF_OBJECT_NAME = os.getenv("SF_OBJECT_NAME", "Agent_Prospect__c")
SF_SCORE_FIELD = os.getenv("SF_SCORE_FIELD", "Suitability_Score__c")
SF_LINK_FIELD = os.getenv("SF_RESUME_FIELD_LINK", "Resume_File_Link__c")

# Validate required credentials
required = ["SF_USERNAME", "SF_PASSWORD", "SF_SECURITY_TOKEN"]
missing = [var for var in required if not os.getenv(var)]
if missing:
    raise ValueError(f"Missing required .env variables: {', '.join(missing)}")

# Determine domain
domain = "login" if "login" in SF_LOGIN_URL else "test"

# Connect to Salesforce
try:
    sf = Salesforce(
        username=SF_USERNAME,
        password=SF_PASSWORD,
        security_token=SF_SECURITY_TOKEN,
        domain=domain,
        version="59.0"
    )
    print("Salesforce connection successful.")
except Exception as e:
    raise ValueError(f"Failed to connect to Salesforce: {str(e)}")

# Load Hugging Face model
classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")

def process_resume(file):
    try:
        # Extract text from PDF
        with pdfplumber.open(file.name) as pdf:
            extracted_text = "\n".join([page.extract_text() or "" for page in pdf.pages])
        print(f"Extracted text length: {len(extracted_text)} characters")

        if not extracted_text.strip():
            return "❌ No extractable text found in the PDF."

        # Call Hugging Face model
        result = classifier(extracted_text[:1000])
        label = result[0]['label']
        score = round(float(result[0]['score']) * 100, 2)
        summary = f"Predicted Label: {label}\nSuitability Score: {score:.2f}"
        print(f"Classifier result: {summary}")

        # Encode PDF in base64
        with open(file.name, "rb") as f:
            encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
        print(f"Encoded PDF size: {len(encoded_pdf)}")

        # Upload file as ContentVersion
        content_result = sf.ContentVersion.create({
            "Title": "Resume",
            "PathOnClient": file.name,
            "VersionData": encoded_pdf
        })
        version_id = content_result.get("id")
        print(f"ContentVersion created: {version_id}")

        # Get ContentDocumentId from ContentVersion
        query_result = sf.query(
            f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{version_id}'"
        )
        print(f"Query result: {query_result}")
        if not query_result["records"]:
            return "❌ Failed to retrieve ContentDocumentId."
        content_doc_id = query_result["records"][0]["ContentDocumentId"]
        print(f"ContentDocumentId: {content_doc_id}")

        # Create a new Agent_Prospect__c record
        try:
            record_result = sf.__getattr__(SF_OBJECT_NAME).create({
                SF_SCORE_FIELD: score,
                SF_LINK_FIELD: ""  # Placeholder; will update with download link
            })
            record_id = record_result.get("id")
            print(f"New Agent_Prospect__c record created: {record_id}")
        except Exception as e:
            print(f"Error creating Agent_Prospect__c record: {str(e)}")
            raise

        # Link file to the new Salesforce record
        try:
            sf.ContentDocumentLink.create({
                "ContentDocumentId": content_doc_id,
                "LinkedEntityId": record_id,
                "ShareType": "V",
                "Visibility": "AllUsers"
            })
            print("ContentDocumentLink created successfully.")
        except Exception as e:
            print(f"ContentDocumentLink Error: {str(e)}")
            raise

        # Create download link
        download_link = f"https://{sf.sf_instance}/sfc/servlet.shepherd/document/download/{content_doc_id}"
        print(f"Download link: {download_link}")

        # Update the record with the download link
        try:
            sf.__getattr__(SF_OBJECT_NAME).update(record_id, {
                SF_LINK_FIELD: download_link
            })
            print("Salesforce record updated with download link.")
        except Exception as e:
            print(f"Error updating record with download link: {str(e)}")
            raise

        return f"{summary}\n\nβœ… New record created and resume uploaded to Salesforce.\nπŸ“Ž [Download Resume]({download_link})\nRecord ID: {record_id}"

    except Exception as e:
        return f"❌ Error: {str(e)}"

# Gradio Interface
gr.Interface(
    fn=process_resume,
    inputs=gr.File(label="Upload Resume (PDF)", file_types=[".pdf"]),
    outputs="text",
    title="LIC Resume AI Scorer",
    description="Upload a resume PDF. A new record will be created in Salesforce with the score and resume."
).launch(share=False)