Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import base64
|
5 |
+
|
6 |
+
API_URL = "https://image-modal.nebuia.com/extract_id_data"
|
7 |
+
|
8 |
+
def send_image_to_api(image_path, json_prompt):
|
9 |
+
# Read the image file and convert to base64
|
10 |
+
with open(image_path, "rb") as image_file:
|
11 |
+
image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
|
12 |
+
|
13 |
+
# Parse the JSON prompt
|
14 |
+
try:
|
15 |
+
json_prompt_dict = json.loads(json_prompt)
|
16 |
+
except json.JSONDecodeError:
|
17 |
+
return "Error: Invalid JSON prompt"
|
18 |
+
|
19 |
+
# Create a dictionary with the file data and JSON prompt
|
20 |
+
files = {
|
21 |
+
"file": ("image.jpg", base64.b64decode(image_base64), "image/jpeg")
|
22 |
+
}
|
23 |
+
data = {
|
24 |
+
"json_prompt": json.dumps(json_prompt_dict)
|
25 |
+
}
|
26 |
+
|
27 |
+
try:
|
28 |
+
# Send POST request to the API
|
29 |
+
response = requests.post(API_URL, files=files, data=data)
|
30 |
+
|
31 |
+
# Check if the request was successful
|
32 |
+
if response.status_code == 200:
|
33 |
+
# Parse the JSON response
|
34 |
+
result = response.json()
|
35 |
+
if result.get("success"):
|
36 |
+
return json.dumps(result["data"], indent=2)
|
37 |
+
else:
|
38 |
+
error_message = f"Error in processing:\n{result.get('error', 'Unknown error')}\n"
|
39 |
+
error_message += f"Raw output: {result.get('raw_output', 'No raw output available')}"
|
40 |
+
return error_message
|
41 |
+
else:
|
42 |
+
return f"Error: Received status code {response.status_code}\n{response.text}"
|
43 |
+
except requests.RequestException as e:
|
44 |
+
return f"Error sending request: {e}"
|
45 |
+
|
46 |
+
# Define the Gradio interface
|
47 |
+
def gradio_interface(image_path, json_prompt):
|
48 |
+
if image_path is None:
|
49 |
+
return "Please upload an image."
|
50 |
+
return send_image_to_api(image_path, json_prompt)
|
51 |
+
|
52 |
+
# Create the Gradio interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=gradio_interface,
|
55 |
+
inputs=[
|
56 |
+
gr.Image(type="filepath", label="Sube un ID"),
|
57 |
+
gr.Code(label="JSON Prompt", language="json", lines=10, value='{\n "nombre": ""\n}')
|
58 |
+
],
|
59 |
+
outputs=gr.Textbox(label="API Response", lines=10),
|
60 |
+
title="NebuIA ID Estructure Extractor",
|
61 |
+
description="Sube un ID, modifica la estructura con los datos que deseas extraer y presiona submint."
|
62 |
+
)
|
63 |
+
|
64 |
+
# Launch the interface
|
65 |
+
iface.launch()
|