kivilaid commited on
Commit
22381af
·
1 Parent(s): 88f516c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -25
app.py CHANGED
@@ -1,31 +1,54 @@
1
  import gradio as gr
2
- import zipfile
3
- import os
4
- from tempfile import TemporaryDirectory
5
 
6
- def extract_zip(file_obj):
7
- with TemporaryDirectory() as tmpdirname:
8
- # Save the uploaded zip file to the temporary directory
9
- zip_path = os.path.join(tmpdirname, 'uploaded.zip')
10
- with open(zip_path, 'wb') as f:
11
- f.write(file_obj.read())
12
-
13
- # Extract the zip file
14
- with zipfile.ZipFile(zip_path, 'r') as zip_ref:
15
- zip_ref.extractall(tmpdirname)
16
-
17
- # Return the path to the extracted files
18
- return f"Extracted to {tmpdirname}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Create the Gradio interface
21
- iface = gr.Interface(
22
- fn=extract_zip,
23
- inputs=gr.inputs.File(type="file", label="Upload ZIP File"),
24
- outputs="text",
25
- title="ZIP File Extractor",
26
- description="Upload a ZIP file and it will be extracted."
27
  )
28
 
29
- if __name__ == "__main__":
30
- iface.launch()
31
-
 
1
  import gradio as gr
2
+ import json
 
 
3
 
4
+ def generate_carbone_tags_and_values(json_input):
5
+ try:
6
+ # Parse the JSON input
7
+ data = json.loads(json_input)
8
+
9
+ # Function to recursively extract keys, values, and format them in Carbone style
10
+ def extract_keys_and_values(obj, parent_key=''):
11
+ carbone_tags = []
12
+ carbone_tags_values = []
13
+ if isinstance(obj, dict):
14
+ for k, v in obj.items():
15
+ full_key = f"{parent_key}.{k}" if parent_key else k
16
+ tags, tags_values = extract_keys_and_values(v, full_key)
17
+ carbone_tags.extend(tags)
18
+ carbone_tags_values.extend(tags_values)
19
+ elif isinstance(obj, list):
20
+ for i, item in enumerate(obj):
21
+ tags, tags_values = extract_keys_and_values(item, f"{parent_key}[{i}]")
22
+ carbone_tags.extend(tags)
23
+ carbone_tags_values.extend(tags_values)
24
+ else:
25
+ carbone_tag = f"{{d.{parent_key}}}"
26
+ carbone_tags.append(carbone_tag)
27
+ carbone_tags_values.append((carbone_tag, obj))
28
+ return carbone_tags, carbone_tags_values
29
+
30
+ # Extract keys, values and format them
31
+ carbone_keys, carbone_keys_values = extract_keys_and_values(data)
32
+
33
+ # Sort the keys alphabetically
34
+ carbone_keys.sort()
35
+ carbone_keys_values.sort(key=lambda x: x[0])
36
+
37
+ # Format the outputs
38
+ tags_output = "\n".join(carbone_keys)
39
+ tags_values_output = "\n".join([f"{tag}: {value}" for tag, value in carbone_keys_values])
40
+ return tags_output, tags_values_output
41
+ except json.JSONDecodeError:
42
+ return "Invalid JSON. Please check your input.", ""
43
 
44
  # Create the Gradio interface
45
+ interface = gr.Interface(
46
+ fn=generate_carbone_tags_and_values,
47
+ inputs=gr.Textbox(lines=20, placeholder="Paste JSON here..."),
48
+ outputs=[gr.Textbox(lines=20), gr.Textbox(lines=20)],
49
+ title="JSON to Carbone Tags and Values Converter",
50
+ description="Paste JSON to generate Carbone tags and their values. The first output box shows only tags, and the second shows tags with their corresponding values."
51
  )
52
 
53
+ # Launch the interface
54
+ interface.launch()