Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,32 +2,43 @@ 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
|
| 9 |
-
# Read the
|
| 10 |
-
with open(
|
| 11 |
-
|
| 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": (
|
| 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
|
|
@@ -44,22 +55,34 @@ def send_image_to_api(image_path, json_prompt):
|
|
| 44 |
return f"Error sending request: {e}"
|
| 45 |
|
| 46 |
# Define the Gradio interface
|
| 47 |
-
def gradio_interface(
|
| 48 |
-
if
|
| 49 |
-
return "Please upload an image."
|
| 50 |
-
return
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# Create the Gradio interface
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# Launch the interface
|
| 65 |
-
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import base64
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
|
| 8 |
API_URL = "https://image-modal.nebuia.com/extract_id_data"
|
| 9 |
|
| 10 |
+
def send_file_to_api(file_path, json_prompt):
|
| 11 |
+
# Read the file and convert to base64
|
| 12 |
+
with open(file_path, "rb") as file:
|
| 13 |
+
file_base64 = base64.b64encode(file.read()).decode('utf-8')
|
| 14 |
+
|
| 15 |
# Parse the JSON prompt
|
| 16 |
try:
|
| 17 |
json_prompt_dict = json.loads(json_prompt)
|
| 18 |
except json.JSONDecodeError:
|
| 19 |
return "Error: Invalid JSON prompt"
|
| 20 |
+
|
| 21 |
+
# Determine file type
|
| 22 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
| 23 |
+
if file_extension in ['.jpg', '.jpeg', '.png']:
|
| 24 |
+
mime_type = "image/jpeg"
|
| 25 |
+
elif file_extension == '.pdf':
|
| 26 |
+
mime_type = "application/pdf"
|
| 27 |
+
else:
|
| 28 |
+
return "Error: Unsupported file type"
|
| 29 |
+
|
| 30 |
# Create a dictionary with the file data and JSON prompt
|
| 31 |
files = {
|
| 32 |
+
"file": (os.path.basename(file_path), base64.b64decode(file_base64), mime_type)
|
| 33 |
}
|
| 34 |
data = {
|
| 35 |
"json_prompt": json.dumps(json_prompt_dict)
|
| 36 |
}
|
| 37 |
+
|
| 38 |
try:
|
| 39 |
# Send POST request to the API
|
| 40 |
response = requests.post(API_URL, files=files, data=data)
|
| 41 |
+
|
| 42 |
# Check if the request was successful
|
| 43 |
if response.status_code == 200:
|
| 44 |
# Parse the JSON response
|
|
|
|
| 55 |
return f"Error sending request: {e}"
|
| 56 |
|
| 57 |
# Define the Gradio interface
|
| 58 |
+
def gradio_interface(file, json_prompt):
|
| 59 |
+
if file is None:
|
| 60 |
+
return "Please upload an image or PDF file."
|
| 61 |
+
return send_file_to_api(file.name, json_prompt)
|
| 62 |
+
|
| 63 |
+
# Custom color for the theme
|
| 64 |
+
custom_purple = "#7f56d9"
|
| 65 |
|
| 66 |
+
# Create the Gradio interface using Blocks
|
| 67 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="purple", secondary_hue="purple")) as demo:
|
| 68 |
+
gr.Markdown(
|
| 69 |
+
"""
|
| 70 |
+
<div style="text-align: center;">
|
| 71 |
+
<img src="https://copilot.nebuia.com/images/logo_white.png" style="max-height: 400px; max-width: 120px; object-fit: contain;">
|
| 72 |
+
</div>
|
| 73 |
+
|
| 74 |
+
## NebuIA ID Structure Extractor
|
| 75 |
+
Sube un ID (imagen o PDF), modifica la estructura con los datos que deseas extraer y presiona Submit.
|
| 76 |
+
"""
|
| 77 |
+
)
|
| 78 |
+
with gr.Row():
|
| 79 |
+
file_input = gr.File(label="Sube un ID (imagen o PDF)", type="filepath")
|
| 80 |
+
json_input = gr.Code(label="JSON Prompt", language="json", lines=10, value='{\n "nombre": ""\n}')
|
| 81 |
+
|
| 82 |
+
output = gr.Textbox(label="API Response", lines=10)
|
| 83 |
+
submit_btn = gr.Button("Submit")
|
| 84 |
+
|
| 85 |
+
submit_btn.click(fn=gradio_interface, inputs=[file_input, json_input], outputs=output)
|
| 86 |
|
| 87 |
# Launch the interface
|
| 88 |
+
demo.launch()
|