Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 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 =
|
| 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)}"
|
|
@@ -46,8 +46,8 @@ 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()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
from transformers import pipeline
|
| 4 |
+
from huggingface_hub import InferenceApi
|
| 5 |
|
| 6 |
+
# Set up the Inference API to use a GPU in the Hugging Face Space
|
| 7 |
+
inference = InferenceApi(repo_id="gpt-3.5-turbo", task="text-generation", use_gpu=True)
|
|
|
|
| 8 |
|
| 9 |
+
# Function to attempt to fix JSON using a Hugging Face model with GPU
|
| 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 using GPU
|
| 15 |
+
response = inference(inputs=prompt, max_length=1024, num_return_sequences=1)
|
| 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[0]['generated_text'].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 with GPU."
|
| 25 |
|
| 26 |
except json.JSONDecodeError as e:
|
| 27 |
return None, f"Failed to fix JSON: {str(e)}"
|
|
|
|
| 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 with GPU",
|
| 50 |
+
description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version using AI on a GPU."
|
| 51 |
)
|
| 52 |
|
| 53 |
iface.launch()
|