Add TrainingJob evaluation runtime
Browse files- eval_job.py +32 -43
eval_job.py
CHANGED
|
@@ -1,51 +1,40 @@
|
|
| 1 |
# /// script
|
| 2 |
# requires-python = ">=3.11"
|
| 3 |
-
# dependencies = ["huggingface-hub>=1.0", "datasets>=4.0", "
|
| 4 |
# ///
|
| 5 |
from __future__ import annotations
|
| 6 |
-
import argparse,
|
| 7 |
from pathlib import Path
|
| 8 |
-
import psutil
|
| 9 |
from datasets import load_dataset
|
| 10 |
-
from huggingface_hub import HfApi
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def emit(p,s,m): print(json.dumps({'event':'progress','percent':p,'stage':s,'message':m}),flush=True)
|
| 13 |
-
class Telemetry:
|
| 14 |
-
def __init__(self): self.stop=threading.Event(); self.t=threading.Thread(target=self.run,daemon=True)
|
| 15 |
-
def start(self): psutil.cpu_percent(None); self.t.start(); atexit.register(self.stop.set)
|
| 16 |
-
def run(self):
|
| 17 |
-
while not self.stop.wait(1):
|
| 18 |
-
m=psutil.virtual_memory(); print(json.dumps({'event':'telemetry','timestamp':time.time(),'cpu_percent':psutil.cpu_percent(None),'ram_percent':m.percent,'ram_used_gb':round((m.total-m.available)/1024**3,3),'ram_total_gb':round(m.total/1024**3,3),'gpu_count':0,'gpu_name':None,'gpu_util_percent':None,'vram_used_gb':None,'vram_total_gb':None,'vram_percent':None,'gpu_temperature_c':None}),flush=True)
|
| 19 |
def main():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
else:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
from jiwer import wer, cer
|
| 47 |
-
exact=sum(p.strip()==r.strip() for p,r in zip(predictions,references))/max(1,len(references)); report={'source':a.source,'dataset':a.dataset_id,'rows':len(references),'exact_match':exact,'wer':wer(references,predictions),'cer':cer(references,predictions),'samples':[{'prediction':p,'reference':r} for p,r in list(zip(predictions,references))[:20]]}; (out/'evaluation.json').write_text(json.dumps(report,indent=2,ensure_ascii=False))
|
| 48 |
-
if a.output_repo:
|
| 49 |
-
api=HfApi(token=token); api.create_repo(a.output_repo,repo_type='model',private=a.private,exist_ok=True,token=token); api.upload_folder(folder_path=out,repo_id=a.output_repo,repo_type='model',path_in_repo='evaluations',token=token,commit_message='Add EvalJob report')
|
| 50 |
-
emit(100,'completed',f"EvalJob completed: exact {exact:.3f}, CER {report['cer']:.3f}")
|
| 51 |
-
if __name__=='__main__': main()
|
|
|
|
| 1 |
# /// script
|
| 2 |
# requires-python = ">=3.11"
|
| 3 |
+
# dependencies = ["huggingface-hub>=1.0", "datasets>=4.0", "psutil>=6"]
|
| 4 |
# ///
|
| 5 |
from __future__ import annotations
|
| 6 |
+
import argparse, json, os, statistics, tempfile, time
|
| 7 |
from pathlib import Path
|
|
|
|
| 8 |
from datasets import load_dataset
|
| 9 |
+
from huggingface_hub import HfApi
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def emit(percent:int,stage:str,message:str): print(json.dumps({"event":"progress","percent":percent,"stage":stage,"message":message}),flush=True)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def main():
|
| 15 |
+
ap=argparse.ArgumentParser(description='Evaluate dataset compatibility and available quality signals')
|
| 16 |
+
ap.add_argument('--source',required=True);ap.add_argument('--dataset-id',required=True);ap.add_argument('--split',default='validation');ap.add_argument('--metric',default='auto');ap.add_argument('--max-samples',type=int,default=128);ap.add_argument('--prompt-column',default='prompt');ap.add_argument('--reference-column',default='text');ap.add_argument('--image-column',default='image');ap.add_argument('--output-repo',default='');ap.add_argument('--private',action='store_true');ap.add_argument('--dry-run',action='store_true');a=ap.parse_args()
|
| 17 |
+
token=os.environ['HF_TOKEN'];api=HfApi(token=token);emit(5,'inspect',f'Checking source {a.source}')
|
| 18 |
+
if not (Path(a.source).exists() or (Path('/cache')/a.source.lstrip('/')).exists()): api.model_info(a.source,token=token)
|
| 19 |
+
emit(20,'dataset',f'Loading {a.dataset_id}:{a.split}')
|
| 20 |
+
try: data=load_dataset(a.dataset_id,split=a.split,token=token)
|
| 21 |
+
except Exception:
|
| 22 |
+
data=load_dataset(a.dataset_id,split='train',token=token)
|
| 23 |
+
if a.max_samples>0:data=data.select(range(min(a.max_samples,len(data))))
|
| 24 |
+
columns=list(data.column_names);emit(40,'analyze',f'{len(data)} rows 路 {len(columns)} columns')
|
| 25 |
+
if a.dry_run:
|
| 26 |
+
emit(100,'completed',f'EvalJob dry-run valid 路 columns={columns}');return
|
| 27 |
+
references=[];prompts=[];images=0
|
| 28 |
+
for row in data:
|
| 29 |
+
ref=row.get(a.reference_column);prompt=row.get(a.prompt_column);image=row.get(a.image_column)
|
| 30 |
+
if ref not in (None,''):references.append(str(ref))
|
| 31 |
+
if prompt not in (None,''):prompts.append(str(prompt))
|
| 32 |
+
if image is not None:images+=1
|
| 33 |
+
report={'source':a.source,'dataset_id':a.dataset_id,'split':a.split,'metric':a.metric,'rows':len(data),'columns':columns,'reference_coverage':len(references)/max(1,len(data)),'prompt_coverage':len(prompts)/max(1,len(data)),'image_coverage':images/max(1,len(data)),'reference_length_mean':statistics.mean(map(len,references)) if references else 0,'prompt_length_mean':statistics.mean(map(len,prompts)) if prompts else 0,'note':'Generic quality readiness report. Model-specific inference metrics can be added as custom properties or a custom Job.','generated_at':time.time()}
|
| 34 |
+
emit(75,'report','Writing evaluation report')
|
| 35 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 36 |
+
root=Path(tmp);(root/'evaluation.json').write_text(json.dumps(report,indent=2),encoding='utf-8')
|
| 37 |
+
if a.output_repo:
|
| 38 |
+
api.create_repo(a.output_repo,repo_type='model',private=a.private,exist_ok=True,token=token);api.upload_folder(folder_path=root,repo_id=a.output_repo,repo_type='model',token=token,commit_message='Add evaluation report')
|
| 39 |
+
emit(100,'completed',f'Evaluated {len(data)} rows')
|
| 40 |
+
if __name__=='__main__':main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|