Update app.py
Browse files
app.py
CHANGED
|
@@ -40,13 +40,19 @@ def ai_fix_json(model_id, json_data):
|
|
| 40 |
return None, f"Failed to process with model {model_id}. Error: {str(e)}"
|
| 41 |
|
| 42 |
def process_file(model_id, uploaded_file):
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
cleaned_json, message = ai_fix_json(model_id, json_data)
|
| 45 |
|
| 46 |
if cleaned_json:
|
| 47 |
try:
|
| 48 |
-
parsed_data = json.loads(cleaned_json)
|
| 49 |
-
pretty_json = json.dumps(parsed_data, indent=4)
|
| 50 |
return pretty_json, message, pretty_json
|
| 51 |
except json.JSONDecodeError as e:
|
| 52 |
return None, f"Failed to fix JSON: {str(e)}", None
|
|
|
|
| 40 |
return None, f"Failed to process with model {model_id}. Error: {str(e)}"
|
| 41 |
|
| 42 |
def process_file(model_id, uploaded_file):
|
| 43 |
+
# If uploaded_file is a file-like object, read its content
|
| 44 |
+
if hasattr(uploaded_file, 'read'):
|
| 45 |
+
json_data = uploaded_file.read().decode("utf-8")
|
| 46 |
+
else:
|
| 47 |
+
# If it's already a string (like in the case of `NamedString`), use it directly
|
| 48 |
+
json_data = uploaded_file
|
| 49 |
+
|
| 50 |
cleaned_json, message = ai_fix_json(model_id, json_data)
|
| 51 |
|
| 52 |
if cleaned_json:
|
| 53 |
try:
|
| 54 |
+
parsed_data = json.loads(cleaned_json, ensure_ascii=False) # Properly load and handle Unicode characters
|
| 55 |
+
pretty_json = json.dumps(parsed_data, indent=4, ensure_ascii=False) # Keep Unicode characters readable
|
| 56 |
return pretty_json, message, pretty_json
|
| 57 |
except json.JSONDecodeError as e:
|
| 58 |
return None, f"Failed to fix JSON: {str(e)}", None
|