Spaces:
Runtime error
Runtime error
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) |