Skip to content

Instantly share code, notes, and snippets.

@gante
Last active July 29, 2023 20:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gante/2719cc4b1c8bd85a602b029e75f5517a to your computer and use it in GitHub Desktop.
Save gante/2719cc4b1c8bd85a602b029e75f5517a to your computer and use it in GitHub Desktop.
Portuguese image generation
from diffusers import StableDiffusionPipeline
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from torch import autocast
PT_PROMPT = "Um gato com um chapéu, pintura a aguarelas" # A cat with a hat, watercolor painting
# translation PT -> EN
transl_model_id = "Narrativa/mbart-large-50-finetuned-opus-pt-en-translation"
tokenizer = AutoTokenizer.from_pretrained(transl_model_id)
text_model = AutoModelForSeq2SeqLM.from_pretrained(transl_model_id)
pt_tokens = tokenizer([PT_PROMPT], return_tensors="pt")
en_tokens = text_model.generate(**pt_tokens, max_new_tokens=100, num_beams=8, early_stopping=True)
en_prompt = tokenizer.batch_decode(en_tokens, skip_special_tokens=True)
print(f"translation: [PT] {PT_PROMPT} -> [EN] {en_prompt[0]}")
# text -> image
# remember: you need to request access to stable diffusion. See the colab below for more information
# https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/stable_diffusion.ipynb
diffusion_model_id = "CompVis/stable-diffusion-v1-3-diffusers"
pipe = StableDiffusionPipeline.from_pretrained(diffusion_model_id, use_auth_token=True)
with autocast("cuda"):
image = pipe(en_prompt, guidance_scale=7.5, height=512, width=512)["sample"][0]
image.save(f"stable_ai_img.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment