Oranblock commited on
Commit
212ac8d
·
verified ·
1 Parent(s): 976abbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -49
app.py CHANGED
@@ -1,71 +1,53 @@
1
  import gradio as gr
2
  import json
3
- import json5
4
 
5
- # Function to attempt to fix JSON using json5
6
- def fix_json(json_data):
7
- try:
8
- # First, try parsing with json5 which is more lenient
9
- parsed_data = json5.loads(json_data)
10
-
11
- # Try to pretty-print (indent) the parsed data
12
- fixed_json = json.dumps(parsed_data, indent=4)
13
- return fixed_json, "JSON fixed using json5."
14
-
15
- except Exception as e:
16
- return None, f"Failed to fix JSON: {str(e)}"
17
 
18
- # Function to remove duplicates and fix JSON
19
- def process_json(json_data):
20
- # Check if the uploaded JSON data is empty
21
- if not json_data.strip():
22
- return None, "Error: The uploaded file is empty.", None
23
 
24
- # Attempt to fix the JSON
25
- fixed_json, fix_message = fix_json(json_data)
26
- if fixed_json is None:
27
- return None, fix_message, None
28
 
 
 
 
 
29
  try:
30
- # Parse the fixed JSON string to a list of dictionaries
31
- data = json.loads(fixed_json)
32
-
33
- # Check if data is a list of dictionaries
34
- if not isinstance(data, list):
35
- return None, "Error: JSON is not a list of objects", None
36
-
37
- # Remove duplicates by converting the list to a set of tuples, then back to a list of dicts
38
- unique_data = [dict(t) for t in {tuple(d.items()) for d in data}]
39
-
40
- # Convert the list back to JSON
41
- cleaned_json = json.dumps(unique_data, indent=4)
42
-
43
- return cleaned_json, f"JSON is valid and cleaned. {fix_message}", cleaned_json
44
-
45
  except json.JSONDecodeError as e:
46
- return None, f"Invalid JSON after fixing: {e}", None
47
 
48
  # Function to handle file uploads and processing
49
  def process_file(uploaded_file):
50
  # Read the content of the uploaded file directly
51
  json_data = uploaded_file # This is already the content of the file as a string
52
-
53
- # Process the JSON data
54
- cleaned_json, message, download_json = process_json(json_data)
55
-
56
- if message:
57
- return cleaned_json, message, download_json
58
-
59
- # Return the cleaned JSON for display and as a downloadable file
60
- return cleaned_json, "JSON is valid and cleaned.", download_json
 
61
 
62
  # Gradio interface
63
  iface = gr.Interface(
64
  fn=process_file,
65
  inputs=gr.File(label="Upload your JSON file"),
66
  outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")],
67
- title="Advanced JSON Cleaner and Fixer",
68
- description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version."
69
  )
70
 
71
  iface.launch()
 
1
  import gradio as gr
2
  import json
3
+ from transformers import pipeline
4
 
5
+ # Load a text generation model from Hugging Face without an API key
6
+ # You can use a smaller model if gpt-3.5-turbo isn't available
7
+ generator = pipeline("text-generation", model="gpt-3.5-turbo")
 
 
 
 
 
 
 
 
 
8
 
9
+ # Function to attempt to fix JSON using a Hugging Face model
10
+ def ai_fix_json(json_data):
11
+ # Prepare a prompt for the AI model to fix the JSON
12
+ prompt = f"Fix the following JSON data and make it valid:\n\n{json_data}\n\nFixed JSON:"
 
13
 
14
+ # Generate a response from the model
15
+ response = generator(prompt, max_length=1024, num_return_sequences=1)[0]['generated_text']
 
 
16
 
17
+ # Extract the fixed JSON from the response (you may need to fine-tune this depending on the model's output format)
18
+ fixed_json = response.split("Fixed JSON:")[-1].strip()
19
+
20
+ # Try to load the fixed JSON to ensure it's valid
21
  try:
22
+ parsed_data = json.loads(fixed_json)
23
+ pretty_json = json.dumps(parsed_data, indent=4)
24
+ return pretty_json, "JSON fixed using AI."
25
+
 
 
 
 
 
 
 
 
 
 
 
26
  except json.JSONDecodeError as e:
27
+ return None, f"Failed to fix JSON: {str(e)}"
28
 
29
  # Function to handle file uploads and processing
30
  def process_file(uploaded_file):
31
  # Read the content of the uploaded file directly
32
  json_data = uploaded_file # This is already the content of the file as a string
33
+
34
+ # Attempt to fix the JSON using AI
35
+ cleaned_json, message = ai_fix_json(json_data)
36
+
37
+ if cleaned_json:
38
+ # Return the fixed JSON for display and as a downloadable file
39
+ return cleaned_json, message, cleaned_json
40
+
41
+ else:
42
+ return None, message, None
43
 
44
  # Gradio interface
45
  iface = gr.Interface(
46
  fn=process_file,
47
  inputs=gr.File(label="Upload your JSON file"),
48
  outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")],
49
+ title="AI-Powered JSON Cleaner",
50
+ description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version using AI."
51
  )
52
 
53
  iface.launch()