import gradio as gr import requests import json import os API_URL = "https://ia.nebuia.com/api/v1/integrator/extractor/from/image" API_KEY = "2J7NFWQ-236PGAM-2S9RD3C-2DG54CY" API_SECRET = "a47abf97-866b-4154-b29c-346c9b02919e" def send_excel_to_api(file_path, json_structure): headers = { 'key': API_KEY, 'secret': API_SECRET } files = { 'file': open(file_path, 'rb') } data = { 'structure': json_structure } try: response = requests.post(API_URL, headers=headers, files=files, data=data) if response.status_code == 200: result = response.json() print(result) if result.get("status"): return json.dumps(result["payload"], indent=2) else: return f"Error: {result.get('message', 'Unknown error')}" else: return f"Error: Received status code {response.status_code}\n{response.text}" except requests.RequestException as e: return f"Error sending request: {e}" finally: files['file'].close() def gradio_interface(file, json_structure): if file is None: return "Please upload an Excel file." return send_excel_to_api(file.name, json_structure) # Create the Gradio interface iface = gr.Interface( fn=gradio_interface, inputs=[ gr.File(label="Upload Excel File"), gr.Code(label="JSON Structure", language="json", lines=10, value='''{ "curp": "", "clave_elector": "", "nombre": "", "fecha_nacimiento": "" }''') ], outputs=gr.Textbox(label="API Response", lines=10), title="NebuIA Excel Extractor", description="Sube un excel, modifica la estructura a llenar y presiona Submit." ) # Launch the interface iface.launch()