Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,96 @@
|
|
1 |
-
|
|
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
except Exception as e:
|
16 |
return f"❌ Error: {str(e)}"
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
fn=
|
21 |
inputs=[
|
22 |
-
gr.
|
23 |
-
gr.Textbox(
|
24 |
],
|
25 |
outputs="text",
|
26 |
-
title="LIC
|
27 |
-
description="
|
28 |
-
)
|
29 |
-
|
30 |
-
# Launch WITHOUT the share button
|
31 |
-
interface.launch(share=False) # 👈 No Share via Link button
|
|
|
1 |
+
import os
|
2 |
+
import pdfplumber
|
3 |
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
from simple_salesforce import Salesforce
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import requests
|
8 |
+
import base64
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
|
13 |
+
# Salesforce credentials from .env
|
14 |
+
SF_USERNAME = os.getenv("SF_USERNAME")
|
15 |
+
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
16 |
+
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
17 |
+
SF_LOGIN_URL = os.getenv("SF_LOGIN_URL")
|
18 |
+
SF_OBJECT_NAME = os.getenv("SF_OBJECT_NAME", "Agent_Prospect__c")
|
19 |
+
SF_SCORE_FIELD = os.getenv("SF_SCORE_FIELD", "Suitability_Score__c")
|
20 |
+
SF_LINK_FIELD = os.getenv("SF_RESUME_FIELD_LINK", "Resume_File_Link__c")
|
21 |
+
SF_RECORD_ID = os.getenv("SF_RECORD_ID")
|
22 |
+
|
23 |
+
# Hugging Face classifier
|
24 |
classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
25 |
|
26 |
+
# Connect to Salesforce
|
27 |
+
sf = Salesforce(
|
28 |
+
username=SF_USERNAME,
|
29 |
+
password=SF_PASSWORD,
|
30 |
+
security_token=SF_SECURITY_TOKEN,
|
31 |
+
domain="login" if "login" in SF_LOGIN_URL else "test"
|
32 |
+
)
|
33 |
+
|
34 |
+
def process_resume(file, record_id):
|
35 |
try:
|
36 |
+
# Extract text from PDF
|
37 |
+
with pdfplumber.open(file.name) as pdf:
|
38 |
+
text = ""
|
39 |
+
for page in pdf.pages:
|
40 |
+
text += page.extract_text() or ""
|
41 |
+
|
42 |
+
if not text.strip():
|
43 |
+
return "❌ Could not extract text from PDF."
|
44 |
+
|
45 |
+
# Call HF model
|
46 |
+
result = classifier(text[:1000])
|
47 |
+
score_text = result[0]['label']
|
48 |
+
score_val = float(result[0]['score']) * 100
|
49 |
+
summary = f"Predicted Label: {score_text}\nSuitability Score: {score_val:.2f}"
|
50 |
+
|
51 |
+
# Convert PDF to base64
|
52 |
+
with open(file.name, "rb") as f:
|
53 |
+
pdf_content = f.read()
|
54 |
+
pdf_base64 = base64.b64encode(pdf_content).decode("utf-8")
|
55 |
+
|
56 |
+
# Upload PDF to Salesforce using ContentVersion
|
57 |
+
content_result = sf.ContentVersion.create({
|
58 |
+
"Title": "Resume",
|
59 |
+
"PathOnClient": file.name,
|
60 |
+
"VersionData": pdf_base64
|
61 |
+
})
|
62 |
+
content_doc_id = content_result.get("id")
|
63 |
+
|
64 |
+
# Link file to record
|
65 |
+
sf.ContentDocumentLink.create({
|
66 |
+
"ContentDocumentId": content_doc_id,
|
67 |
+
"LinkedEntityId": record_id,
|
68 |
+
"ShareType": "V",
|
69 |
+
"Visibility": "AllUsers"
|
70 |
+
})
|
71 |
+
|
72 |
+
# Build download link (you can improve this with actual org domain)
|
73 |
+
download_link = f"https://{sf.sf_instance}/sfc/servlet.shepherd/document/download/{content_doc_id}"
|
74 |
+
|
75 |
+
# Update score + file link on record
|
76 |
+
sf.__getattr__(SF_OBJECT_NAME).update(record_id, {
|
77 |
+
SF_SCORE_FIELD: round(score_val, 2),
|
78 |
+
SF_LINK_FIELD: download_link
|
79 |
+
})
|
80 |
+
|
81 |
+
return f"{summary}\n\n✅ Score saved in Salesforce.\n📎 PDF uploaded: [Download]({download_link})"
|
82 |
+
|
83 |
except Exception as e:
|
84 |
return f"❌ Error: {str(e)}"
|
85 |
|
86 |
+
# Gradio UI
|
87 |
+
gr.Interface(
|
88 |
+
fn=process_resume,
|
89 |
inputs=[
|
90 |
+
gr.File(label="Upload Resume (PDF)", file_types=[".pdf"]),
|
91 |
+
gr.Textbox(label="Salesforce Record ID (Agent_Prospect__c)")
|
92 |
],
|
93 |
outputs="text",
|
94 |
+
title="LIC Resume AI Scorer",
|
95 |
+
description="Upload a PDF resume and enter Salesforce Record ID to auto-score and attach the file."
|
96 |
+
).launch(share=False)
|
|
|
|
|
|