viswanani commited on
Commit
caa6b89
Β·
verified Β·
1 Parent(s): f9e0d21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -61
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import os
3
- from io import BytesIO
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] # Take the first file from the list
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: {text_data}\nKey-Value Pairs: {kv_pairs}"
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] # Take the first file from the list
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] # Take the first file from the list
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) # Wrap single path in list for consistency
142
-
143
- # Gradio UI
144
- with gr.Blocks() as app:
145
- gr.Markdown("# Smart Contract Migrator")
146
-
147
- # PDF Upload Section
148
- gr.Markdown("## Upload PDFs")
149
- pdf_upload = gr.File(label="Upload Contract PDFs", file_count="multiple", file_types=[".pdf"])
150
- upload_status = gr.Textbox(label="Upload Status")
151
- pdf_upload.change(upload_pdfs, inputs=pdf_upload, outputs=upload_status)
152
-
153
- # Salesforce Object Selection
154
- gr.Markdown("## Salesforce Object Selection")
155
- object_dropdown = gr.Dropdown(label="Select Salesforce Object")
156
- fetch_objects_button = gr.Button("Fetch Objects")
157
- object_status = gr.Textbox(label="Object Fetch Status")
158
- fetch_objects_button.click(fetch_objects, outputs=[object_dropdown, object_status])
159
-
160
- # Field Selection
161
- field_checkboxes = gr.CheckboxGroup(label="Select Fields")
162
- fetch_fields_button = gr.Button("Fetch Fields")
163
- field_status = gr.Textbox(label="Field Fetch Status")
164
- fetch_fields_button.click(fetch_fields, inputs=object_dropdown, outputs=[field_checkboxes, field_status])
165
-
166
- # PDF Processing
167
- gr.Markdown("## Process PDF")
168
- process_button = gr.Button("Process PDF")
169
- process_output = gr.Textbox(label="Extracted Data")
170
- process_button.click(process_pdf, inputs=pdf_upload, outputs=process_output)
171
-
172
- # Field Mapping
173
- gr.Markdown("## Field Mapping")
174
- map_button = gr.Button("Map Fields")
175
- mapping_output = gr.Textbox(label="Field Mappings")
176
- map_button.click(display_mappings, inputs=[pdf_upload, object_dropdown], outputs=mapping_output)
177
-
178
- # Salesforce Migration
179
- gr.Markdown("## Migrate to Salesforce")
180
- migrate_button = gr.Button("Migrate to Salesforce")
181
- migrate_output = gr.Textbox(label="Migration Status")
182
- migrate_button.click(migrate_to_salesforce, inputs=[pdf_upload, object_dropdown], outputs=migrate_output)
183
-
184
- # Reconciliation Dashboard
185
- gr.Markdown("## Reconciliation Dashboard")
186
- show_failures_button = gr.Button("Show Failures")
187
- failures_output = gr.Textbox(label="Failed Migrations")
188
- pdf_path_input = gr.Textbox(label="PDF Path to Retry")
189
- retry_button = gr.Button("Retry Migration")
190
- retry_output = gr.Textbox(label="Retry Status")
191
- show_failures_button.click(display_failures, outputs=failures_output)
192
- retry_button.click(retry_migration, inputs=[pdf_path_input, object_dropdown], outputs=retry_output)
193
-
194
- app.launch()
 
 
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()