linzhengyu commited on
Commit
6f2fa1b
·
1 Parent(s): d1fd905

feat: allow submit eval to backend, allow json file upload

Browse files
app.py CHANGED
@@ -38,7 +38,7 @@ from src.display.utils import (
38
  )
39
  from src.envs import API, settings
40
  from src.populate import get_evaluation_queue_df, get_leaderboard_df
41
- from src.submission.submit import add_new_eval
42
 
43
 
44
  def restart_space():
@@ -347,13 +347,6 @@ def main():
347
  with gr.Column():
348
  model_name_textbox = gr.Textbox(label="Model name", placeholder="user/model_name")
349
  revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
350
- model_type = gr.Dropdown(
351
- choices=[t.to_str(" : ") for t in ModelType if t != ModelType.Unknown],
352
- label="Model type",
353
- multiselect=False,
354
- value=None,
355
- interactive=True,
356
- )
357
 
358
  def search_models(query):
359
  if not query.strip():
@@ -373,6 +366,44 @@ def main():
373
  search_name.change(fn=search_models, inputs=search_name, outputs=table)
374
  table.select(fn=on_select, inputs=table, outputs=model_name_textbox)
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  with gr.Column():
377
  precision = gr.Dropdown(
378
  choices=[i.value.name for i in Precision if i != Precision.Unknown],
@@ -389,18 +420,20 @@ def main():
389
  interactive=True,
390
  )
391
  base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
 
392
 
393
  submit_button = gr.Button("Submit Eval")
394
  submission_result = gr.Markdown()
395
  submit_button.click(
396
- add_new_eval,
397
  [
398
  model_name_textbox,
399
  base_model_name_textbox,
400
  revision_name_textbox,
401
  precision,
402
  weight_type,
403
- model_type,
 
404
  ],
405
  submission_result,
406
  )
 
38
  )
39
  from src.envs import API, settings
40
  from src.populate import get_evaluation_queue_df, get_leaderboard_df
41
+ from src.submission.submit import add_new_submit
42
 
43
 
44
  def restart_space():
 
347
  with gr.Column():
348
  model_name_textbox = gr.Textbox(label="Model name", placeholder="user/model_name")
349
  revision_name_textbox = gr.Textbox(label="Revision commit", placeholder="main")
 
 
 
 
 
 
 
350
 
351
  def search_models(query):
352
  if not query.strip():
 
366
  search_name.change(fn=search_models, inputs=search_name, outputs=table)
367
  table.select(fn=on_select, inputs=table, outputs=model_name_textbox)
368
 
369
+ def file_to_json_str(file) -> str:
370
+ """
371
+ 读取上传的 JSON 文件并返回字符串
372
+ 如果解析失败,抛出异常以阻止上传成功
373
+ """
374
+ if file is None:
375
+ return ""
376
+ try:
377
+ import json
378
+ # file 是文件路径字符串(当 type="filepath" 时)
379
+ file_path = file if isinstance(file, str) else file.name
380
+ with open(file_path, 'r', encoding='utf-8') as f:
381
+ json_data = json.load(f)
382
+ # 将 JSON 对象转换为格式化的字符串
383
+ json_str = json.dumps(json_data, indent=2, ensure_ascii=False)
384
+ return json_str
385
+ except Exception as e:
386
+ raise gr.Error(f"Error reading JSON file: {str(e)}")
387
+
388
+ json_upload = gr.File(
389
+ label="Upload JSON file",
390
+ file_types=[".json"],
391
+ type="filepath",
392
+ )
393
+ json_str = gr.Textbox(
394
+ label="JSON Content",
395
+ placeholder="JSON content will appear here after upload",
396
+ lines=10,
397
+ interactive=True,
398
+ visible=False,
399
+ )
400
+ json_upload.upload(
401
+ fn=file_to_json_str,
402
+ inputs=json_upload,
403
+ outputs=json_str,
404
+ )
405
+
406
+
407
  with gr.Column():
408
  precision = gr.Dropdown(
409
  choices=[i.value.name for i in Precision if i != Precision.Unknown],
 
420
  interactive=True,
421
  )
422
  base_model_name_textbox = gr.Textbox(label="Base model (for delta or adapter weights)")
423
+ commit_textbox = gr.Textbox(label="Commits")
424
 
425
  submit_button = gr.Button("Submit Eval")
426
  submission_result = gr.Markdown()
427
  submit_button.click(
428
+ add_new_submit,
429
  [
430
  model_name_textbox,
431
  base_model_name_textbox,
432
  revision_name_textbox,
433
  precision,
434
  weight_type,
435
+ json_str,
436
+ commit_textbox,
437
  ],
438
  submission_result,
439
  )
src/assets/css/custom.css CHANGED
@@ -14,7 +14,7 @@
14
  font-size: 16px !important;
15
  }
16
 
17
- #citation-button > label > button {
18
  margin: 6px;
19
  transform: scale(1.3);
20
  }
@@ -27,7 +27,7 @@
27
  margin-top: 15px
28
  }
29
 
30
- #search-bar-table-box > div:first-child {
31
  background: none;
32
  border: none;
33
  }
@@ -60,36 +60,45 @@
60
  #scale-logo .download {
61
  display: none;
62
  }
63
- #filter_type{
 
64
  border: 0;
65
  padding-left: 0;
66
  padding-top: 0;
67
  }
 
68
  #filter_type label {
69
  display: flex;
70
  }
71
- #filter_type label > span{
 
72
  margin-top: var(--spacing-lg);
73
  margin-right: 0.5em;
74
  }
75
- #filter_type label > .wrap{
 
76
  width: 103px;
77
  }
78
- #filter_type label > .wrap .wrap-inner{
 
79
  padding: 2px;
80
  }
81
- #filter_type label > .wrap .wrap-inner input{
 
82
  width: 1px
83
  }
84
- #filter-columns-type{
85
- border:0;
86
- padding:0.5;
 
87
  }
88
- #filter-columns-size{
89
- border:0;
90
- padding:0.5;
 
91
  }
92
- #box-filter > .form{
 
93
  border: 0
94
  }
95
 
@@ -104,4 +113,4 @@
104
  white-space: nowrap !important;
105
  overflow: hidden !important;
106
  text-overflow: ellipsis !important;
107
- }
 
14
  font-size: 16px !important;
15
  }
16
 
17
+ #citation-button>label>button {
18
  margin: 6px;
19
  transform: scale(1.3);
20
  }
 
27
  margin-top: 15px
28
  }
29
 
30
+ #search-bar-table-box>div:first-child {
31
  background: none;
32
  border: none;
33
  }
 
60
  #scale-logo .download {
61
  display: none;
62
  }
63
+
64
+ #filter_type {
65
  border: 0;
66
  padding-left: 0;
67
  padding-top: 0;
68
  }
69
+
70
  #filter_type label {
71
  display: flex;
72
  }
73
+
74
+ #filter_type label>span {
75
  margin-top: var(--spacing-lg);
76
  margin-right: 0.5em;
77
  }
78
+
79
+ #filter_type label>.wrap {
80
  width: 103px;
81
  }
82
+
83
+ #filter_type label>.wrap .wrap-inner {
84
  padding: 2px;
85
  }
86
+
87
+ #filter_type label>.wrap .wrap-inner input {
88
  width: 1px
89
  }
90
+
91
+ #filter-columns-type {
92
+ border: 0;
93
+ padding: 0.5;
94
  }
95
+
96
+ #filter-columns-size {
97
+ border: 0;
98
+ padding: 0.5;
99
  }
100
+
101
+ #box-filter>.form {
102
  border: 0
103
  }
104
 
 
113
  white-space: nowrap !important;
114
  overflow: hidden !important;
115
  text-overflow: ellipsis !important;
116
+ }
src/backend/routes/hf.py CHANGED
@@ -95,7 +95,7 @@ async def upload_file_content_handler(
95
 
96
  @router.post("/community/submit/")
97
  async def community_submit(
98
- params: Annotated[CommunitySubmit_Params, Depends()],
99
  ) -> ResponseData[UploadFileContent_RespData]:
100
  """Submit a new evaluation request to the Hugging Face repository."""
101
  file_obj = io.BytesIO(params.content.encode("utf-8"))
@@ -107,4 +107,5 @@ async def community_submit(
107
  repo_id=settings.REQUESTS_REPO_ID,
108
  repo_type="dataset",
109
  )
110
- return ResponseData(data=data)
 
 
95
 
96
  @router.post("/community/submit/")
97
  async def community_submit(
98
+ params: CommunitySubmit_Params,
99
  ) -> ResponseData[UploadFileContent_RespData]:
100
  """Submit a new evaluation request to the Hugging Face repository."""
101
  file_obj = io.BytesIO(params.content.encode("utf-8"))
 
107
  repo_id=settings.REQUESTS_REPO_ID,
108
  repo_type="dataset",
109
  )
110
+ print(params)
111
+ return ResponseData(data=None)
src/display/utils.py CHANGED
@@ -144,6 +144,13 @@ class WeightType(Enum):
144
  class Precision(Enum):
145
  float16 = ModelDetails(name="float16")
146
  bfloat16 = ModelDetails(name="bfloat16")
 
 
 
 
 
 
 
147
  Unknown = ModelDetails(name="?")
148
 
149
  @classmethod
@@ -152,6 +159,20 @@ class Precision(Enum):
152
  return Precision.float16
153
  if precision in ["torch.bfloat16", "bfloat16"]:
154
  return Precision.bfloat16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  return Precision.Unknown
156
 
157
 
 
144
  class Precision(Enum):
145
  float16 = ModelDetails(name="float16")
146
  bfloat16 = ModelDetails(name="bfloat16")
147
+ float32 = ModelDetails(name="float32")
148
+ float64 = ModelDetails(name="float64")
149
+ int8 = ModelDetails(name="int8")
150
+ uint8 = ModelDetails(name="uint8")
151
+ int16 = ModelDetails(name="int16")
152
+ int32 = ModelDetails(name="int32")
153
+ int64 = ModelDetails(name="int64")
154
  Unknown = ModelDetails(name="?")
155
 
156
  @classmethod
 
159
  return Precision.float16
160
  if precision in ["torch.bfloat16", "bfloat16"]:
161
  return Precision.bfloat16
162
+ if precision in ["torch.float32", "float32"]:
163
+ return Precision.float32
164
+ if precision in ["torch.float64", "float64"]:
165
+ return Precision.float64
166
+ if precision in ["torch.int8", "int8"]:
167
+ return Precision.int8
168
+ if precision in ["torch.uint8", "uint8"]:
169
+ return Precision.uint8
170
+ if precision in ["torch.int16", "int16"]:
171
+ return Precision.int16
172
+ if precision in ["torch.int32", "int32"]:
173
+ return Precision.int32
174
+ if precision in ["torch.int64", "int64"]:
175
+ return Precision.int64
176
  return Precision.Unknown
177
 
178
 
src/submission/submit.py CHANGED
@@ -2,6 +2,8 @@ import json
2
  import os
3
  import sys
4
  from datetime import datetime, timezone
 
 
5
 
6
  from src.display.formatting import styled_error, styled_message, styled_warning
7
  from src.envs import API, settings
@@ -20,6 +22,93 @@ else:
20
  REQUESTED_MODELS = None
21
  USERS_TO_SUBMISSION_DATES = None
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def add_new_eval(
25
  model: str,
 
2
  import os
3
  import sys
4
  from datetime import datetime, timezone
5
+ import requests
6
+ import json
7
 
8
  from src.display.formatting import styled_error, styled_message, styled_warning
9
  from src.envs import API, settings
 
22
  REQUESTED_MODELS = None
23
  USERS_TO_SUBMISSION_DATES = None
24
 
25
+ def add_new_submit(
26
+ model: str,
27
+ base_model: str,
28
+ revision: str,
29
+ precision: str,
30
+ weight_type: str,
31
+ json_str: str,
32
+ commit_message: str
33
+ ):
34
+ global REQUESTED_MODELS
35
+ global USERS_TO_SUBMISSION_DATES
36
+ if not REQUESTED_MODELS:
37
+ REQUESTED_MODELS, USERS_TO_SUBMISSION_DATES = already_submitted_models(settings.EVAL_REQUESTS_PATH)
38
+
39
+ user_name = ""
40
+ model_path = model
41
+ if "/" in model:
42
+ user_name = model.split("/")[0]
43
+ model_path = model.split("/")[1]
44
+
45
+ precision = precision.split(" ")[0]
46
+ # Does the model actually exist?
47
+ if revision == "":
48
+ revision = "main"
49
+
50
+ # Is the model on the hub?
51
+ if weight_type in ["Delta", "Adapter"]:
52
+ base_model_on_hub, error, _ = is_model_on_hub(
53
+ model_name=base_model, revision=revision, token=settings.TOKEN, test_tokenizer=True
54
+ )
55
+ if not base_model_on_hub:
56
+ return styled_error(f'Base model "{base_model}" {error}')
57
+
58
+ if not weight_type == "Adapter":
59
+ model_on_hub, error, _ = is_model_on_hub(
60
+ model_name=model, revision=revision, token=settings.TOKEN, test_tokenizer=True
61
+ )
62
+ if not model_on_hub:
63
+ return styled_error(f'Model "{model}" {error}')
64
+
65
+ # Is the model info correctly filled?
66
+ try:
67
+ model_info = API.model_info(repo_id=model, revision=revision)
68
+ except Exception:
69
+ return styled_error("Could not get your model information. Please fill it up properly.")
70
+
71
+ # Were the model card and license filled?
72
+ try:
73
+ license = model_info.cardData["license"]
74
+ except Exception:
75
+ return styled_error("Please select a license for your model")
76
+
77
+ request_json = {
78
+ "username": user_name,
79
+ "model_id": model,
80
+ "model_sha": revision,
81
+ "model_dtype": precision,
82
+ "content": json_str,
83
+ "weight_type": weight_type,
84
+ "commit_message":commit_message
85
+ }
86
+
87
+ # Check for duplicate submission
88
+ if f"{model}_{revision}_{precision}" in REQUESTED_MODELS:
89
+ return styled_warning("This model has been already submitted.")
90
+
91
+ try:
92
+ response = requests.post(
93
+ url=f"http://localhost:{settings.BACKEND_PORT}/api/v1/hf/community/submit/",
94
+ json=request_json, # 使用 json 参数发送 JSON body
95
+ headers={"Content-Type": "application/json"}
96
+ )
97
+ print(request_json)
98
+ print(response.json())
99
+ if response.status_code == 200:
100
+ data = response.json()
101
+ if data.get("code") == 0:
102
+ return styled_message(
103
+ "Your request has been submitted to the evaluation queue!\nPlease wait for the model to show in the PENDING list."
104
+ )
105
+ return styled_error(
106
+ "Submission unsuccessful."
107
+ )
108
+ except Exception:
109
+ return styled_error(
110
+ "Submission unsuccessful."
111
+ )
112
 
113
  def add_new_eval(
114
  model: str,