Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
class JSONEditor:
|
6 |
+
def __init__(self):
|
7 |
+
self.current_data = None
|
8 |
+
self.current_filename = None
|
9 |
+
|
10 |
+
def load_json(self, file):
|
11 |
+
"""Load JSON file and return its contents"""
|
12 |
+
try:
|
13 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
14 |
+
data = json.load(f)
|
15 |
+
self.current_data = data
|
16 |
+
self.current_filename = os.path.basename(file.name)
|
17 |
+
return json.dumps(data, indent=2), f"Successfully loaded {self.current_filename}"
|
18 |
+
except Exception as e:
|
19 |
+
return "", f"Error loading file: {str(e)}"
|
20 |
+
|
21 |
+
def update_json(self, json_str):
|
22 |
+
"""Validate and update JSON data"""
|
23 |
+
try:
|
24 |
+
# Validate JSON
|
25 |
+
updated_data = json.loads(json_str)
|
26 |
+
self.current_data = updated_data
|
27 |
+
return json.dumps(updated_data, indent=2), "JSON updated successfully"
|
28 |
+
except json.JSONDecodeError as e:
|
29 |
+
return json_str, f"Invalid JSON: {str(e)}"
|
30 |
+
|
31 |
+
def save_json(self):
|
32 |
+
"""Save current JSON data to a file"""
|
33 |
+
if not self.current_data:
|
34 |
+
return "No data to save", None
|
35 |
+
|
36 |
+
try:
|
37 |
+
# Create a default filename if not already loaded from a file
|
38 |
+
save_filename = self.current_filename or "edited_data.json"
|
39 |
+
|
40 |
+
# Ensure the file is saved in the current working directory
|
41 |
+
save_path = os.path.join(os.getcwd(), save_path)
|
42 |
+
|
43 |
+
with open(save_path, 'w', encoding='utf-8') as f:
|
44 |
+
json.dump(self.current_data, f, indent=2)
|
45 |
+
|
46 |
+
return f"File saved as {save_filename}", save_path
|
47 |
+
except Exception as e:
|
48 |
+
return f"Error saving file: {str(e)}", None
|
49 |
+
|
50 |
+
# Create an instance of JSONEditor
|
51 |
+
json_editor = JSONEditor()
|
52 |
+
|
53 |
+
def create_interface():
|
54 |
+
"""Create Gradio interface"""
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
# File upload component
|
57 |
+
file_input = gr.File(file_count="single", type="file",
|
58 |
+
file_types=['.json'],
|
59 |
+
label="Upload JSON File")
|
60 |
+
|
61 |
+
# Status message area
|
62 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
63 |
+
|
64 |
+
# JSON text area for editing
|
65 |
+
json_text = gr.Textbox(label="JSON Content",
|
66 |
+
lines=20,
|
67 |
+
interactive=True)
|
68 |
+
|
69 |
+
# Buttons for actions
|
70 |
+
with gr.Row():
|
71 |
+
load_btn = gr.Button("Load File")
|
72 |
+
update_btn = gr.Button("Update JSON")
|
73 |
+
save_btn = gr.Button("Save File")
|
74 |
+
|
75 |
+
# File download component
|
76 |
+
file_output = gr.File(label="Download Edited JSON")
|
77 |
+
|
78 |
+
# Event handlers
|
79 |
+
load_btn.click(
|
80 |
+
fn=json_editor.load_json,
|
81 |
+
inputs=file_input,
|
82 |
+
outputs=[json_text, status_output]
|
83 |
+
)
|
84 |
+
|
85 |
+
update_btn.click(
|
86 |
+
fn=json_editor.update_json,
|
87 |
+
inputs=json_text,
|
88 |
+
outputs=[json_text, status_output]
|
89 |
+
)
|
90 |
+
|
91 |
+
save_btn.click(
|
92 |
+
fn=json_editor.save_json,
|
93 |
+
inputs=None,
|
94 |
+
outputs=[status_output, file_output]
|
95 |
+
)
|
96 |
+
|
97 |
+
return demo
|
98 |
+
|
99 |
+
# Launch the Gradio app
|
100 |
+
if __name__ == "__main__":
|
101 |
+
interface = create_interface()
|
102 |
+
interface.launch()
|
103 |
+
|
104 |
+
# Requirements for Hugging Face Spaces:
|
105 |
+
# - gradio
|
106 |
+
# - Requirements file would look like:
|
107 |
+
# gradio>=3.50.0
|
108 |
+
|
109 |
+
"""
|
110 |
+
Hugging Face Spaces Deployment Instructions:
|
111 |
+
1. Create a new Space
|
112 |
+
2. Choose Python as the SDK
|
113 |
+
3. Select Gradio as the Space SDK
|
114 |
+
4. Upload this script as app.py
|
115 |
+
5. Create a requirements.txt with the gradio dependency
|
116 |
+
6. Make sure to set the Space visibility as you prefer
|
117 |
+
|
118 |
+
Recommended Space Configuration:
|
119 |
+
- Hardware: CPU Basic
|
120 |
+
- Space SDK: Gradio
|
121 |
+
- Python Version: 3.10+
|
122 |
+
"""
|