Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
from utils import (
|
| 5 |
get_salesforce_client, get_salesforce_objects, get_object_fields,
|
| 6 |
extract_text_from_pdf, extract_key_value_pairs, map_fields,
|
|
@@ -10,7 +10,8 @@ from utils import (
|
|
| 10 |
# Ensure uploads directory exists
|
| 11 |
os.makedirs("uploads", exist_ok=True)
|
| 12 |
|
| 13 |
-
# Gradio Interface Functions
|
|
|
|
| 14 |
def upload_pdfs(files):
|
| 15 |
uploaded_files = []
|
| 16 |
if isinstance(files, list):
|
|
@@ -65,19 +66,19 @@ def fetch_fields(object_name):
|
|
| 65 |
def process_pdf(pdf_paths):
|
| 66 |
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 67 |
return "Error: No valid PDF file provided"
|
| 68 |
-
pdf_path = pdf_paths[0]
|
| 69 |
text_data, error = extract_text_from_pdf(pdf_path)
|
| 70 |
if error:
|
| 71 |
return f"Error: {error}"
|
| 72 |
kv_pairs, error = extract_key_value_pairs(pdf_path)
|
| 73 |
if error:
|
| 74 |
return f"Error: {error}"
|
| 75 |
-
return f"Text
|
| 76 |
|
| 77 |
def display_mappings(pdf_paths, object_name):
|
| 78 |
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 79 |
return "Error: No valid PDF file provided"
|
| 80 |
-
pdf_path = pdf_paths[0]
|
| 81 |
sf, error = get_salesforce_client()
|
| 82 |
if error:
|
| 83 |
return f"Error: {error}"
|
|
@@ -98,7 +99,7 @@ def display_mappings(pdf_paths, object_name):
|
|
| 98 |
def migrate_to_salesforce(pdf_paths, object_name):
|
| 99 |
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 100 |
return "Error: No valid PDF file provided"
|
| 101 |
-
pdf_path = pdf_paths[0]
|
| 102 |
sf, error = get_salesforce_client()
|
| 103 |
if error:
|
| 104 |
log_failure(pdf_path, object_name, error)
|
|
@@ -124,7 +125,7 @@ def migrate_to_salesforce(pdf_paths, object_name):
|
|
| 124 |
if error:
|
| 125 |
log_failure(pdf_path, object_name, error)
|
| 126 |
return f"Error: {error}"
|
| 127 |
-
return f"Record Created: {record_id}\n{attach_status}"
|
| 128 |
|
| 129 |
def display_failures():
|
| 130 |
try:
|
|
@@ -138,57 +139,58 @@ def display_failures():
|
|
| 138 |
return "No failures logged"
|
| 139 |
|
| 140 |
def retry_migration(pdf_path, object_name):
|
| 141 |
-
return migrate_to_salesforce([pdf_path], object_name)
|
| 142 |
-
|
| 143 |
-
# Gradio
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
gr.
|
| 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 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
from utils import (
|
| 5 |
get_salesforce_client, get_salesforce_objects, get_object_fields,
|
| 6 |
extract_text_from_pdf, extract_key_value_pairs, map_fields,
|
|
|
|
| 10 |
# Ensure uploads directory exists
|
| 11 |
os.makedirs("uploads", exist_ok=True)
|
| 12 |
|
| 13 |
+
# --- Gradio Interface Functions ---
|
| 14 |
+
|
| 15 |
def upload_pdfs(files):
|
| 16 |
uploaded_files = []
|
| 17 |
if isinstance(files, list):
|
|
|
|
| 66 |
def process_pdf(pdf_paths):
|
| 67 |
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 68 |
return "Error: No valid PDF file provided"
|
| 69 |
+
pdf_path = pdf_paths[0]
|
| 70 |
text_data, error = extract_text_from_pdf(pdf_path)
|
| 71 |
if error:
|
| 72 |
return f"Error: {error}"
|
| 73 |
kv_pairs, error = extract_key_value_pairs(pdf_path)
|
| 74 |
if error:
|
| 75 |
return f"Error: {error}"
|
| 76 |
+
return f"Text:\n{text_data}\n\nKey-Value Pairs:\n{kv_pairs}"
|
| 77 |
|
| 78 |
def display_mappings(pdf_paths, object_name):
|
| 79 |
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 80 |
return "Error: No valid PDF file provided"
|
| 81 |
+
pdf_path = pdf_paths[0]
|
| 82 |
sf, error = get_salesforce_client()
|
| 83 |
if error:
|
| 84 |
return f"Error: {error}"
|
|
|
|
| 99 |
def migrate_to_salesforce(pdf_paths, object_name):
|
| 100 |
if not pdf_paths or not isinstance(pdf_paths, list) or not pdf_paths[0]:
|
| 101 |
return "Error: No valid PDF file provided"
|
| 102 |
+
pdf_path = pdf_paths[0]
|
| 103 |
sf, error = get_salesforce_client()
|
| 104 |
if error:
|
| 105 |
log_failure(pdf_path, object_name, error)
|
|
|
|
| 125 |
if error:
|
| 126 |
log_failure(pdf_path, object_name, error)
|
| 127 |
return f"Error: {error}"
|
| 128 |
+
return f"β
Record Created: {record_id}\nπ Attachment: {attach_status}"
|
| 129 |
|
| 130 |
def display_failures():
|
| 131 |
try:
|
|
|
|
| 139 |
return "No failures logged"
|
| 140 |
|
| 141 |
def retry_migration(pdf_path, object_name):
|
| 142 |
+
return migrate_to_salesforce([pdf_path], object_name)
|
| 143 |
+
|
| 144 |
+
# --- Gradio App Layout ---
|
| 145 |
+
|
| 146 |
+
with gr.Blocks(css="footer {display: none !important;}") as app:
|
| 147 |
+
gr.Markdown("# π§ Smart Contract Migrator")
|
| 148 |
+
|
| 149 |
+
with gr.Tab("π Upload & Process PDF"):
|
| 150 |
+
with gr.Row():
|
| 151 |
+
pdf_upload = gr.File(label="Upload Contract PDFs", file_count="multiple", file_types=[".pdf"])
|
| 152 |
+
upload_status = gr.Textbox(label="Status", interactive=False)
|
| 153 |
+
pdf_upload.change(upload_pdfs, inputs=pdf_upload, outputs=upload_status)
|
| 154 |
+
|
| 155 |
+
with gr.Row():
|
| 156 |
+
process_button = gr.Button("Extract Info")
|
| 157 |
+
process_output = gr.Textbox(label="Text & Key-Value Output", lines=6, interactive=False)
|
| 158 |
+
process_button.click(process_pdf, inputs=pdf_upload, outputs=process_output)
|
| 159 |
+
|
| 160 |
+
with gr.Tab("π Salesforce Integration"):
|
| 161 |
+
with gr.Row():
|
| 162 |
+
fetch_objects_button = gr.Button("π Get Objects")
|
| 163 |
+
object_dropdown = gr.Dropdown(label="Salesforce Object")
|
| 164 |
+
object_status = gr.Textbox(label="Status", interactive=False)
|
| 165 |
+
fetch_objects_button.click(fetch_objects, outputs=[object_dropdown, object_status])
|
| 166 |
+
|
| 167 |
+
with gr.Row():
|
| 168 |
+
fetch_fields_button = gr.Button("π Get Fields")
|
| 169 |
+
field_checkboxes = gr.CheckboxGroup(label="Fields")
|
| 170 |
+
field_status = gr.Textbox(label="Status", interactive=False)
|
| 171 |
+
fetch_fields_button.click(fetch_fields, inputs=object_dropdown, outputs=[field_checkboxes, field_status])
|
| 172 |
+
|
| 173 |
+
with gr.Row():
|
| 174 |
+
map_button = gr.Button("π Map Fields")
|
| 175 |
+
mapping_output = gr.Textbox(label="Mappings", lines=6, interactive=False)
|
| 176 |
+
map_button.click(display_mappings, inputs=[pdf_upload, object_dropdown], outputs=mapping_output)
|
| 177 |
+
|
| 178 |
+
with gr.Row():
|
| 179 |
+
migrate_button = gr.Button("π Migrate to Salesforce")
|
| 180 |
+
migrate_output = gr.Textbox(label="Migration Result", lines=4, interactive=False)
|
| 181 |
+
migrate_button.click(migrate_to_salesforce, inputs=[pdf_upload, object_dropdown], outputs=migrate_output)
|
| 182 |
+
|
| 183 |
+
with gr.Tab("π§Ύ Reconciliation"):
|
| 184 |
+
with gr.Row():
|
| 185 |
+
show_failures_button = gr.Button("π Show Failures")
|
| 186 |
+
failures_output = gr.Textbox(label="Failures", lines=6, interactive=False)
|
| 187 |
+
show_failures_button.click(display_failures, outputs=failures_output)
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
pdf_path_input = gr.Textbox(label="PDF Path to Retry")
|
| 191 |
+
retry_button = gr.Button("π Retry")
|
| 192 |
+
retry_output = gr.Textbox(label="Retry Status", interactive=False)
|
| 193 |
+
retry_button.click(retry_migration, inputs=[pdf_path_input, object_dropdown], outputs=retry_output)
|
| 194 |
+
|
| 195 |
+
# --- Launch App ---
|
| 196 |
+
app.launch()
|