rawanessam commited on
Commit
6ccee3e
·
verified ·
1 Parent(s): 77f5325

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -21
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
- # Handle both API and UI inputs
 
 
 
6
  if isinstance(input_data, dict):
7
- # API case - extract from 'data' field if present
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
- return {"error": "Invalid JSON in data field"}
16
- else:
 
17
  data = input_data
18
- else:
19
- # UI case - direct string input
20
  try:
21
  data = json.loads(input_data)
22
- except:
23
- return {"error": "Invalid JSON string"}
 
 
 
24
 
25
- # Extraction logic
26
- codes = set()
27
- for project in data.get("projecttemplates", []):
28
- for detail in project.get("projecttemplatedetails", []):
29
- if (bqcode := detail.get("bqcodelibrary", {}).get("bqcode")):
30
- codes.add(bqcode)
31
- return list(codes)
 
 
 
 
 
 
 
 
 
 
 
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
- title="BQ Code Extractor"
 
 
38
  )
39
 
40
- iface.launch()
 
 
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)