File size: 2,457 Bytes
40fc766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391a75e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40fc766
 
 
 
 
 
 
 
 
 
391a75e
 
 
 
 
 
 
40fc766
 
391a75e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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"

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()
            return json.dumps(result, indent=2)
        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='''{
    "activo a corto plazo": {
        "valor inventarios - ejercicio 2023": "",
        "valor inventarios - ejercicio 2022": ""
    },
    "pasivo a corto plazo": {
        "impuestos por pagar - ejercicio 2023": "",
        "impuestos por pagar - ejercicio 2022": ""
    },
    "perdida_del_ejercicio": "",
    "rfc": ""
}'''
        )
    
    output = gr.Textbox(label="API Response", lines=10)
    submit_btn = gr.Button("Submit")
    
    submit_btn.click(fn=gradio_interface, inputs=[file_input, json_input], outputs=output)

# Launch the interface
demo.launch()