This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from diffusers import StableDiffusionPipeline | |
from matplotlib import pyplot as plt | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16) | |
pipe = pipe.to(device) | |
prompts = ["a hamburger"] * 8 + ["a birthday cake"] * 12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from diffusers import StableDiffusionXLPipeline | |
class CustomPipeline(StableDiffusionXLPipeline): | |
@classmethod | |
def from_pretrained(cls, *args, **kwargs): | |
self = super().from_pretrained(*args, **kwargs) | |
assert self.watermark is None # watermarking currently not supported | |
def postprocess_no_grad(image, *args, **kwargs): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
from tqdm.auto import tqdm | |
import pandas as pd | |
def kwargs2url(**kwargs): | |
url = 'https://commons.wikimedia.org/w/api.php?' | |
for k, v in kwargs.items(): | |
url += f'{k}={v}&' | |
return url.rstrip('&') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import surprise | |
import pandas as pd | |
data = surprise.Dataset.load_builtin('ml-100k') | |
ddir = surprise.get_dataset_dir() | |
item_data = pd.read_csv(f'{ddir}/ml-100k/ml-100k/u.item', | |
sep='|', | |
header=None, | |
encoding='ISO-8859-1', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
wget -O - https://pypi.org/simple/ 2>/dev/null | grep "$1" | grep -Po "(?<=>).*(?=<)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class Agent: | |
def __init__(self, n_machines=10): | |
self.t = 0 # current time step | |
self.n_machines = n_machines | |
def pick_machine(self): | |
# returns index of machine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask_caching import Cache | |
app = Flask(__name__) | |
app.config.from_mapping({"CACHE_TYPE": "simple"}) | |
cache = Cache(app) | |
def approximate_pi(n): | |
output = 0 | |
for i in range(1, n): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def top_k_categorical_accuracy(y_true, y_pred_proba, k=1): | |
return np.equal(np.argsort(y_pred_proba)[:, -k:], y_true[:, None]).any(axis=1).mean() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import tensorflow_hub as hub | |
from tokenizers import BertWordPieceTokenizer | |
from tensorflow.keras.layers import Input | |
from tensorflow.keras.models import Model | |
import numpy as np | |
class BERTPreprocessor: | |
SEP_TOKEN = '[SEP]' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup as bs | |
def html2text(html): | |
soup = bs(html, features='lxml') | |
for script in soup(["script", "style"]): | |
script.decompose() | |
for br in soup.find_all("br"): | |
br.replace_with("\n") | |
return soup.get_text(separator=' ').strip() |
NewerOlder