| import gradio as gr |
| import json |
| from transformers import pipeline |
|
|
| |
| |
| generator = pipeline("text-generation", model="gpt-3.5-turbo") |
|
|
| |
| def ai_fix_json(json_data): |
| |
| prompt = f"Fix the following JSON data and make it valid:\n\n{json_data}\n\nFixed JSON:" |
|
|
| |
| response = generator(prompt, max_length=1024, num_return_sequences=1)[0]['generated_text'] |
|
|
| |
| fixed_json = response.split("Fixed JSON:")[-1].strip() |
| |
| |
| try: |
| parsed_data = json.loads(fixed_json) |
| pretty_json = json.dumps(parsed_data, indent=4) |
| return pretty_json, "JSON fixed using AI." |
| |
| except json.JSONDecodeError as e: |
| return None, f"Failed to fix JSON: {str(e)}" |
|
|
| |
| def process_file(uploaded_file): |
| |
| json_data = uploaded_file |
| |
| |
| cleaned_json, message = ai_fix_json(json_data) |
| |
| if cleaned_json: |
| |
| return cleaned_json, message, cleaned_json |
| |
| else: |
| return None, message, None |
|
|
| |
| iface = gr.Interface( |
| fn=process_file, |
| inputs=gr.File(label="Upload your JSON file"), |
| outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")], |
| title="AI-Powered JSON Cleaner", |
| description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version using AI." |
| ) |
|
|
| iface.launch() |