Aaron Mueller commited on
Commit
e7e9a2c
·
1 Parent(s): df72705

fix upload for JSONs

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -81,17 +81,26 @@ def init_leaderboard(dataframe, track):
81
  interactive=False,
82
  )
83
 
 
84
  def process_json(temp_file):
85
- if isinstance(temp_file, str):
86
- obj = json.loads(temp_file)
87
- else:
88
- try:
89
- with gzip.open(temp_file, 'rt') as header:
90
- obj = json.loads(header)
91
- except:
92
- with open(temp_file, 'r') as header:
93
- obj = json.loads(header)
94
- return obj
 
 
 
 
 
 
 
 
95
 
96
 
97
  demo = gr.Blocks(css=custom_css)
@@ -165,9 +174,14 @@ with demo:
165
  interactive=True
166
  )
167
 
168
- upload_button = gr.UploadButton(label="Upload predictions", file_types = ['.json', '.json.gz'], file_count = "single")
169
- predictions = {}
170
- upload_button.upload(fn=process_json, inputs=upload_button, outputs=predictions, api_name="upload_json")
 
 
 
 
 
171
 
172
  submit_button = gr.Button("Submit Eval")
173
  submission_result = gr.Markdown()
 
81
  interactive=False,
82
  )
83
 
84
+ submitted_predictions = {}
85
  def process_json(temp_file):
86
+ if temp_file is None:
87
+ return {}
88
+
89
+ # Handle file upload
90
+ try:
91
+ file_path = temp_file.name
92
+ if file_path.endswith('.gz'):
93
+ with gzip.open(file_path, 'rt') as f:
94
+ data = json.load(f)
95
+ submitted_predictions.update(data)
96
+ else:
97
+ with open(file_path, 'r') as f:
98
+ data = json.load(f)
99
+ submitted_predictions.update(data)
100
+ except Exception as e:
101
+ raise gr.Error(f"Error processing file: {str(e)}")
102
+
103
+ return data
104
 
105
 
106
  demo = gr.Blocks(css=custom_css)
 
174
  interactive=True
175
  )
176
 
177
+ upload_button = gr.UploadButton(label="Upload predictions", file_types=[".json", ".gz"], file_count="single")
178
+ output_json = gr.JSON(label="Processed JSON")
179
+ upload_button.upload(
180
+ fn=process_json,
181
+ inputs=upload_button,
182
+ outputs=output_json,
183
+ api_name="upload_json"
184
+ )
185
 
186
  submit_button = gr.Button("Submit Eval")
187
  submission_result = gr.Markdown()