Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,15 @@
|
|
| 1 |
-
########################################
|
| 2 |
-
# Gradio schema patch (optional)
|
| 3 |
-
# Place this at the very top to avoid "bool not iterable" issues
|
| 4 |
-
########################################
|
| 5 |
import gradio_client.utils as gc_utils
|
| 6 |
|
| 7 |
_original_json_schema_to_python_type = gc_utils._json_schema_to_python_type
|
| 8 |
|
| 9 |
def patched_json_schema_to_python_type(schema, defs=None):
|
| 10 |
if isinstance(schema, bool):
|
| 11 |
-
# If we find a boolean schema (e.g. additionalProperties: false), return {}
|
| 12 |
return {}
|
| 13 |
return _original_json_schema_to_python_type(schema, defs)
|
| 14 |
|
| 15 |
gc_utils._json_schema_to_python_type = patched_json_schema_to_python_type
|
| 16 |
|
| 17 |
-
|
| 18 |
-
# Standard imports
|
| 19 |
-
########################################
|
| 20 |
import logging
|
| 21 |
import os
|
| 22 |
os.makedirs("tmp", exist_ok=True)
|
|
@@ -30,7 +23,6 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|
| 30 |
import json
|
| 31 |
from io import BytesIO
|
| 32 |
|
| 33 |
-
# Additional imports from your code
|
| 34 |
from src.radial.radial import create_plot
|
| 35 |
from gradio_leaderboard import Leaderboard, SelectColumns
|
| 36 |
from gradio_space_ci import enable_space_ci
|
|
@@ -41,22 +33,18 @@ from src.envs import API, H4_TOKEN, HF_HOME, REPO_ID, RESET_JUDGEMENT_ENV
|
|
| 41 |
from src.leaderboard.build_leaderboard import build_leadearboard_df, download_openbench, download_dataset
|
| 42 |
import huggingface_hub
|
| 43 |
|
| 44 |
-
# huggingface_hub.login(token=H4_TOKEN)
|
| 45 |
|
| 46 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "false"
|
| 47 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 48 |
enable_space_ci()
|
| 49 |
|
| 50 |
-
|
| 51 |
-
# File handlers
|
| 52 |
-
########################################
|
| 53 |
def handle_file_upload(file_bytes):
|
| 54 |
"""
|
| 55 |
Read the uploaded bytes and parse JSON directly,
|
| 56 |
avoiding ephemeral disk paths or file read issues.
|
| 57 |
"""
|
| 58 |
logging.info("File uploaded (bytes). Size: %d bytes", len(file_bytes))
|
| 59 |
-
# Convert from bytes to JSON (assuming valid JSON input).
|
| 60 |
v = json.loads(file_bytes.decode("utf-8"))
|
| 61 |
return v
|
| 62 |
|
|
@@ -67,7 +55,6 @@ def submit_file(v, mn):
|
|
| 67 |
"""
|
| 68 |
print('START SUBMITTING!!!')
|
| 69 |
|
| 70 |
-
# Validate that 'results' exists in v
|
| 71 |
if 'results' not in v:
|
| 72 |
return "Invalid JSON: missing 'results' key"
|
| 73 |
|
|
@@ -82,14 +69,12 @@ def submit_file(v, mn):
|
|
| 82 |
]
|
| 83 |
|
| 84 |
for column in columns:
|
| 85 |
-
# Validate data structure
|
| 86 |
if column not in new_file or not isinstance(new_file[column], dict):
|
| 87 |
return f"Missing or invalid column: {column}"
|
| 88 |
if 'acc,none' not in new_file[column]:
|
| 89 |
return f"Missing 'acc,none' key in column: {column}"
|
| 90 |
new_file[column] = new_file[column]['acc,none']
|
| 91 |
|
| 92 |
-
# Validate 'config'
|
| 93 |
if 'config' not in v or 'model_dtype' not in v['config']:
|
| 94 |
return "Missing 'config' or 'model_dtype' in JSON"
|
| 95 |
|
|
@@ -98,10 +83,9 @@ def submit_file(v, mn):
|
|
| 98 |
|
| 99 |
print('WE READ FILE: ', new_file)
|
| 100 |
|
| 101 |
-
# Convert to JSON and upload
|
| 102 |
buf = BytesIO()
|
| 103 |
buf.write(json.dumps(new_file).encode('utf-8'))
|
| 104 |
-
buf.seek(0)
|
| 105 |
API.upload_file(
|
| 106 |
path_or_fileobj=buf,
|
| 107 |
path_in_repo="model_data/external/" + mn.replace('/', '__') + ".json",
|
|
@@ -112,9 +96,7 @@ def submit_file(v, mn):
|
|
| 112 |
os.environ[RESET_JUDGEMENT_ENV] = "1"
|
| 113 |
return "Success!"
|
| 114 |
|
| 115 |
-
|
| 116 |
-
# Utility functions
|
| 117 |
-
########################################
|
| 118 |
def restart_space():
|
| 119 |
API.restart_space(repo_id=REPO_ID)
|
| 120 |
download_openbench()
|
|
@@ -122,9 +104,7 @@ def restart_space():
|
|
| 122 |
def update_plot(selected_models):
|
| 123 |
return create_plot(selected_models)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
# Build Gradio app
|
| 127 |
-
########################################
|
| 128 |
def build_demo():
|
| 129 |
download_openbench()
|
| 130 |
demo = gr.Blocks(title="Kaz LLM LB", css=custom_css)
|
|
@@ -155,13 +135,11 @@ def build_demo():
|
|
| 155 |
with gr.Column():
|
| 156 |
model_name_textbox = gr.Textbox(label="Model name")
|
| 157 |
|
| 158 |
-
# Use 'bytes' so we load file content in memory.
|
| 159 |
file_output = gr.File(
|
| 160 |
label="Drag and drop JSON file judgment here",
|
| 161 |
type="binary"
|
| 162 |
)
|
| 163 |
|
| 164 |
-
# We'll store the returned JSON object in uploaded_file (NOT the path).
|
| 165 |
uploaded_file = gr.State()
|
| 166 |
|
| 167 |
with gr.Row():
|
|
@@ -170,14 +148,12 @@ def build_demo():
|
|
| 170 |
|
| 171 |
submit_button = gr.Button("Submit File", variant='primary')
|
| 172 |
|
| 173 |
-
# On file upload, parse JSON -> store in uploaded_file
|
| 174 |
file_output.upload(
|
| 175 |
fn=handle_file_upload,
|
| 176 |
inputs=file_output,
|
| 177 |
outputs=uploaded_file
|
| 178 |
)
|
| 179 |
|
| 180 |
-
# On button click, call submit_file with the stored JSON + model name
|
| 181 |
submit_button.click(
|
| 182 |
fn=submit_file,
|
| 183 |
inputs=[uploaded_file, model_name_textbox],
|
|
@@ -202,13 +178,9 @@ def build_demo():
|
|
| 202 |
)
|
| 203 |
return demo
|
| 204 |
|
| 205 |
-
########################################
|
| 206 |
-
# Aggregation and scheduling
|
| 207 |
-
########################################
|
| 208 |
def aggregate_leaderboard_data():
|
| 209 |
download_dataset("kz-transformers/s-openbench-eval", "m_data")
|
| 210 |
|
| 211 |
-
# Start with your baseline data
|
| 212 |
data_list = [
|
| 213 |
{
|
| 214 |
"model_dtype": "torch.float16",
|
|
@@ -379,9 +351,7 @@ def update_board_():
|
|
| 379 |
logging.info("Updating the judgement at startup")
|
| 380 |
aggregate_leaderboard_data()
|
| 381 |
|
| 382 |
-
|
| 383 |
-
# Main
|
| 384 |
-
########################################
|
| 385 |
if __name__ == "__main__":
|
| 386 |
os.environ[RESET_JUDGEMENT_ENV] = "1"
|
| 387 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
@@ -391,5 +361,4 @@ if __name__ == "__main__":
|
|
| 391 |
scheduler.start()
|
| 392 |
|
| 393 |
demo_app = build_demo()
|
| 394 |
-
# Don't pass root_path on HF Spaces. Let it mount at default "/"
|
| 395 |
demo_app.launch(debug=True, share=False, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio_client.utils as gc_utils
|
| 2 |
|
| 3 |
_original_json_schema_to_python_type = gc_utils._json_schema_to_python_type
|
| 4 |
|
| 5 |
def patched_json_schema_to_python_type(schema, defs=None):
|
| 6 |
if isinstance(schema, bool):
|
|
|
|
| 7 |
return {}
|
| 8 |
return _original_json_schema_to_python_type(schema, defs)
|
| 9 |
|
| 10 |
gc_utils._json_schema_to_python_type = patched_json_schema_to_python_type
|
| 11 |
|
| 12 |
+
|
|
|
|
|
|
|
| 13 |
import logging
|
| 14 |
import os
|
| 15 |
os.makedirs("tmp", exist_ok=True)
|
|
|
|
| 23 |
import json
|
| 24 |
from io import BytesIO
|
| 25 |
|
|
|
|
| 26 |
from src.radial.radial import create_plot
|
| 27 |
from gradio_leaderboard import Leaderboard, SelectColumns
|
| 28 |
from gradio_space_ci import enable_space_ci
|
|
|
|
| 33 |
from src.leaderboard.build_leaderboard import build_leadearboard_df, download_openbench, download_dataset
|
| 34 |
import huggingface_hub
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "false"
|
| 38 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 39 |
enable_space_ci()
|
| 40 |
|
| 41 |
+
|
|
|
|
|
|
|
| 42 |
def handle_file_upload(file_bytes):
|
| 43 |
"""
|
| 44 |
Read the uploaded bytes and parse JSON directly,
|
| 45 |
avoiding ephemeral disk paths or file read issues.
|
| 46 |
"""
|
| 47 |
logging.info("File uploaded (bytes). Size: %d bytes", len(file_bytes))
|
|
|
|
| 48 |
v = json.loads(file_bytes.decode("utf-8"))
|
| 49 |
return v
|
| 50 |
|
|
|
|
| 55 |
"""
|
| 56 |
print('START SUBMITTING!!!')
|
| 57 |
|
|
|
|
| 58 |
if 'results' not in v:
|
| 59 |
return "Invalid JSON: missing 'results' key"
|
| 60 |
|
|
|
|
| 69 |
]
|
| 70 |
|
| 71 |
for column in columns:
|
|
|
|
| 72 |
if column not in new_file or not isinstance(new_file[column], dict):
|
| 73 |
return f"Missing or invalid column: {column}"
|
| 74 |
if 'acc,none' not in new_file[column]:
|
| 75 |
return f"Missing 'acc,none' key in column: {column}"
|
| 76 |
new_file[column] = new_file[column]['acc,none']
|
| 77 |
|
|
|
|
| 78 |
if 'config' not in v or 'model_dtype' not in v['config']:
|
| 79 |
return "Missing 'config' or 'model_dtype' in JSON"
|
| 80 |
|
|
|
|
| 83 |
|
| 84 |
print('WE READ FILE: ', new_file)
|
| 85 |
|
|
|
|
| 86 |
buf = BytesIO()
|
| 87 |
buf.write(json.dumps(new_file).encode('utf-8'))
|
| 88 |
+
buf.seek(0)
|
| 89 |
API.upload_file(
|
| 90 |
path_or_fileobj=buf,
|
| 91 |
path_in_repo="model_data/external/" + mn.replace('/', '__') + ".json",
|
|
|
|
| 96 |
os.environ[RESET_JUDGEMENT_ENV] = "1"
|
| 97 |
return "Success!"
|
| 98 |
|
| 99 |
+
|
|
|
|
|
|
|
| 100 |
def restart_space():
|
| 101 |
API.restart_space(repo_id=REPO_ID)
|
| 102 |
download_openbench()
|
|
|
|
| 104 |
def update_plot(selected_models):
|
| 105 |
return create_plot(selected_models)
|
| 106 |
|
| 107 |
+
|
|
|
|
|
|
|
| 108 |
def build_demo():
|
| 109 |
download_openbench()
|
| 110 |
demo = gr.Blocks(title="Kaz LLM LB", css=custom_css)
|
|
|
|
| 135 |
with gr.Column():
|
| 136 |
model_name_textbox = gr.Textbox(label="Model name")
|
| 137 |
|
|
|
|
| 138 |
file_output = gr.File(
|
| 139 |
label="Drag and drop JSON file judgment here",
|
| 140 |
type="binary"
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
uploaded_file = gr.State()
|
| 144 |
|
| 145 |
with gr.Row():
|
|
|
|
| 148 |
|
| 149 |
submit_button = gr.Button("Submit File", variant='primary')
|
| 150 |
|
|
|
|
| 151 |
file_output.upload(
|
| 152 |
fn=handle_file_upload,
|
| 153 |
inputs=file_output,
|
| 154 |
outputs=uploaded_file
|
| 155 |
)
|
| 156 |
|
|
|
|
| 157 |
submit_button.click(
|
| 158 |
fn=submit_file,
|
| 159 |
inputs=[uploaded_file, model_name_textbox],
|
|
|
|
| 178 |
)
|
| 179 |
return demo
|
| 180 |
|
|
|
|
|
|
|
|
|
|
| 181 |
def aggregate_leaderboard_data():
|
| 182 |
download_dataset("kz-transformers/s-openbench-eval", "m_data")
|
| 183 |
|
|
|
|
| 184 |
data_list = [
|
| 185 |
{
|
| 186 |
"model_dtype": "torch.float16",
|
|
|
|
| 351 |
logging.info("Updating the judgement at startup")
|
| 352 |
aggregate_leaderboard_data()
|
| 353 |
|
| 354 |
+
|
|
|
|
|
|
|
| 355 |
if __name__ == "__main__":
|
| 356 |
os.environ[RESET_JUDGEMENT_ENV] = "1"
|
| 357 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
| 361 |
scheduler.start()
|
| 362 |
|
| 363 |
demo_app = build_demo()
|
|
|
|
| 364 |
demo_app.launch(debug=True, share=False, show_api=False)
|