Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,9 @@ import spaces
|
|
| 9 |
os.environ['CUDA_HOME'] = '/usr/local/cuda'
|
| 10 |
os.environ['PATH'] = os.environ['PATH'] + ':/usr/local/cuda/bin'
|
| 11 |
|
| 12 |
-
def initialize_zerogpu():
|
| 13 |
-
# Example function to initialize ZeroGPU, replace with actual initialization if needed
|
| 14 |
-
pass
|
| 15 |
-
|
| 16 |
# Function to perform JSON correction using a chosen model
|
| 17 |
@spaces.GPU(duration=120) # Use GPU for this function, if available
|
| 18 |
def ai_fix_json(model_id, json_data):
|
| 19 |
-
# Initialize the InferenceClient with the chosen model
|
| 20 |
client = InferenceClient(model=model_id)
|
| 21 |
|
| 22 |
prompt = f"Fix the following JSON data and make it valid:\n\n{json_data}\n\nFixed JSON:"
|
|
@@ -31,11 +26,13 @@ def ai_fix_json(model_id, json_data):
|
|
| 31 |
raise RuntimeError("GPU not available, falling back to CPU.")
|
| 32 |
|
| 33 |
except Exception as gpu_error:
|
| 34 |
-
# Fall back to CPU if GPU processing fails
|
| 35 |
print(f"Falling back to CPU due to: {gpu_error}")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def process_file(model_id, uploaded_file):
|
| 41 |
json_data = uploaded_file # This is already the content of the file as a string
|
|
@@ -48,20 +45,29 @@ def process_file(model_id, uploaded_file):
|
|
| 48 |
except json.JSONDecodeError as e:
|
| 49 |
return None, f"Failed to fix JSON: {str(e)}", None
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=process_file,
|
| 53 |
inputs=[
|
| 54 |
-
gr.
|
| 55 |
gr.File(label="Upload your JSON file")
|
| 56 |
],
|
| 57 |
outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")],
|
| 58 |
title="AI-Powered JSON Cleaner with Model Selection",
|
| 59 |
-
description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version using AI with GPU/CPU fallback. Select any model from the
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
print("ZeroGPU initialized.")
|
| 66 |
except Exception as e:
|
| 67 |
print(f"ZeroGPU initialization failed: {e}. Falling back to CPU.")
|
|
|
|
| 9 |
os.environ['CUDA_HOME'] = '/usr/local/cuda'
|
| 10 |
os.environ['PATH'] = os.environ['PATH'] + ':/usr/local/cuda/bin'
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function to perform JSON correction using a chosen model
|
| 13 |
@spaces.GPU(duration=120) # Use GPU for this function, if available
|
| 14 |
def ai_fix_json(model_id, json_data):
|
|
|
|
| 15 |
client = InferenceClient(model=model_id)
|
| 16 |
|
| 17 |
prompt = f"Fix the following JSON data and make it valid:\n\n{json_data}\n\nFixed JSON:"
|
|
|
|
| 26 |
raise RuntimeError("GPU not available, falling back to CPU.")
|
| 27 |
|
| 28 |
except Exception as gpu_error:
|
|
|
|
| 29 |
print(f"Falling back to CPU due to: {gpu_error}")
|
| 30 |
+
try:
|
| 31 |
+
response = client.text_generation(prompt, max_new_tokens=1024)
|
| 32 |
+
fixed_json = response[0]['generated_text'].split("Fixed JSON:")[-1].strip()
|
| 33 |
+
return fixed_json, f"JSON fixed using AI on CPU with model: {model_id}"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return None, f"Failed to process with model {model_id}. Error: {str(e)}", None
|
| 36 |
|
| 37 |
def process_file(model_id, uploaded_file):
|
| 38 |
json_data = uploaded_file # This is already the content of the file as a string
|
|
|
|
| 45 |
except json.JSONDecodeError as e:
|
| 46 |
return None, f"Failed to fix JSON: {str(e)}", None
|
| 47 |
|
| 48 |
+
# List of available models
|
| 49 |
+
model_options = [
|
| 50 |
+
"EleutherAI/gpt-neo-2.7B",
|
| 51 |
+
"gpt2",
|
| 52 |
+
"facebook/opt-1.3b",
|
| 53 |
+
"EleutherAI/gpt-j-6B",
|
| 54 |
+
"google/flan-t5-base"
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=process_file,
|
| 59 |
inputs=[
|
| 60 |
+
gr.Dropdown(label="Select Model", choices=model_options, value="EleutherAI/gpt-neo-2.7B"),
|
| 61 |
gr.File(label="Upload your JSON file")
|
| 62 |
],
|
| 63 |
outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")],
|
| 64 |
title="AI-Powered JSON Cleaner with Model Selection",
|
| 65 |
+
description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version using AI with GPU/CPU fallback. Select any model from the dropdown list."
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
try:
|
| 70 |
+
# Initialize any necessary services, e.g., ZeroGPU
|
| 71 |
print("ZeroGPU initialized.")
|
| 72 |
except Exception as e:
|
| 73 |
print(f"ZeroGPU initialization failed: {e}. Falling back to CPU.")
|