File size: 8,353 Bytes
099275d
26632cd
b9869ac
099275d
 
b8b023b
099275d
 
 
b8b023b
 
f038148
099275d
 
b9869ac
 
9c87ed0
099275d
9c87ed0
 
 
1ab2895
 
 
f038148
 
 
 
1ab2895
 
 
 
 
1ef118b
 
 
 
 
f038148
 
 
 
1ef118b
 
 
 
 
099275d
 
 
 
 
883e33e
099275d
 
883e33e
 
099275d
 
 
 
883e33e
099275d
 
883e33e
 
099275d
6ceb267
 
 
b9869ac
099275d
 
 
 
 
 
b9869ac
099275d
6ceb267
 
 
b9869ac
099275d
 
 
 
 
 
 
 
 
b8b023b
099275d
 
 
 
1ef118b
099275d
 
6ceb267
 
 
b9869ac
099275d
 
 
 
 
 
 
 
 
 
 
 
b8b023b
099275d
 
 
d6b15a6
58179cd
d6b15a6
f4137db
d6b15a6
f038148
099275d
 
 
 
 
 
 
 
b9869ac
099275d
 
c1e25dc
099275d
 
 
 
 
 
 
 
 
 
b9869ac
 
f038148
b9869ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88dcc50
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import gradio as gr
import os
import json
from utils import (
    get_salesforce_client, get_salesforce_objects, get_object_fields,
    extract_text_from_pdf, extract_key_value_pairs,
    create_record, attach_pdf, log_failure
)

from ai_mapper import ai_map_fields  # βœ… NEW: AI-based mapper

# Ensure uploads directory exists
os.makedirs("uploads", exist_ok=True)

# --- Gradio Interface Functions ---

def upload_pdfs(files):
    uploaded_files = []
    if isinstance(files, list):
        for file in files:
            if file is not None:
                file_name = os.path.basename(file.name)
                file_path = f"uploads/{file_name}"
                try:
                    if hasattr(file, 'read'):
                        content = file.read()
                    else:
                        content = str(file).encode('utf-8')
                    with open(file_path, "wb") as f:
                        f.write(content)
                    uploaded_files.append(file_path)
                except Exception as e:
                    return f"Error processing {file_name}: {str(e)}"
    else:
        if files is not None:
            file_name = os.path.basename(files.name)
            file_path = f"uploads/{file_name}"
            try:
                if hasattr(files, 'read'):
                    content = files.read()
                else:
                    content = str(files).encode('utf-8')
                with open(file_path, "wb") as f:
                    f.write(content)
                uploaded_files.append(file_path)
            except Exception as e:
                return f"Error processing {file_name}: {str(e)}"
    return f"Uploaded {len(uploaded_files)} PDF(s): {', '.join(uploaded_files)}"

def fetch_objects():
    sf, error = get_salesforce_client()
    if error:
        return gr.update(choices=[]), f"Error: {error}"
    objects, error = get_salesforce_objects(sf)
    if error:
        return gr.update(choices=[]), f"Error: {error}"
    return gr.update(choices=objects), "Objects fetched successfully"

def fetch_fields(object_name):
    sf, error = get_salesforce_client()
    if error:
        return gr.update(choices=[]), f"Error: {error}"
    fields, error = get_object_fields(sf, object_name)
    if error:
        return gr.update(choices=[]), f"Error: {error}"
    return gr.update(choices=fields), "Fields fetched successfully"

def process_pdf(pdf_paths):
    if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
        return "Error: No valid PDF file provided"
    pdf_path = pdf_paths[0]
    text_data, error = extract_text_from_pdf(pdf_path)
    if error:
        return f"Error: {error}"
    kv_pairs, error = extract_key_value_pairs(pdf_path)
    if error:
        return f"Error: {error}"
    return f"Text:\n{text_data}\n\nKey-Value Pairs:\n{kv_pairs}"

def display_mappings(pdf_paths, object_name):
    if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
        return "Error: No valid PDF file provided"
    pdf_path = pdf_paths[0]
    sf, error = get_salesforce_client()
    if error:
        return f"Error: {error}"
    fields, error = get_object_fields(sf, object_name)
    if error:
        return f"Error: {error}"
    extracted_data, error = extract_key_value_pairs(pdf_path)
    if error:
        return f"Error: {error}"
    mappings, confidence_scores, error = ai_map_fields(extracted_data[0]["keys"], fields)  # βœ… updated
    if error:
        return f"Error: {error}"
    output = ""
    for key, field in mappings.items():
        output += f"{key} -> {field} (Confidence: {confidence_scores[key]})\n"
    return output

def migrate_to_salesforce(pdf_paths, object_name):
    if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
        return "Error: No valid PDF file provided"
    pdf_path = pdf_paths[0]
    sf, error = get_salesforce_client()
    if error:
        log_failure(pdf_path, object_name, error)
        return f"Error: {error}"
    extracted_data, error = extract_key_value_pairs(pdf_path)
    if error:
        log_failure(pdf_path, object_name, error)
        return f"Error: {error}"
    fields, error = get_object_fields(sf, object_name)
    if error:
        log_failure(pdf_path, object_name, error)
        return f"Error: {error}"
    mappings, _, error = ai_map_fields(extracted_data[0]["keys"], fields)  # βœ… updated
    if error:
        log_failure(pdf_path, object_name, error)
        return f"Error: {error}"
    data = {mappings[key]: value for key, value in zip(extracted_data[0]["keys"], extracted_data[0]["values"])}

    # βœ… Required Salesforce IDs added as per Option 2
    data["AccountId"] = "001dL00001ASyPbQAL"
    data["OwnerId"] = "005dL00000f9B0l"

    record_id, error = create_record(sf, object_name, data)
    if error:
        log_failure(pdf_path, object_name, error)
        return f"Error: {error}"
    attach_status, error = attach_pdf(sf, record_id, pdf_path)
    if error:
        log_failure(pdf_path, object_name, error)
        return f"Error: {error}"
    return f"βœ… Record Created: {record_id}\nπŸ“Ž Attachment: {attach_status}"

def display_failures():
    try:
        with open("failures.json", "r") as f:
            failures = [json.loads(line) for line in f]
        output = ""
        for idx, failure in enumerate(failures):
            output += f"Failure {idx + 1}: PDF={failure['pdf']}, Object={failure['object']}, Error={failure['error']}\n"
        return output
    except FileNotFoundError:
        return "No failures logged"

def retry_migration(pdf_path, object_name):
    return migrate_to_salesforce([pdf_path], object_name)

# --- Gradio App Layout ---

with gr.Blocks(css="footer {display: none !important;}") as app:
    gr.Markdown("# 🧠 Smart Contract Migrator")

    with gr.Tab("πŸ“„ Upload & Process PDF"):
        with gr.Row():
            pdf_upload = gr.File(label="Upload Contract PDFs", file_count="multiple", file_types=[".pdf"])
            upload_status = gr.Textbox(label="Status", interactive=False)
        pdf_upload.change(upload_pdfs, inputs=pdf_upload, outputs=upload_status)

        with gr.Row():
            process_button = gr.Button("Extract Info")
            process_output = gr.Textbox(label="Text & Key-Value Output", lines=6, interactive=False)
        process_button.click(process_pdf, inputs=pdf_upload, outputs=process_output)

    with gr.Tab("πŸ” Salesforce Integration"):
        with gr.Row():
            fetch_objects_button = gr.Button("πŸ”„ Get Objects")
            object_dropdown = gr.Dropdown(label="Salesforce Object")
            object_status = gr.Textbox(label="Status", interactive=False)
        fetch_objects_button.click(fetch_objects, outputs=[object_dropdown, object_status])

        with gr.Row():
            fetch_fields_button = gr.Button("πŸ”„ Get Fields")
            field_checkboxes = gr.CheckboxGroup(label="Fields")
            field_status = gr.Textbox(label="Status", interactive=False)
        fetch_fields_button.click(fetch_fields, inputs=object_dropdown, outputs=[field_checkboxes, field_status])

        with gr.Row():
            map_button = gr.Button("πŸ”— Map Fields")
            mapping_output = gr.Textbox(label="Mappings", lines=6, interactive=False)
        map_button.click(display_mappings, inputs=[pdf_upload, object_dropdown], outputs=mapping_output)

        with gr.Row():
            migrate_button = gr.Button("πŸš€ Migrate to Salesforce")
            migrate_output = gr.Textbox(label="Migration Result", lines=4, interactive=False)
        migrate_button.click(migrate_to_salesforce, inputs=[pdf_upload, object_dropdown], outputs=migrate_output)

    with gr.Tab("🧾 Reconciliation"):
        with gr.Row():
            show_failures_button = gr.Button("πŸ“‹ Show Failures")
            failures_output = gr.Textbox(label="Failures", lines=6, interactive=False)
        show_failures_button.click(display_failures, outputs=failures_output)

        with gr.Row():
            pdf_path_input = gr.Textbox(label="PDF Path to Retry")
            retry_button = gr.Button("πŸ” Retry")
            retry_output = gr.Textbox(label="Retry Status", interactive=False)
        retry_button.click(retry_migration, inputs=[pdf_path_input, object_dropdown], outputs=retry_output)

# --- Launch App ---
app.launch()