Clémentine commited on
Commit
c97be0a
·
1 Parent(s): 9c61c2c

change it into a command writer

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -19
  2. app.py +26 -47
  3. utils.py +51 -68
Dockerfile DELETED
@@ -1,19 +0,0 @@
1
- # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # you will also find guides on how best to write your Dockerfile
3
-
4
- FROM python:3.12
5
-
6
- RUN useradd -m -u 1000 user
7
- USER user
8
- ENV PATH="/home/user/.local/bin:$PATH"
9
-
10
- ENV HOME=/home/user
11
- WORKDIR $HOME
12
-
13
- COPY requirements.txt requirements.txt
14
- RUN pip install -r requirements.txt
15
-
16
- COPY app.py app.py
17
- COPY utils.py utils.py
18
-
19
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from utils import get_models, get_available_tasks, get_providers_for_model, build_lighteval_command, run_lighteval
4
 
5
  # Handle login and token storage
6
  def on_login(profile: gr.OAuthProfile):
@@ -9,13 +9,14 @@ def on_login(profile: gr.OAuthProfile):
9
  return f"Logged in as **{profile.name}** (@{profile.username})"
10
 
11
  # Update command when selections change
12
- def update_command_display(model, provider, tasks):
13
- return build_lighteval_command(model, provider, tasks)
 
14
 
15
 
16
- with gr.Blocks(title="LightEval Job Runner") as demo:
17
- gr.Markdown("# LightEval Job Runner")
18
- gr.Markdown("Configure and run lighteval jobs on Hugging Face models")
19
 
20
  # Add login button and user info
21
  with gr.Row():
@@ -40,30 +41,26 @@ with gr.Blocks(title="LightEval Job Runner") as demo:
40
  tasks_dropdown = gr.Dropdown(
41
  label="Tasks (searchable - type to filter)",
42
  choices=get_available_tasks(),
43
- value=None,
44
  multiselect=True,
45
  interactive=True,
46
  allow_custom_value=False,
47
  filterable=True
48
  )
49
 
50
- command_display = gr.Textbox(
51
- label="Generated LightEval Command",
52
- placeholder="Command will be generated from your selections",
53
- lines=4,
54
- interactive=False,
55
- show_copy_button=True
56
  )
57
 
58
- run_btn = gr.Button("Run Command", variant="primary")
59
-
60
- with gr.Row():
61
- logs_output = gr.Textbox(
62
- label="Logs",
63
- lines=20,
64
- max_lines=30,
65
- show_copy_button=True
66
- )
67
 
68
  # Link model to providers
69
  model_dropdown.change(
@@ -72,31 +69,13 @@ with gr.Blocks(title="LightEval Job Runner") as demo:
72
  outputs=provider_dropdown
73
  )
74
 
75
-
76
- model_dropdown.change(
77
- fn=update_command_display,
78
- inputs=[model_dropdown, provider_dropdown, tasks_dropdown],
79
- outputs=command_display
80
- )
81
-
82
- provider_dropdown.change(
83
- fn=update_command_display,
84
- inputs=[model_dropdown, provider_dropdown, tasks_dropdown],
85
- outputs=command_display
86
- )
87
-
88
- tasks_dropdown.change(
89
- fn=update_command_display,
90
- inputs=[model_dropdown, provider_dropdown, tasks_dropdown],
91
- outputs=command_display
92
- )
93
-
94
- # Run command
95
- run_btn.click(
96
- fn=run_lighteval,
97
- inputs=[command_display],
98
- outputs=logs_output
99
- )
100
 
101
  if __name__ == "__main__":
102
  demo.launch()
 
1
  import gradio as gr
2
 
3
+ from utils import get_models, get_available_tasks, get_providers_for_model, build_lighteval_command
4
 
5
  # Handle login and token storage
6
  def on_login(profile: gr.OAuthProfile):
 
9
  return f"Logged in as **{profile.name}** (@{profile.username})"
10
 
11
  # Update command when selections change
12
+ def update_command_display(model, provider, tasks, results_org, profile: gr.OAuthProfile):
13
+ username = profile.username if profile else "YOUR_USERNAME"
14
+ return build_lighteval_command(model, provider, tasks, results_org, username)
15
 
16
 
17
+ with gr.Blocks(title="LightEval ❤️ Jobs") as demo:
18
+ gr.Markdown("# Run evaluations in 5 lines of code!")
19
+ gr.Markdown("Generate your own snippet to run lighteval easily using jobs and inference providers")
20
 
21
  # Add login button and user info
22
  with gr.Row():
 
41
  tasks_dropdown = gr.Dropdown(
42
  label="Tasks (searchable - type to filter)",
43
  choices=get_available_tasks(),
44
+ value=get_available_tasks()[0],
45
  multiselect=True,
46
  interactive=True,
47
  allow_custom_value=False,
48
  filterable=True
49
  )
50
 
51
+ results_org = gr.Textbox(
52
+ label="Results Organization",
53
+ placeholder="Enter HF organization to save detailed results to",
54
+ interactive=True
 
 
55
  )
56
 
57
+ command_display = gr.Code(
58
+ label="Get the command for your model",
59
+ value=build_lighteval_command(),
60
+ language="python",
61
+ lines=15,
62
+ interactive=False
63
+ )
 
 
64
 
65
  # Link model to providers
66
  model_dropdown.change(
 
69
  outputs=provider_dropdown
70
  )
71
 
72
+ # Update command display when any input changes
73
+ for component in [model_dropdown, provider_dropdown, tasks_dropdown, results_org]:
74
+ component.change(
75
+ fn=update_command_display,
76
+ inputs=[model_dropdown, provider_dropdown, tasks_dropdown, results_org],
77
+ outputs=command_display
78
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  if __name__ == "__main__":
81
  demo.launch()
utils.py CHANGED
@@ -1,28 +1,35 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
4
- from huggingface_hub import list_models, model_info
5
  from lighteval.tasks.registry import Registry
 
6
 
7
  def get_models():
8
  """Get list of popular text generation models from Hugging Face"""
9
  try:
10
  models = list_models(
11
- task="text-generation",
12
- sort="downloads",
13
  direction=-1,
14
- limit=100
 
15
  )
16
- model_ids = [model.id for model in models if hasattr(model, 'id')]
 
 
 
 
 
17
  return model_ids
18
  except Exception as e:
19
  print(f"Error fetching models: {e}")
20
- return ["meta-llama/Llama-2-7b-hf", "mistralai/Mistral-7B-v0.1"]
21
 
22
  def get_providers_for_model(model_id):
23
  """Get inference providers for a specific model"""
24
  if not model_id:
25
- return gr.update(choices=[], value=None)
26
 
27
  try:
28
  info = model_info(model_id, expand="inferenceProviderMapping")
@@ -33,7 +40,7 @@ def get_providers_for_model(model_id):
33
  return gr.update(choices=[], value=None)
34
  except Exception as e:
35
  print(f"Error fetching providers for {model_id}: {e}")
36
- return gr.update(choices=[], value=None)
37
 
38
  # Cache tasks at module level to avoid reloading
39
  _TASKS_CACHE = None
@@ -47,7 +54,12 @@ def get_available_tasks():
47
  try:
48
  print("Loading lighteval tasks...")
49
  registry = Registry()
50
- tasks_list = [f"{v.suite[0]}|{k}" for k, v in registry.load_tasks().items()]
 
 
 
 
 
51
  tasks = sorted(tasks_list)
52
  _TASKS_CACHE = tasks
53
  print(f"Loaded {len(tasks)} tasks")
@@ -56,66 +68,37 @@ def get_available_tasks():
56
  print(f"Error fetching tasks: {e}")
57
  return []
58
 
59
- def build_lighteval_command(model, provider, tasks):
60
- """Build lighteval command from selections"""
61
- if not model:
62
- return "Error: Please select a model"
63
- if not provider:
64
- return "Error: Please select a provider"
65
- if not tasks or len(tasks) == 0:
66
- return "Error: Please select at least one task"
67
-
68
- # Format: lighteval endpoint inference-providers "model_name=MODEL,provider=PROVIDER" "TASK1|0" "TASK2|0"
69
  model_provider_arg = f'model_name={model},provider={provider}'
70
- task_args = ','.join([f'{task}|0' for task in tasks])
71
-
72
- command = f'lighteval endpoint inference-providers "{model_provider_arg}" "{task_args}"'
73
- return command
74
-
75
-
76
- def run_lighteval(command, oauth_token: gr.OAuthToken):
77
- """Run lighteval command and yield logs in real-time"""
78
- if not command.strip():
79
- yield "Error: Please enter a command"
80
- return
81
-
82
- if not oauth_token:
83
- yield "Error: Please login first to authenticate with Hugging Face"
84
- return
85
-
86
- try:
87
- # Set up environment with HF token
88
- env = os.environ.copy()
89
- env['HF_TOKEN'] = oauth_token.token
90
-
91
- # Run the command
92
- process = subprocess.Popen(
93
- command,
94
- shell=True,
95
- stdout=subprocess.PIPE,
96
- stderr=subprocess.STDOUT,
97
- text=True,
98
- bufsize=1,
99
- universal_newlines=True,
100
- env=env
101
- )
102
-
103
- output = ""
104
- # Stream output line by line
105
- for line in process.stdout:
106
- output += line
107
- yield output
108
-
109
- # Wait for process to complete
110
- process.wait()
111
 
112
- if process.returncode != 0:
113
- output += f"\n\nProcess exited with code {process.returncode}"
114
- yield output
115
- else:
116
- output += "\n\nProcess completed successfully!"
117
- yield output
118
 
119
- except Exception as e:
120
- yield f"Error running command: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
+ from huggingface_hub import list_models, model_info, run_job, inspect_job, whoami
5
  from lighteval.tasks.registry import Registry
6
+ from lighteval.utils.utils import as_list
7
 
8
  def get_models():
9
  """Get list of popular text generation models from Hugging Face"""
10
  try:
11
  models = list_models(
12
+ filter="text-generation",
13
+ sort="likes",
14
  direction=-1,
15
+ limit=1000,
16
+ expand="inferenceProviderMapping"
17
  )
18
+
19
+ model_ids = [
20
+ model.id
21
+ for model in models
22
+ if hasattr(model, 'inference_provider_mapping') and model.inference_provider_mapping
23
+ ]
24
  return model_ids
25
  except Exception as e:
26
  print(f"Error fetching models: {e}")
27
+ return []
28
 
29
  def get_providers_for_model(model_id):
30
  """Get inference providers for a specific model"""
31
  if not model_id:
32
+ return gr.update(choices=["NO PROVIDER"], value=None)
33
 
34
  try:
35
  info = model_info(model_id, expand="inferenceProviderMapping")
 
40
  return gr.update(choices=[], value=None)
41
  except Exception as e:
42
  print(f"Error fetching providers for {model_id}: {e}")
43
+ return gr.update(choices=["NO PROVIDER"], value=None)
44
 
45
  # Cache tasks at module level to avoid reloading
46
  _TASKS_CACHE = None
 
54
  try:
55
  print("Loading lighteval tasks...")
56
  registry = Registry()
57
+ # We only want generative metrics as the others won't run with inference providers
58
+ tasks_list = [
59
+ f"{config.suite[0]}|{name}"
60
+ for name, config in registry.load_tasks().items()
61
+ if all(metric.category.value == "GENERATIVE" for metric in config.metrics)
62
+ ]
63
  tasks = sorted(tasks_list)
64
  _TASKS_CACHE = tasks
65
  print(f"Loaded {len(tasks)} tasks")
 
68
  print(f"Error fetching tasks: {e}")
69
  return []
70
 
71
+ def build_lighteval_command(
72
+ model = "MODEL",
73
+ provider = "PROVIDER",
74
+ tasks = "TASKS",
75
+ results_org = "YOUR_ORG",
76
+ username="YOUR_USERNAME"
77
+ ):
78
+ """Build run_job command from selections"""
79
+ tasks = as_list(tasks)
 
80
  model_provider_arg = f'model_name={model},provider={provider}'
81
+ task_args = ','.join([f'{task}' for task in tasks])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ # Build the Python code string
84
+ command = f'''from huggingface_hub import run_job, inspect_job
85
+ import os
 
 
 
86
 
87
+ # The token must be able to call inference providers and write to the org you selected on your behalf
88
+ # Test the command with `"--max-samples", "10",` first, and remove to run a full evaluation job
89
+
90
+ job = run_job(
91
+ image="hf.co/spaces/OpenEvals/EvalsOnTheHub",
92
+ command=[
93
+ "lighteval", "endpoint", "inference-providers",
94
+ "{model_provider_arg}",
95
+ "{task_args}",
96
+ "--push-to-hub", "--save-details",
97
+ "--results-org", "{results_org}"
98
+ ],
99
+ namespace="{username}",
100
+ secrets={{"HF_TOKEN": os.getenv("HF_TOKEN")}},
101
+ token=os.getenv("HF_TOKEN")
102
+ )'''
103
+ return command
104