Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,58 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
def extract_bq_codes_from_json(input_data):
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
| 6 |
if isinstance(input_data, dict):
|
| 7 |
-
|
| 8 |
-
if "data" in input_data:
|
| 9 |
try:
|
| 10 |
if isinstance(input_data["data"][0], str):
|
| 11 |
data = json.loads(input_data["data"][0])
|
| 12 |
else:
|
| 13 |
data = input_data["data"][0]
|
| 14 |
-
except:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
data = input_data
|
| 18 |
-
|
| 19 |
-
# UI case - direct string input
|
| 20 |
try:
|
| 21 |
data = json.loads(input_data)
|
| 22 |
-
except:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Extraction logic
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
for
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=extract_bq_codes_from_json,
|
| 35 |
-
inputs=gr.Textbox(lines=10, label="JSON Input"),
|
| 36 |
-
outputs=gr.JSON(),
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
def extract_bq_codes_from_json(input_data):
|
| 5 |
+
# Debug: Print raw input
|
| 6 |
+
print("Raw input received:", type(input_data), input_data)
|
| 7 |
+
|
| 8 |
+
# Handle all possible input formats
|
| 9 |
if isinstance(input_data, dict):
|
| 10 |
+
if "data" in input_data: # Gradio API format
|
|
|
|
| 11 |
try:
|
| 12 |
if isinstance(input_data["data"][0], str):
|
| 13 |
data = json.loads(input_data["data"][0])
|
| 14 |
else:
|
| 15 |
data = input_data["data"][0]
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print("API format parse error:", e)
|
| 18 |
+
return {"error": f"Invalid API format: {str(e)}"}
|
| 19 |
+
else: # Raw JSON format
|
| 20 |
data = input_data
|
| 21 |
+
elif isinstance(input_data, str): # Direct text input
|
|
|
|
| 22 |
try:
|
| 23 |
data = json.loads(input_data)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print("String parse error:", e)
|
| 26 |
+
return {"error": f"Invalid JSON string: {str(e)}"}
|
| 27 |
+
else:
|
| 28 |
+
return {"error": "Input must be JSON string or object"}
|
| 29 |
|
| 30 |
+
# Extraction logic with defensive programming
|
| 31 |
+
try:
|
| 32 |
+
codes = set()
|
| 33 |
+
for project in data.get("projecttemplates", []):
|
| 34 |
+
for detail in project.get("projecttemplatedetails", []):
|
| 35 |
+
if isinstance(detail, dict):
|
| 36 |
+
bqcode = detail.get("bqcodelibrary", {}).get("bqcode")
|
| 37 |
+
if bqcode:
|
| 38 |
+
codes.add(bqcode)
|
| 39 |
+
return {"success": True, "codes": list(codes)}
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return {"error": f"Processing error: {str(e)}", "received_data": data}
|
| 42 |
+
|
| 43 |
+
# Create interface with explicit examples
|
| 44 |
+
examples = [
|
| 45 |
+
{'projecttemplates': [{'projecttemplatedetails': [{'bqcodelibrary': {'bqcode': 'BQ123'}}]}]},
|
| 46 |
+
'{"projecttemplates":[{"projecttemplatedetails":[{"bqcodelibrary":{"bqcode":"BQ456"}}]}]'
|
| 47 |
+
]
|
| 48 |
|
| 49 |
iface = gr.Interface(
|
| 50 |
fn=extract_bq_codes_from_json,
|
| 51 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste JSON here...", label="JSON Input"),
|
| 52 |
+
outputs=gr.JSON(label="Results"),
|
| 53 |
+
examples=examples,
|
| 54 |
+
title="BQ Code Extractor",
|
| 55 |
+
description="Extracts BQ codes from JSON. Accepts both raw JSON objects and strings."
|
| 56 |
)
|
| 57 |
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
iface.launch(debug=True)
|