Madhavan Iyengar commited on
Commit
42a6f9c
1 Parent(s): e26fb5f

add more robust error checking

Browse files
Files changed (1) hide show
  1. app.py +75 -42
app.py CHANGED
@@ -33,52 +33,85 @@ from src.envs import API, EVAL_REQUESTS_PATH, EVAL_RESULTS_PATH, QUEUE_REPO, REP
33
  from src.populate import get_evaluation_queue_df, get_leaderboard_df
34
  from src.submission.submit import add_new_eval
35
  from src.submission.evaluate import calculate_metrics
 
36
 
37
- def handle_new_eval_submission(model_name, model_zip, model_link=None):
38
-
39
- if not model_name:
40
- return "Please enter a model name."
41
-
42
- # check if the model name is already in the leaderboard
43
- if model_name in leaderboard_df[AutoEvalColumn.model.name].values:
44
- return "Model name already exists in the leaderboard. Please choose a different name."
45
-
46
- # check if the zip file is actually a zip file
47
- if model_zip is None or not zipfile.is_zipfile(model_zip):
48
- return "Please upload a valid zip file."
49
-
50
- # check if the model name is only one word
51
- if len(model_name.split()) > 1:
52
- return "Model name should be a single word with hyphens."
53
-
54
- extraction_path = EVAL_RESULTS_PATH_BACKEND
55
-
56
- if not os.path.exists(extraction_path):
57
- os.makedirs(extraction_path)
58
 
59
- # define path for the zip file to be extracted to
60
- extraction_path = os.path.join(extraction_path, model_name)
61
-
62
- if model_zip is not None:
63
- with zipfile.ZipFile(model_zip, 'r') as zip_ref:
64
- zip_ref.extractall(extraction_path)
65
- print("File unzipped successfully to:", extraction_path)
66
 
67
- # Evaluate the model's performance
68
- calculate_metrics(extraction_path, model_name)
69
-
70
- # upload to results repo
71
- API.upload_file(
72
- path_or_fileobj=os.path.join(os.getcwd(), EVAL_RESULTS_PATH, '3d-pope', model_name, 'results.json'),
73
- path_in_repo=os.path.join('3d-pope', model_name, 'results.json'),
74
- repo_id=RESULTS_REPO,
75
- repo_type="dataset",
76
- )
77
-
78
- restart_space()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- return "Submission received and results are being processed. Please check the leaderboard for updates."
81
-
82
 
83
 
84
  def restart_space():
 
33
  from src.populate import get_evaluation_queue_df, get_leaderboard_df
34
  from src.submission.submit import add_new_eval
35
  from src.submission.evaluate import calculate_metrics
36
+ import json
37
 
38
+ def handle_new_eval_submission(model_name, model_zip, model_link=None) -> str:
39
+ try:
40
+ # Input validation
41
+ if not model_name:
42
+ return "Please enter a model name."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ if not isinstance(model_name, str):
45
+ return "Model name must be a string."
 
 
 
 
 
46
 
47
+ if len(model_name.split()) > 1:
48
+ return "Model name should be a single word with hyphens."
49
+
50
+ # Check if the model name is already in the leaderboard
51
+ if model_name in leaderboard_df[AutoEvalColumn.model.name].values:
52
+ return "Model name already exists in the leaderboard. Please choose a different name."
53
+
54
+ if model_zip is None:
55
+ return "Please provide a zip file."
56
+
57
+ extraction_path = os.path.join(EVAL_RESULTS_PATH_BACKEND, model_name)
58
+
59
+ if model_zip is not None:
60
+ # Check if the zip file is actually a zip file
61
+ if not zipfile.is_zipfile(model_zip):
62
+ return "Please upload a valid zip file."
63
+
64
+ # Create extraction path if it doesn't exist
65
+ os.makedirs(extraction_path, exist_ok=True)
66
+
67
+ # Extract the zip file
68
+ try:
69
+ with zipfile.ZipFile(model_zip, 'r') as zip_ref:
70
+ zip_ref.extractall(extraction_path)
71
+ except zipfile.BadZipFile:
72
+ return "The uploaded file is not a valid zip file."
73
+ except Exception as e:
74
+ return f"An error occurred while extracting the zip file: {str(e)}"
75
+
76
+ print("File unzipped successfully to:", extraction_path)
77
+
78
+ # Evaluate the model's performance
79
+ try:
80
+ calculate_metrics(extraction_path, model_name)
81
+ except Exception as e:
82
+ return f"An error occurred while calculating metrics: {str(e)}"
83
+
84
+ # Upload results to repo
85
+ results_file_path = os.path.join(os.getcwd(), EVAL_RESULTS_PATH, '3d-pope', model_name, 'results.json')
86
+ if not os.path.exists(results_file_path):
87
+ return f"Results file not found at {results_file_path}"
88
+
89
+ try:
90
+ with open(results_file_path, 'r') as f:
91
+ json.load(f) # Validate JSON structure
92
+ except json.JSONDecodeError:
93
+ return "The results file is not a valid JSON file."
94
+
95
+ try:
96
+ API.upload_file(
97
+ path_or_fileobj=results_file_path,
98
+ path_in_repo=os.path.join('3d-pope', model_name, 'results.json'),
99
+ repo_id=RESULTS_REPO,
100
+ repo_type="dataset",
101
+ )
102
+ except Exception as e:
103
+ return f"An error occurred while uploading results: {str(e)}"
104
+
105
+ # Restart the space
106
+ try:
107
+ restart_space()
108
+ except Exception as e:
109
+ return f"An error occurred while restarting the space: {str(e)}"
110
+
111
+ return "Submission received and results are being processed. Please check the leaderboard for updates."
112
 
113
+ except Exception as e:
114
+ return f"An unexpected error occurred: {str(e)}"
115
 
116
 
117
  def restart_space():