File size: 2,268 Bytes
8e9e3a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
605fbb1
8e9e3a7
 
 
 
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
import gradio as gr
import requests
import json
import base64

API_URL = "https://image-modal.nebuia.com/extract_id_data"

def send_image_to_api(image_path, json_prompt):
    # Read the image file and convert to base64
    with open(image_path, "rb") as image_file:
        image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
    
    # Parse the JSON prompt
    try:
        json_prompt_dict = json.loads(json_prompt)
    except json.JSONDecodeError:
        return "Error: Invalid JSON prompt"

    # Create a dictionary with the file data and JSON prompt
    files = {
        "file": ("image.jpg", base64.b64decode(image_base64), "image/jpeg")
    }
    data = {
        "json_prompt": json.dumps(json_prompt_dict)
    }
    
    try:
        # Send POST request to the API
        response = requests.post(API_URL, files=files, data=data)
        
        # Check if the request was successful
        if response.status_code == 200:
            # Parse the JSON response
            result = response.json()
            if result.get("success"):
                return json.dumps(result["data"], indent=2)
            else:
                error_message = f"Error in processing:\n{result.get('error', 'Unknown error')}\n"
                error_message += f"Raw output: {result.get('raw_output', 'No raw output available')}"
                return error_message
        else:
            return f"Error: Received status code {response.status_code}\n{response.text}"
    except requests.RequestException as e:
        return f"Error sending request: {e}"

# Define the Gradio interface
def gradio_interface(image_path, json_prompt):
    if image_path is None:
        return "Please upload an image."
    return send_image_to_api(image_path, json_prompt)

# Create the Gradio interface
iface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Image(type="filepath", label="Sube un ID"),
        gr.Code(label="JSON Prompt", language="json", lines=10, value='{\n    "nombre": ""\n}')
    ],
    outputs=gr.Textbox(label="API Response", lines=10),
    title="NebuIA ID Estructure Extractor",
    description="Sube un ID, modifica la estructura con los datos que deseas extraer y presiona Submit."
)

# Launch the interface
iface.launch()