Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,166 +1,124 @@
|
|
|
|
|
| 1 |
import os
|
| 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 |
-
return
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return
|
| 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 |
-
else:
|
| 125 |
-
mappings[key] = object_fields[0]
|
| 126 |
-
confidence_scores[key] = 0.0
|
| 127 |
-
|
| 128 |
-
return mappings, confidence_scores, None
|
| 129 |
-
except Exception as e:
|
| 130 |
-
return None, None, str(e)
|
| 131 |
-
|
| 132 |
-
def create_record(sf, object_name, data):
|
| 133 |
-
try:
|
| 134 |
-
result = sf.__getattr__(object_name).create(data)
|
| 135 |
-
return result.get("id", "Unknown ID"), None
|
| 136 |
-
except Exception as e:
|
| 137 |
-
return None, str(e)
|
| 138 |
-
|
| 139 |
-
def attach_pdf(sf, record_id, pdf_path):
|
| 140 |
-
try:
|
| 141 |
-
with open(pdf_path, "rb") as f:
|
| 142 |
-
body = f.read()
|
| 143 |
-
content_version = sf.ContentVersion.create({
|
| 144 |
-
"Title": os.path.basename(pdf_path),
|
| 145 |
-
"PathOnClient": os.path.basename(pdf_path),
|
| 146 |
-
"VersionData": body.encode("base64") if isinstance(body, str) else body
|
| 147 |
-
})
|
| 148 |
-
|
| 149 |
-
content_document_id = sf.query(
|
| 150 |
-
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version['id']}'"
|
| 151 |
-
)["records"][0]["ContentDocumentId"]
|
| 152 |
-
|
| 153 |
-
sf.ContentDocumentLink.create({
|
| 154 |
-
"ContentDocumentId": content_document_id,
|
| 155 |
-
"LinkedEntityId": record_id,
|
| 156 |
-
"ShareType": "V"
|
| 157 |
-
})
|
| 158 |
-
|
| 159 |
-
return "PDF attached successfully", None
|
| 160 |
-
except Exception as e:
|
| 161 |
-
return None, str(e)
|
| 162 |
-
|
| 163 |
-
def log_failure(pdf_path, object_name, error):
|
| 164 |
-
with open("failures.json", "a") as f:
|
| 165 |
-
json.dump({"pdf": pdf_path, "object": object_name, "error": error}, f)
|
| 166 |
-
f.write("\n")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from utils import (
|
| 4 |
+
get_salesforce_client,
|
| 5 |
+
get_salesforce_objects,
|
| 6 |
+
get_object_fields,
|
| 7 |
+
extract_key_value_pairs,
|
| 8 |
+
map_fields,
|
| 9 |
+
create_record,
|
| 10 |
+
attach_pdf,
|
| 11 |
+
log_failure
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Set up environment
|
| 15 |
+
os.environ["GRADIO_TEMP_DIR"] = "temp"
|
| 16 |
+
os.makedirs("temp", exist_ok=True)
|
| 17 |
+
|
| 18 |
+
# Global variable
|
| 19 |
+
object_fields_cache = {}
|
| 20 |
+
|
| 21 |
+
# Get all Salesforce object names
|
| 22 |
+
def get_salesforce_object_names():
|
| 23 |
+
sf, error = get_salesforce_client()
|
| 24 |
+
if error:
|
| 25 |
+
return ["Error connecting to Salesforce"]
|
| 26 |
+
object_names, _ = get_salesforce_objects(sf)
|
| 27 |
+
return object_names
|
| 28 |
+
|
| 29 |
+
# Extract key-value pairs
|
| 30 |
+
def extract_contract_fields(pdf_paths):
|
| 31 |
+
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 32 |
+
return {}, "No PDF uploaded"
|
| 33 |
+
extracted_data, error = extract_key_value_pairs(pdf_paths[0])
|
| 34 |
+
if error:
|
| 35 |
+
return {}, f"Extraction error: {error}"
|
| 36 |
+
return extracted_data[0], "Extraction successful"
|
| 37 |
+
|
| 38 |
+
# Map fields between extracted data and Salesforce object fields
|
| 39 |
+
def map_contract_fields(pdf_paths, object_name):
|
| 40 |
+
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 41 |
+
return "No PDF uploaded"
|
| 42 |
+
sf, error = get_salesforce_client()
|
| 43 |
+
if error:
|
| 44 |
+
return f"Error: {error}"
|
| 45 |
+
extracted_data, error = extract_key_value_pairs(pdf_paths[0])
|
| 46 |
+
if error:
|
| 47 |
+
return f"Error: {error}"
|
| 48 |
+
if object_name in object_fields_cache:
|
| 49 |
+
fields = object_fields_cache[object_name]
|
| 50 |
+
else:
|
| 51 |
+
fields, error = get_object_fields(sf, object_name)
|
| 52 |
+
object_fields_cache[object_name] = fields
|
| 53 |
+
mappings, scores, _ = map_fields(extracted_data, fields)
|
| 54 |
+
mapping_display = ""
|
| 55 |
+
for key, sf_field in mappings.items():
|
| 56 |
+
mapping_display += f"{key} -> {sf_field} (Confidence: {scores.get(key, 0)})\n"
|
| 57 |
+
return mapping_display
|
| 58 |
+
|
| 59 |
+
# Migrate to Salesforce
|
| 60 |
+
def migrate_to_salesforce(pdf_paths, object_name):
|
| 61 |
+
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 62 |
+
return "Error: No valid PDF file provided"
|
| 63 |
+
pdf_path = pdf_paths[0]
|
| 64 |
+
sf, error = get_salesforce_client()
|
| 65 |
+
if error:
|
| 66 |
+
log_failure(pdf_path, object_name, error)
|
| 67 |
+
return f"Error: {error}"
|
| 68 |
+
extracted_data, error = extract_key_value_pairs(pdf_path)
|
| 69 |
+
if error:
|
| 70 |
+
log_failure(pdf_path, object_name, error)
|
| 71 |
+
return f"Error: {error}"
|
| 72 |
+
fields, error = get_object_fields(sf, object_name)
|
| 73 |
+
if error:
|
| 74 |
+
log_failure(pdf_path, object_name, error)
|
| 75 |
+
return f"Error: {error}"
|
| 76 |
+
mappings, _, error = map_fields(extracted_data, fields)
|
| 77 |
+
if error:
|
| 78 |
+
log_failure(pdf_path, object_name, error)
|
| 79 |
+
return f"Error: {error}"
|
| 80 |
+
|
| 81 |
+
data = {}
|
| 82 |
+
for key, value in zip(extracted_data[0]["keys"], extracted_data[0]["values"]):
|
| 83 |
+
sf_field = mappings.get(key)
|
| 84 |
+
if not sf_field:
|
| 85 |
+
continue
|
| 86 |
+
if "value" in key.lower() and isinstance(value, str):
|
| 87 |
+
value = float(value.replace(",", "").replace("$", ""))
|
| 88 |
+
data[sf_field] = value
|
| 89 |
+
|
| 90 |
+
data["AccountId"] = "001XXXXXXXXXXXXXXX" # Replace with actual AccountId
|
| 91 |
+
|
| 92 |
+
record_id, error = create_record(sf, object_name, data)
|
| 93 |
+
if error:
|
| 94 |
+
log_failure(pdf_path, object_name, error)
|
| 95 |
+
return f"Error: {error}"
|
| 96 |
+
attach_status, error = attach_pdf(sf, record_id, pdf_path)
|
| 97 |
+
if error:
|
| 98 |
+
log_failure(pdf_path, object_name, error)
|
| 99 |
+
return f"Error: {error}"
|
| 100 |
+
return f"✅ Record Created: {record_id}\n📎 Attachment: {attach_status}"
|
| 101 |
+
|
| 102 |
+
# Gradio UI
|
| 103 |
+
with gr.Blocks(title="Smart Contract Migrator") as demo:
|
| 104 |
+
gr.Markdown("# 📄 Smart Contract Migrator to Salesforce")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
pdf_input = gr.File(label="Upload Contract PDF", file_types=[".pdf"])
|
| 108 |
+
object_dropdown = gr.Dropdown(label="Salesforce Object", choices=get_salesforce_object_names())
|
| 109 |
+
|
| 110 |
+
extract_btn = gr.Button("Extract Fields")
|
| 111 |
+
extract_output = gr.JSON(label="Extracted Fields")
|
| 112 |
+
|
| 113 |
+
map_btn = gr.Button("Map Fields")
|
| 114 |
+
map_output = gr.Textbox(label="Field Mappings")
|
| 115 |
+
|
| 116 |
+
migrate_btn = gr.Button("Migrate to Salesforce")
|
| 117 |
+
migrate_output = gr.Textbox(label="Migration Result")
|
| 118 |
+
|
| 119 |
+
extract_btn.click(fn=extract_contract_fields, inputs=[pdf_input], outputs=[extract_output, map_output])
|
| 120 |
+
map_btn.click(fn=map_contract_fields, inputs=[pdf_input, object_dropdown], outputs=map_output)
|
| 121 |
+
migrate_btn.click(fn=migrate_to_salesforce, inputs=[pdf_input, object_dropdown], outputs=migrate_output)
|
| 122 |
+
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|