11import gradio as gr
22from gpt import classify_and_count , extract_and_enrich_all
33from extract_first_nations import process_single_pdf
4-
54import json
65import os
76
@@ -36,28 +35,24 @@ def classify_document_ui(file_input):
3635
3736
3837def extract_and_enrich_ui (file_input , classification ):
39- """Run the full extraction + enrichment pipeline ."""
38+ """Run extraction + enrichment + first nations ."""
4039 if not classification :
41- return {"error" : "Please classify the document first." }
40+ return json . dumps ( {"error" : "Please classify the document first." }, indent = 4 )
4241 result = extract_and_enrich_all (file_input , classification )
43- return result
44-
42+ if result and "conditions" in result :
43+ file_path = file_input .name if hasattr (file_input , "name" ) else file_input
44+ if isinstance (file_path , str ) and file_path .endswith (".pdf" ):
45+ result = process_single_pdf (file_path , result )
46+ return json .dumps (result , indent = 4 )
4547
46- def add_first_nations_ui (file_input , enriched_json ):
47- """Add first nations info to the enriched JSON."""
48- if not enriched_json or "conditions" not in enriched_json :
49- return enriched_json
50- file_path = file_input .name if hasattr (file_input , "name" ) else file_input
51- if file_path .endswith (".pdf" ):
52- result = process_single_pdf (file_path , enriched_json )
53- return result
54- return enriched_json
5548
56-
57- def send_to_json_editor (json_data ):
58- if isinstance (json_data , str ):
59- json_data = json .loads (json_data )
60- return json .dumps (json_data , indent = 4 ), json_data
49+ def send_to_json_editor (json_str ):
50+ if isinstance (json_str , dict ):
51+ json_data = json_str
52+ else :
53+ json_data = json .loads (json_str ) if json_str else {}
54+ formatted = json .dumps (json_data , indent = 4 )
55+ return formatted , formatted
6156
6257
6358def save_json (content , project_id , document_id , project_name , project_type ,
@@ -93,9 +88,9 @@ def save_json(content, project_id, document_id, project_name, project_type,
9388 with open (output_path , "w" ) as f :
9489 json .dump (content_dict , f , indent = 4 )
9590
96- return f"Saved to { output_path } " , json .dumps (content_dict , indent = 4 ), content_dict
91+ return f"Saved to { output_path } " , json .dumps (content_dict , indent = 4 ), json . dumps ( content_dict , indent = 4 )
9792 except Exception as e :
98- return f"Save failed: { str (e )} " , content , None
93+ return f"Save failed: { str (e )} " , content , content if isinstance ( content , str ) else json . dumps ( content , indent = 4 )
9994
10095
10196# ---------------------------------------------------------------------------
@@ -129,8 +124,7 @@ def save_json(content, project_id, document_id, project_name, project_type,
129124 # --- Extraction section ---
130125 with gr .Column ():
131126 submit_button = gr .Button ("Extract & Enrich Conditions" , variant = "primary" )
132- extracted_conditions = gr .JSON (label = "Extracted & Enriched Conditions" )
133- first_nations_result = gr .JSON (label = "With First Nations" )
127+ final_result = gr .Code (language = "json" , label = "Extracted Conditions" , interactive = False )
134128
135129 with gr .Tab ("JSON Editor" ):
136130 # --- Metadata inputs ---
@@ -153,21 +147,17 @@ def save_json(content, project_id, document_id, project_name, project_type,
153147 status_output = gr .Textbox (label = "Status" , lines = 1 , interactive = False )
154148
155149 with gr .Row ():
156- json_viewer = gr .JSON ( label = "JSON Viewer" )
150+ json_viewer = gr .Code ( language = "json" , label = "JSON Viewer" , interactive = False )
157151 json_editor = gr .Textbox (label = "JSON Content Editor" , lines = 500 )
158152
159- # --- Pipeline: Classify -> Extract & Enrich -> First Nations -> Editor ---
153+ # --- Pipeline: Extract & Enrich (+ First Nations) -> Editor ---
160154 submit_button .click (
161155 fn = extract_and_enrich_ui ,
162156 inputs = [file_input , classification_state ],
163- outputs = [extracted_conditions ]
164- ).then (
165- fn = add_first_nations_ui ,
166- inputs = [file_input , extracted_conditions ],
167- outputs = [first_nations_result ]
157+ outputs = [final_result ]
168158 ).then (
169159 fn = send_to_json_editor ,
170- inputs = [first_nations_result ],
160+ inputs = [final_result ],
171161 outputs = [json_editor , json_viewer ]
172162 )
173163
0 commit comments