akera commited on
Commit
e764015
Β·
verified Β·
1 Parent(s): 745019f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -159,33 +159,45 @@ def download_test_set() -> Tuple[str, str]:
159
  error_msg = f"❌ Error creating test set download: {str(e)}"
160
  return None, error_msg
161
 
 
162
  def validate_submission(file, model_name: str, author: str, description: str) -> Tuple[str, Optional[pd.DataFrame]]:
163
- """Validate uploaded prediction file, handling both file-like and NamedString inputs."""
164
  try:
165
  if file is None:
166
  return "❌ Please upload a predictions file", None
167
  if not model_name.strip():
168
  return "❌ Please provide a model name", None
169
 
170
- # Read raw bytes from the upload
171
- try:
172
- file_content = file.read()
173
- except AttributeError:
174
- # Fallback for Gradio NamedString
175
- data = getattr(file, "data", None) or getattr(file, "value", None)
176
- if data is None:
177
- return "❌ Could not read uploaded file", None
178
- file_content = data.encode("utf-8") if isinstance(data, str) else data
 
 
 
 
 
 
 
179
 
180
- # Determine filename
181
- filename = getattr(file, "name", None) or getattr(file, "filename", None) or "uploaded_file"
 
 
 
 
182
 
183
- # Load complete test set
184
  global complete_test_set
185
  if complete_test_set is None:
186
  complete_test_set = get_complete_test_set()
187
 
188
- # Run validation
189
  validation_result = validate_submission_complete(
190
  file_content, filename, complete_test_set, model_name
191
  )
@@ -196,11 +208,11 @@ def validate_submission(file, model_name: str, author: str, description: str) ->
196
  return validation_result["report"], None
197
 
198
  except Exception as e:
199
- error_msg = (
200
- f"❌ Validation error: {str(e)}\n\nTraceback:\n"
201
- + traceback.format_exc()
202
  )
203
- return error_msg, None
204
 
205
 
206
  def evaluate_submission(
 
159
  error_msg = f"❌ Error creating test set download: {str(e)}"
160
  return None, error_msg
161
 
162
+
163
  def validate_submission(file, model_name: str, author: str, description: str) -> Tuple[str, Optional[pd.DataFrame]]:
164
+ """Validate uploaded prediction file, supporting str paths, bytes, and Gradio wrappers."""
165
  try:
166
  if file is None:
167
  return "❌ Please upload a predictions file", None
168
  if not model_name.strip():
169
  return "❌ Please provide a model name", None
170
 
171
+ # 1) Determine raw bytes
172
+ if isinstance(file, bytes):
173
+ file_content = file
174
+ elif isinstance(file, str):
175
+ # could be a path or raw text
176
+ if os.path.exists(file):
177
+ with open(file, "rb") as f:
178
+ file_content = f.read()
179
+ else:
180
+ file_content = file.encode("utf-8")
181
+ elif hasattr(file, "name") and os.path.exists(file.name):
182
+ # tempfile._TemporaryFileWrapper from Gradio
183
+ with open(file.name, "rb") as f:
184
+ file_content = f.read()
185
+ else:
186
+ return "❌ Could not read uploaded file", None
187
 
188
+ # 2) Infer filename for format-sniffing
189
+ filename = (
190
+ getattr(file, "name", None)
191
+ or getattr(file, "filename", None)
192
+ or "predictions.csv"
193
+ )
194
 
195
+ # 3) Load test set if needed
196
  global complete_test_set
197
  if complete_test_set is None:
198
  complete_test_set = get_complete_test_set()
199
 
200
+ # 4) Run existing validation pipeline
201
  validation_result = validate_submission_complete(
202
  file_content, filename, complete_test_set, model_name
203
  )
 
208
  return validation_result["report"], None
209
 
210
  except Exception as e:
211
+ return (
212
+ f"❌ Validation error: {e}\n\nTraceback:\n{traceback.format_exc()}",
213
+ None,
214
  )
215
+
216
 
217
 
218
  def evaluate_submission(