File size: 1,938 Bytes
820f8a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "huggingface-hub[hf_transfer]",
#     "pillow",
#     "torch",
#     "diffusers",
#     "accelerate",
#     "transformers",
#     "python-slugify",
# ]
#
# ///

import argparse
import os
import torch
from diffusers import AutoPipelineForText2Image
from huggingface_hub import HfApi, login
from slugify import slugify

def main(prompt: str, repo: str, hf_token: str = None):
    HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")

    if HF_TOKEN:
        login(token=HF_TOKEN)

    name = slugify(prompt)
    filename = f"{name}.png"
    model_id = "stabilityai/stable-diffusion-xl-base-1.0"
    api = HfApi()

    print(f"Loading model: {model_id}...")
    pipe = AutoPipelineForText2Image.from_pretrained(
        model_id, torch_dtype=torch.float16, variant="fp16"
    ).to("cuda")

    print(f"Generating image for prompt: '{prompt}'...")
    image = pipe(prompt=prompt).images[0]
    temp_image_path = f"/tmp/{filename}"
    image.save(temp_image_path)
    print(f"Image saved temporarily to {temp_image_path}")

    print(f"Uploading {filename} to dataset repository: {repo}...")
    api.upload_file(
        path_or_fileobj=temp_image_path,
        path_in_repo=filename,
        repo_id=repo,
        repo_type="dataset",
        commit_message=f"add {filename}"
    )
    repo_url = f"https://huggingface.co/datasets/{repo}/tree/main"
    print(f"View it here: {repo_url}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Generate a single image using HF Jobs.")
    parser.add_argument("--prompt", required=True, help="The text prompt to generate an image from.")
    parser.add_argument("--repo", required=True, help="Your destination dataset repository ID.")
    parser.add_argument("--hf-token", help="Hugging Face API token.")
    args = parser.parse_args()
    main(prompt=args.prompt, repo=args.repo, hf_token=args.hf_token)