File size: 3,222 Bytes
7f9aeaa
 
8f56da4
7f9aeaa
 
 
 
 
 
 
 
2086a2f
7f9aeaa
 
 
 
 
 
 
 
 
 
 
2086a2f
 
7f9aeaa
 
 
 
 
 
 
 
 
e79685f
7f9aeaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e79685f
 
2086a2f
7f9aeaa
 
 
8f56da4
7f9aeaa
 
8f56da4
 
7f9aeaa
 
 
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
import os
import pdfplumber
import gradio as gr
from transformers import pipeline
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import requests
import base64

# Load environment variables
load_dotenv()

# Salesforce credentials from .env
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")
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")
SF_RECORD_ID = os.getenv("SF_RECORD_ID")

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

# Connect to Salesforce
sf = Salesforce(
    username=SF_USERNAME,
    password=SF_PASSWORD,
    security_token=SF_SECURITY_TOKEN,
    domain="login" if "login" in SF_LOGIN_URL else "test"
)

def process_resume(file, record_id):
    try:
        # Extract text from PDF
        with pdfplumber.open(file.name) as pdf:
            text = ""
            for page in pdf.pages:
                text += page.extract_text() or ""

        if not text.strip():
            return "❌ Could not extract text from PDF."

        # Call HF model
        result = classifier(text[:1000])
        score_text = result[0]['label']
        score_val = float(result[0]['score']) * 100
        summary = f"Predicted Label: {score_text}\nSuitability Score: {score_val:.2f}"

        # Convert PDF to base64
        with open(file.name, "rb") as f:
            pdf_content = f.read()
            pdf_base64 = base64.b64encode(pdf_content).decode("utf-8")

        # Upload PDF to Salesforce using ContentVersion
        content_result = sf.ContentVersion.create({
            "Title": "Resume",
            "PathOnClient": file.name,
            "VersionData": pdf_base64
        })
        content_doc_id = content_result.get("id")

        # Link file to record
        sf.ContentDocumentLink.create({
            "ContentDocumentId": content_doc_id,
            "LinkedEntityId": record_id,
            "ShareType": "V",
            "Visibility": "AllUsers"
        })

        # Build download link (you can improve this with actual org domain)
        download_link = f"https://{sf.sf_instance}/sfc/servlet.shepherd/document/download/{content_doc_id}"

        # Update score + file link on record
        sf.__getattr__(SF_OBJECT_NAME).update(record_id, {
            SF_SCORE_FIELD: round(score_val, 2),
            SF_LINK_FIELD: download_link
        })

        return f"{summary}\n\nβœ… Score saved in Salesforce.\nπŸ“Ž PDF uploaded: [Download]({download_link})"

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

# Gradio UI
gr.Interface(
    fn=process_resume,
    inputs=[
        gr.File(label="Upload Resume (PDF)", file_types=[".pdf"]),
        gr.Textbox(label="Salesforce Record ID (Agent_Prospect__c)")
    ],
    outputs="text",
    title="LIC Resume AI Scorer",
    description="Upload a PDF resume and enter Salesforce Record ID to auto-score and attach the file."
).launch(share=False)