Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import json | |
| API_URL = "https://ia.nebuia.com/api/v1/integrator/extractor/from/excel" | |
| API_KEY = "2J7NFWQ-236PGAM-2S9RD3C-2DG54CY" | |
| API_SECRET = "a47abf97-866b-4154-b29c-346c9b02919e" | |
| import re | |
| import json | |
| def extract_json_from_response(response_text): | |
| """ | |
| Detecta si la respuesta contiene un bloque JSON con formato markdown ```json ``` | |
| y extrae su contenido, o devuelve el texto original si no se encuentra ese patrón. | |
| Args: | |
| response_text (str): El texto de respuesta que puede contener bloques JSON | |
| Returns: | |
| str: El contenido JSON extraído o el texto original | |
| """ | |
| # Patrón para encontrar bloques de código JSON con formato markdown | |
| json_pattern = r"```json\s+([\s\S]*?)\s+```" | |
| # Buscar coincidencias | |
| match = re.search(json_pattern, response_text) | |
| if match: | |
| # Extraer el contenido JSON del bloque | |
| json_content = match.group(1).strip() | |
| # Verificar que sea un JSON válido | |
| try: | |
| # Intentar cargar el JSON para validarlo | |
| json.loads(json_content) | |
| return json_content | |
| except json.JSONDecodeError: | |
| # Si no es un JSON válido, devolver el texto original | |
| return response_text | |
| # Si no hay coincidencias, devolver el texto original | |
| return response_text | |
| # Modificación de la función send_excel_to_api para usar la función de extracción | |
| 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() | |
| # Si la respuesta contiene un payload, formatearla como JSON | |
| if "payload" in result: | |
| response_json = json.dumps(result["payload"], indent=2, ensure_ascii=False) | |
| # Extraer JSON de la respuesta si está en formato markdown | |
| return extract_json_from_response(response_json) | |
| else: | |
| return json.dumps(result, indent=2, ensure_ascii=False) | |
| 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) | |
| # Custom color for the theme | |
| custom_purple = "#7f56d9" | |
| # Create the Gradio interface using Blocks | |
| with gr.Blocks(theme=gr.themes.Default(primary_hue="purple", secondary_hue="purple")) as demo: | |
| gr.Markdown( | |
| """ | |
| <div style="text-align: center;"> | |
| <img src="https://copilot.nebuia.com/images/logo_white.png" style="max-height: 400px; max-width: 120px; object-fit: contain;"> | |
| </div> | |
| ## NebuIA Excel Structure Extractor | |
| Upload an Excel file and provide a JSON structure to send to the API. | |
| """ | |
| ) | |
| with gr.Row(): | |
| file_input = gr.File(label="Upload Excel File") | |
| json_input = gr.Code( | |
| label="JSON Structure", | |
| language="json", | |
| lines=15, | |
| value='''{ | |
| "total de activo a_corto_plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "efectivo y equivalentes de efectivo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "instrumentos financieros de negociacion": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "clientes": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "cuentas y documentos por cobrar": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "deudores diversos": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "estimaciones de cuentas incobrables": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "impuestos por recuperar": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "inventarios": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "pagos anticipados": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "activos disponibles para venta": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "otros activos a corto plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "total de activo a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "propiedades, plantas y equipo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "anticipo a proveedores": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "crédito mercantil": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "activos intangibles": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "inversiones asociadas": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "instrumentos financieros por cobrar a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "otros activos a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "total de activo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "total de pasivo a corto plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "prestamos bancarios": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "proveedores": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "cuentas y documentos por pagar": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "instrumentos financieros": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "acreedores diversos": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "impuestos a la utilidad por pagar": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "anticipo de clientes": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "provisiones": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "impuestos por pagar": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "otros pasivos a corto plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "total de pasivo a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "cuentas y documentos por pagar a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "instrumentos financieros a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "rentas cobradas por anticipado": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "aportaciones para futuros aumentos de capital": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "impuesto a la utilidad diferido por pagar": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "beneficio a empleados": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "provisiones a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "otros pasivos a largo plazo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "total de pasivo": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "total de capital contable": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "capital social": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "reserva legal": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "exceso en la actualización del capital": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "insuficiencia en la actualización de capital": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "reservas de capital": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "primas en venta de acciones": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "utilidades acumuladas": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "utilidad neta": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "perdidas acumuladas": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "participación controladora": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "ingresos netos": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "costo de ventas": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ], | |
| "costo de servicios": [ | |
| { | |
| "año": "", | |
| "total": "" | |
| } | |
| ] | |
| }''' | |
| ) | |
| output = gr.Code(label="API Response", lines=10, language="json") | |
| submit_btn = gr.Button("Submit") | |
| submit_btn.click(fn=gradio_interface, inputs=[file_input, json_input], outputs=output) | |
| # Launch the interface | |
| demo.launch() |