Skip to content

Instantly share code, notes, and snippets.

@Norod
Norod / crapify_images.py
Created March 21, 2022 19:11
Go over a folder of images and reduce the quality and/or add noise to some of them (Useful for making a Pix2Pix model more resilient)
# crapify_images.py
# Go over a folder of images and reduce the quality and/or add noise to some of them (Useful for making a Pix2Pix model more resilient)
# @Norod78
import skimage
import skimage.io
import skimage.io._plugins.pil_plugin as pp
import numpy as np
from PIL import Image
@Norod
Norod / translate_text_file.py
Created June 23, 2022 19:01
Translate a text file using MarianMTModel models from Hugging Face
# !pip install sentencepiece transformers tokenizers
from transformers import MarianTokenizer, MarianMTModel
from typing import List
src = "en" # source language
trg = "he" # target language
model_name = f"Helsinki-NLP/opus-mt-{src}-{trg}"
@Norod
Norod / heb_neo_gra_demo.py
Created July 5, 2022 10:51
hebrew-gpt_neo-small minimal gradio demo using gradio and transformers pipeline
import gradio as gr
from transformers import pipeline
title = "Hebrew GPT-Neo Demo"
description = ""
article = "<p></p>"
examples = [
['צחוקים ושיגועים'],
["למנשה פומפרניקל יש"],
["פעם אחת לפני שנים רבות"]
@Norod
Norod / hebrew_gpt_neo_flax_pred.py
Created July 11, 2022 11:26
Hebrew GPT-Neo text generation using FlaxGPTNeoForCausalLM
import jax
from transformers import FlaxGPTNeoForCausalLM, AutoTokenizer
#model_name = 'Norod78/hebrew-gpt_neo-tiny'
#model_name = 'Norod78/hebrew_poetry-gpt_neo-small'
model_name = 'Norod78/hebrew-gpt_neo-small'
model = FlaxGPTNeoForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
@Norod
Norod / gpt2-large-onnx-convert.py
Created August 10, 2022 07:58
Converting gpt2-large to onnx with multiple external files and using it later for inference
#!/usr/bin/python
# -*- coding: utf-8 -*-
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel, AutoConfig
from transformers.onnx import FeaturesManager, convert, export
from pathlib import Path
import os
model_id = 'gpt2-large'
@Norod
Norod / latent-diffusion-laion-400m-model-text-to-image-with-google-drive-caching.ipynb
Created August 10, 2022 15:07
latent-diffusion-laion-400m-model-text-to-image-with-google-drive-caching.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Norod
Norod / stable-diffusion-model-text-to-image-with-google-drive-caching.ipynb
Created August 24, 2022 14:28
stable-diffusion-model-text-to-image-with-google-drive-caching.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Norod
Norod / stable-cpu.py
Created August 28, 2022 15:04
Stable Diffusion, running on CPU, uses hugging-face diffusers library
#### pip install diffusers==0.2.4 transformers scipy ftfy
####
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
import torch
def main():
seed = 1000 #1000, 42, 420
torch.manual_seed(seed)
generator = torch.Generator()
@Norod
Norod / patch_checker.py
Last active October 14, 2022 18:44
Patches safety_checker in diffuser's stable_diffusion pipeline to not blacken the potential NSFW image. The caller can still decide what to do with it based on the value of has_nsfw_concept[index]
import importlib
checker_location = importlib.util.find_spec('diffusers.pipelines.stable_diffusion.safety_checker').origin
checker_location_flax = importlib.util.find_spec('diffusers.pipelines.stable_diffusion.safety_checker_flax').origin
blocker_location_flax = importlib.util.find_spec('diffusers.pipelines.stable_diffusion.pipeline_flax_stable_diffusion').origin
#FlaxStableDiffusionPipeline
import fileinput
@Norod
Norod / vae-test.py
Last active September 28, 2022 08:51
Encapsulate an encoded VAE latent as PNG image, then load it and use it to decode the original image
#!pip install diffusers==0.2.4
import torch
from diffusers import AutoencoderKL
from PIL import Image
import numpy as np
from torchvision import transforms as tfms
torch_device = None
vae = None