rawanessam commited on
Commit
949b422
·
verified ·
1 Parent(s): 84ca400

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -146
app.py CHANGED
@@ -1,156 +1,28 @@
1
  import gradio as gr
2
  import json
3
- from typing import Union, List, Dict, Any
4
 
5
- def extract_bq_codes(input_data: Union[str, Dict]) -> Dict[str, Any]:
6
- """
7
- Robust JSON processor that handles:
8
- - Direct API calls (raw JSON)
9
- - Gradio API format ({"data": [...]})
10
- - String input from UI
11
-
12
- Returns:
13
- {
14
- "success": bool,
15
- "codes": List[str],
16
- "error": Optional[str],
17
- "input_type": str
18
- }
19
- """
20
  try:
21
- # Debugging: Log raw input
22
- print(f"\n--- Received input ---\nType: {type(input_data)}\nContent: {input_data[:200]}...\n")
23
-
24
- # Phase 1: Parse input
25
- if isinstance(input_data, dict):
26
- if "data" in input_data: # Gradio API format
27
- if not input_data["data"]:
28
- return {"success": False, "error": "Empty data array"}
29
- content = input_data["data"][0]
30
- data = json.loads(content) if isinstance(content, str) else content
31
- else: # Raw JSON format
32
- data = input_data
33
- elif isinstance(input_data, str):
34
- try:
35
- data = json.loads(input_data)
36
- except json.JSONDecodeError as e:
37
- return {
38
- "success": False,
39
- "error": f"Invalid JSON string: {str(e)}",
40
- "input_type": str(type(input_data))
41
- }
42
- else:
43
- return {
44
- "success": False,
45
- "error": f"Unsupported input type: {type(input_data)}",
46
- "input_type": str(type(input_data))
47
- }
48
-
49
- # Phase 2: Validate data structure
50
- if not isinstance(data, dict):
51
- return {
52
- "success": False,
53
- "error": f"Expected dictionary, got {type(data)}",
54
- "input_type": str(type(input_data))
55
- }
56
-
57
- if "projecttemplates" not in data:
58
- return {
59
- "success": False,
60
- "error": "Missing 'projecttemplates' in JSON",
61
- "input_type": str(type(input_data))
62
- }
63
-
64
- # Phase 3: Process data
65
- codes = set()
66
-
67
- # Safely navigate through the complex nested structure
68
- for project in data.get("projecttemplates", []):
69
- if not isinstance(project, dict):
70
- continue
71
-
72
- for detail in project.get("projecttemplatedetails", []):
73
- if not isinstance(detail, dict):
74
- continue
75
-
76
- # Handle potential None values in bqcodelibrary
77
- bqcode_lib = detail.get("bqcodelibrary")
78
- if bqcode_lib and isinstance(bqcode_lib, dict):
79
- bqcode = bqcode_lib.get("bqcode")
80
- if bqcode and isinstance(bqcode, str):
81
- codes.add(bqcode)
82
-
83
- return {
84
- "success": True,
85
- "codes": sorted(list(codes)),
86
- "input_type": str(type(input_data)),
87
- "count": len(codes)
88
- }
89
-
90
- except Exception as e:
91
- return {
92
- "success": False,
93
- "error": f"Unexpected error: {str(e)}",
94
- "type": str(type(e)),
95
- "input_type": str(type(input_data)) if 'input_data' in locals() else "unknown"
96
- }
97
 
98
- # Create interface with improved examples
99
- examples = [
100
- json.dumps({
101
- "projecttemplates": [
102
- {
103
- "projecttemplatedetails": [
104
- {
105
- "bqcodelibrary": {
106
- "bqcode": "F10.01.01.01.00_block"
107
- }
108
- }
109
- ]
110
- }
111
- ]
112
- }),
113
- {
114
- "projecttemplates": [
115
- {
116
- "projecttemplatedetails": [
117
- {
118
- "bqcodelibrary": {
119
- "bqcode": "L20.01.00.01.00_doors"
120
- }
121
- }
122
- ]
123
- }
124
- ]
125
- }
126
- ]
127
 
128
  iface = gr.Interface(
129
- fn=extract_bq_codes,
130
- inputs=gr.Textbox(
131
- lines=15,
132
- label="JSON Input",
133
- placeholder='Paste your complex JSON here...',
134
- max_lines=25
135
- ),
136
- outputs=gr.JSON(label="Extraction Results"),
137
- examples=examples,
138
- title="BQ Code Extractor (Enterprise Version)",
139
- description="""Safely extracts BQ codes from complex construction project JSON structures.
140
- Accepts:
141
- - Raw JSON strings
142
- - JSON objects
143
- - Gradio API format""",
144
- allow_flagging="never",
145
- theme="default"
146
  )
147
 
148
- # Launch with production-ready settings
149
  if __name__ == "__main__":
150
- iface.launch(
151
- server_name="0.0.0.0",
152
- server_port=7860,
153
- debug=True,
154
- show_error=True,
155
- enable_queue=True
156
- )
 
1
  import gradio as gr
2
  import json
 
3
 
4
+ def extract_bq_codes_from_json(json_str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  try:
6
+ data = json.loads(json_str)
7
+ except json.JSONDecodeError:
8
+ return {"error": "Invalid JSON"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ projecttemplates = data.get("projecttemplates", [])
11
+ codes = set()
12
+ for project in projecttemplates:
13
+ details = project.get("projecttemplatedetails", [])
14
+ for detail in details:
15
+ bqcode = detail.get("bqcodelibrary", {}).get("bqcode")
16
+ if bqcode:
17
+ codes.add(bqcode)
18
+ return list(codes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  iface = gr.Interface(
21
+ fn=extract_bq_codes_from_json,
22
+ inputs=gr.Textbox(lines=10, label="JSON Input"),
23
+ outputs=gr.JSON(label="Extracted BQ Codes"),
24
+ title="BQ Code Extractor"
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
 
27
  if __name__ == "__main__":
28
+ iface.launch()