naonauno commited on
Commit
fa6c29b
·
verified ·
1 Parent(s): 08ae12f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -10,10 +10,11 @@ class JSONEditor:
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)}"
@@ -38,7 +39,7 @@ class JSONEditor:
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)
@@ -53,8 +54,8 @@ json_editor = JSONEditor()
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
 
@@ -99,7 +100,7 @@ def create_interface():
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
 
10
  def load_json(self, file):
11
  """Load JSON file and return its contents"""
12
  try:
13
+ # Use filepath instead of file object
14
+ with open(file, 'r', encoding='utf-8') as f:
15
  data = json.load(f)
16
  self.current_data = data
17
+ self.current_filename = os.path.basename(file)
18
  return json.dumps(data, indent=2), f"Successfully loaded {self.current_filename}"
19
  except Exception as e:
20
  return "", f"Error loading file: {str(e)}"
 
39
  save_filename = self.current_filename or "edited_data.json"
40
 
41
  # Ensure the file is saved in the current working directory
42
+ save_path = os.path.join(os.getcwd(), save_filename)
43
 
44
  with open(save_path, 'w', encoding='utf-8') as f:
45
  json.dump(self.current_data, f, indent=2)
 
54
  def create_interface():
55
  """Create Gradio interface"""
56
  with gr.Blocks() as demo:
57
+ # File upload component - changed to filepath
58
+ file_input = gr.File(file_count="single", type="filepath",
59
  file_types=['.json'],
60
  label="Upload JSON File")
61
 
 
100
  # Launch the Gradio app
101
  if __name__ == "__main__":
102
  interface = create_interface()
103
+ interface.launch(server_name="0.0.0.0", server_port=7860)
104
 
105
  # Requirements for Hugging Face Spaces:
106
  # - gradio