Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,43 @@ API_URL = "https://ia.nebuia.com/api/v1/integrator/extractor/from/excel"
|
|
| 6 |
API_KEY = "2J7NFWQ-236PGAM-2S9RD3C-2DG54CY"
|
| 7 |
API_SECRET = "a47abf97-866b-4154-b29c-346c9b02919e"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def send_excel_to_api(file_path, json_structure):
|
| 10 |
headers = {
|
| 11 |
'key': API_KEY,
|
|
@@ -25,8 +62,13 @@ def send_excel_to_api(file_path, json_structure):
|
|
| 25 |
|
| 26 |
if response.status_code == 200:
|
| 27 |
result = response.json()
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
return f"Error: Received status code {response.status_code}\n{response.text}"
|
| 32 |
except requests.RequestException as e:
|
|
|
|
| 6 |
API_KEY = "2J7NFWQ-236PGAM-2S9RD3C-2DG54CY"
|
| 7 |
API_SECRET = "a47abf97-866b-4154-b29c-346c9b02919e"
|
| 8 |
|
| 9 |
+
import re
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
def extract_json_from_response(response_text):
|
| 13 |
+
"""
|
| 14 |
+
Detecta si la respuesta contiene un bloque JSON con formato markdown ```json ```
|
| 15 |
+
y extrae su contenido, o devuelve el texto original si no se encuentra ese patr贸n.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
response_text (str): El texto de respuesta que puede contener bloques JSON
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
str: El contenido JSON extra铆do o el texto original
|
| 22 |
+
"""
|
| 23 |
+
# Patr贸n para encontrar bloques de c贸digo JSON con formato markdown
|
| 24 |
+
json_pattern = r"```json\s+([\s\S]*?)\s+```"
|
| 25 |
+
|
| 26 |
+
# Buscar coincidencias
|
| 27 |
+
match = re.search(json_pattern, response_text)
|
| 28 |
+
|
| 29 |
+
if match:
|
| 30 |
+
# Extraer el contenido JSON del bloque
|
| 31 |
+
json_content = match.group(1).strip()
|
| 32 |
+
|
| 33 |
+
# Verificar que sea un JSON v谩lido
|
| 34 |
+
try:
|
| 35 |
+
# Intentar cargar el JSON para validarlo
|
| 36 |
+
json.loads(json_content)
|
| 37 |
+
return json_content
|
| 38 |
+
except json.JSONDecodeError:
|
| 39 |
+
# Si no es un JSON v谩lido, devolver el texto original
|
| 40 |
+
return response_text
|
| 41 |
+
|
| 42 |
+
# Si no hay coincidencias, devolver el texto original
|
| 43 |
+
return response_text
|
| 44 |
+
|
| 45 |
+
# Modificaci贸n de la funci贸n send_excel_to_api para usar la funci贸n de extracci贸n
|
| 46 |
def send_excel_to_api(file_path, json_structure):
|
| 47 |
headers = {
|
| 48 |
'key': API_KEY,
|
|
|
|
| 62 |
|
| 63 |
if response.status_code == 200:
|
| 64 |
result = response.json()
|
| 65 |
+
# Si la respuesta contiene un payload, formatearla como JSON
|
| 66 |
+
if "payload" in result:
|
| 67 |
+
response_json = json.dumps(result["payload"], indent=2, ensure_ascii=False)
|
| 68 |
+
# Extraer JSON de la respuesta si est谩 en formato markdown
|
| 69 |
+
return extract_json_from_response(response_json)
|
| 70 |
+
else:
|
| 71 |
+
return json.dumps(result, indent=2, ensure_ascii=False)
|
| 72 |
else:
|
| 73 |
return f"Error: Received status code {response.status_code}\n{response.text}"
|
| 74 |
except requests.RequestException as e:
|