CoolFace
Apppublic

nexus00400/Text2Image-SD

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
benchmark.py38 linesDownload Raw Back to root
1import os2import time3import argparse4from statistics import mean5from huggingface_hub import InferenceClient6 7def run(prompt, model, provider, token, runs=5, steps=20, width=1024, height=1024, guidance=3.5):8    client = InferenceClient(provider=provider, api_key=token)9    times = []10    for i in range(runs):11        t0 = time.time()12        _ = client.text_to_image(prompt, model=model,13                                 num_inference_steps=steps,14                                 width=width, height=height,15                                 guidance_scale=guidance)16        times.append(time.time() - t0)17        print(f"Run {i+1}: {times[-1]:.2f}s")18    return times19 20if __name__ == "__main__":21    ap = argparse.ArgumentParser()22    ap.add_argument("--prompt", required=True)23    ap.add_argument("--model", default="stabilityai/sdxl-turbo")24    ap.add_argument("--provider", default="hf-inference")25    ap.add_argument("--runs", type=int, default=5)26    ap.add_argument("--gpu_hr_rate", type=float, default=0.5, help="USD per hour for equivalent GPU")27    args = ap.parse_args()28 29    token = os.environ.get("HF_TOKEN")30    if not token:31        raise SystemExit("Set HF_TOKEN in environment.")32 33    times = run(args.prompt, args.model, args.provider, token, runs=args.runs)34    avg_s = mean(times)35    est_cost = (args.gpu_hr_rate / 3600.0) * avg_s36    print(f"\nAverage time: {avg_s:.2f}s")37    print(f"Estimated cost / image (at ${args.gpu_hr_rate:.2f}/h): ${est_cost:.4f}")38