id
int64
0
190k
prompt
stringlengths
21
13.4M
docstring
stringlengths
1
12k
179,472
from transformers import pipeline, set_seed import random import re text_pipe = pipeline('text-generation', model='succinctly/text2image-prompt-generator') def text_generate(input): seed = random.randint(100, 1000000) set_seed(seed) for count in range(6): sequences = text_pipe(input, max_length=random.randint(60, 90), num_return_sequences=8) list = [] for sequence in sequences: line = sequence['generated_text'].strip() if line != input and len(line) > (len(input) + 4) and line.endswith((":", "-", "—")) is False: list.append(line) result = "\n".join(list) result = re.sub('[^ ]+\.[^ ]+','', result) result = result.replace("<", "").replace(">", "") if result != "": return result if count == 5: return result
null
179,473
from clip_interrogator import Config, Interrogator import torch ci = Interrogator(config) import requests import shutil from PIL import Image def get_prompt_from_image(image): return ci.interrogate(image.convert('RGB'))
null
179,474
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM import torch model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en").eval() tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en") def translate(text): with torch.no_grad(): encoded = tokenizer([text], return_tensors="pt") sequences = model.generate(**encoded) return tokenizer.batch_decode(sequences, skip_special_tokens=True)[0]
null
179,475
from nltk.stem import WordNetLemmatizer TAG_OVERRIDES = { "flashbang": "stun-grenade", "flashbangs": "stun-grenade", "taze": "tase", "tazes": "tase", "tazer": "taser", "tazers": "taser", "kneck": "neck", "knee-on-kneck": "knee-on-neck", "bicycle": "bike", "beanbag": "bean-bag", "beanbags": "bean-bag", "shot": "shoot", "kneel": "knee", "pepper-bullet": "pepper-ball", "protestor": "protester", "real-bullet": "live-round", "le-lethal": "less-lethal", } WNL = WordNetLemmatizer() def format_tag(wnl, tag_overrides, tag): tag_words = tag.strip().replace(".", "").split("-") new_tag_words = [] for tag_word in tag_words: new_tag_words.append(wnl.lemmatize(tag_word)) output_tag = "-".join(new_tag_words) if output_tag in tag_overrides: return tag_overrides[output_tag] return output_tag def read_tag_file(tag_path): all_tags = set() with open(tag_path, "r") as tag_file: for line in tag_file.readlines(): if line.startswith("```") or line.startswith("##") or len(line.strip()) == 0: continue all_tags.add(format_tag(WNL, TAG_OVERRIDES, line.strip())) return all_tags
null
179,476
import random import string import data_builder from text_formatter import COMMON_MISSPELLINGS, fix_common_misspellings, format_tags, read_tag_file, TAG_OVERRIDES, WNL def gen_md_from_rows(state, rows, all_tags): city = "" lines = [] for row in rows: if row["city"] and row["city"] != city: # new city, let everyone know lines.append(f'## {row["city"]}\n') city = row["city"] # convert links list to a links string links_md = "\n".join("* " + markdown_link(it) for it in row["links_v2"]) row["links_md"] = links_md # convert tags from a list to a string row["tags_md"] = format_tags(WNL, all_tags, TAG_OVERRIDES, row["tags"]) row["name"] = fix_common_misspellings(row["name"], COMMON_MISSPELLINGS) row["description"] = fix_common_misspellings(row["description"], COMMON_MISSPELLINGS) # Create this row's markdown lines.append(row_format.format(**row)) return "\n".join(lines) def rewrite_data(data, all_tags): state_to_rows = {} for row in data: state = row["state"] if state not in state_to_rows: state_to_rows[state] = [] state_to_rows[state].append(row) for state, data_rows in state_to_rows.items(): out_path = f"{data_builder.md_dir}/{state}.md" # We don't need to sort `data_rows` because we read these in the order of the markdown files new_md_text = gen_md_from_rows(state, data_rows, all_tags) with open(out_path, "wb") as fout: fout.write(new_md_text.encode("utf-8"))
null
179,477
import random import string import data_builder from text_formatter import COMMON_MISSPELLINGS, fix_common_misspellings, format_tags, read_tag_file, TAG_OVERRIDES, WNL def validate_ids_unique(data): seen = set() for row in data: row_id = row["id"] if row_id in seen: print(row) data_builder.critical_exit(f"Duplicate id found {row_id}") else: seen.add(row_id)
null
179,478
import random import string import data_builder from text_formatter import COMMON_MISSPELLINGS, fix_common_misspellings, format_tags, read_tag_file, TAG_OVERRIDES, WNL def gen_id(row): state = row["state"] state_abbrev = us_state_to_abbrev[state].lower() city = row["city"] city_abbrev = city.replace(" ", "").replace(".", "").lower() if len(city_abbrev) == 0: if state_abbrev == unknown_location_acronym: city_abbrev = unknown_location_acronym elif state_abbrev == "dc": city_abbrev = "dc" else: data_builder.critical_exit("invalid city abbreviation, exiting") # id_line = f'id: {state_abbrev}-{city_abbrev}-{city_index}' return f"{state_abbrev}-{city_abbrev}-{random_chars(4)}" def add_missing_ids(data): for row in data: if "id" not in row or len(row["id"]) == 0: row["id"] = gen_id(row) print("Added id: " + row["id"]) if "name" not in row: print(row) data_builder.critical_exit("this row is broken with no name? (missing ###):") return data
null
179,479
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone md_header = f""" GENERATED FILE, PLEASE MAKE EDITS ON MASTER AT https://github.com/2020PB/police-brutality/ UPDATED AT: {updated_at} """ md_out_format = """ # {location} {text} """ def to_merged_md_file(md_texts, target_path): with open(target_path, "wb") as fout: fout.write(md_header.encode("utf-8")) for location, text in sorted(md_texts.items()): out_text = md_out_format.format(location=location, text=text) fout.write(out_text.encode("utf-8")) print(f"Written merged .md data to {target_path}")
null
179,480
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone def to_csv_file_v1(data, target_path): max_link_count = max(len(it["links"]) for it in data) flat_data = [] for row in data: # just write it but instead of a list of links # put each link in its own column flat_row = row.copy() links = flat_row["links"] del flat_row["links"] for i in range(max_link_count): url = "" if i < len(links): url = links[i] flat_row[f"Link {i + 1}"] = url flat_data.append(flat_row) with open(target_path, "w", newline="", encoding="utf-8") as fout: writer = csv.DictWriter(fout, flat_data[0].keys()) writer.writeheader() writer.writerows(flat_data) print(f"Written .csv data to {target_path}")
null
179,481
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone updated_at = datetime.now(timezone.utc).isoformat() def to_json_file_v1(data, target_path): data_with_meta = { "edit_at": "https://github.com/2020PB/police-brutality", "help": "ask @ubershmekel on twitter", "updated_at": updated_at, "data": data, } with open(target_path, "w") as f: json.dump(data_with_meta, f) print(f"Written .json data to {target_path}")
null
179,482
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone updated_at = datetime.now(timezone.utc).isoformat() def v2_only(item): item = copy.deepcopy(item) item["links"] = item["links_v2"] del item["links_v2"] return item def to_json_file_v2(data, target_path): v2_data = [v2_only(item) for item in data] data_with_meta = { "edit_at": "https://github.com/2020PB/police-brutality", "help": "ask @ubershmekel on twitter", "updated_at": updated_at, "data": v2_data, } with open(target_path, "w") as f: json.dump(data_with_meta, f) print(f"Written .json v2 data to {target_path}")
null
179,483
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone readme_text = """ # /r/2020PoliceBrutality/ dataset This repository exists to accumulate and contextualize evidence of police brutality during the 2020 George Floyd protests. Our goal in doing this is to assist journalists, politicians, prosecutors, activists and concerned individuals who can use the evidence accumulated here for political campaigns, news reporting, public education and prosecution of criminal police officers. * This branch is just the files generated by parsing the markdown for ease of building other sites. * For example your webapp can query and display data from https://raw.githubusercontent.com/2020PB/police-brutality/data_build/all-locations.json * For more info see https://github.com/2020PB/police-brutality * These data files are generated by https://github.com/2020PB/police-brutality/tree/main/tools # THESE FILES ARE GENERATED - DO NOT EDIT (including this readme) * Please edit the `.md` files on the `main` branch at https://github.com/2020PB/police-brutality * Also notice each data row has a `edit_at` link so you can find the source data for every entry. """ def to_readme(target_path): with open(target_path, "w") as f: f.write(readme_text)
null
179,484
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone md_dir = os.path.join(src_dir, "..", "reports") def read_all_md_files(base_dir): def process_md_texts(md_texts): def read_all_data(): md_texts = read_all_md_files(md_dir) data = process_md_texts(md_texts) return data
null
179,485
import csv import glob import json import os import re import copy from dateutil.parser import parse from datetime import datetime, timezone def v1_only(item): # Deepcopy to avoid affecting the original data item = copy.deepcopy(item) v1_keys = set(["links", "state", "city", "edit_at", "name", "date", "date_text", "id"]) # Cache keys to avoid errors for deleting while iterating item_keys = list(item.keys()) for key in item_keys: if key not in v1_keys: del item[key] return item
null
179,486
import itertools import logging import os import random import numpy as np import tensorflow as tf from tensorflow_tts.datasets.abstract_dataset import AbstractDataset from tensorflow_tts.utils import find_files def average_by_duration(x, durs): def tf_average_by_duration(x, durs): outs = tf.numpy_function(average_by_duration, [x, durs], tf.float32) return outs
null
179,487
import argparse import logging import os from numba import jit import sys import matplotlib.pyplot as plt import numpy as np import tensorflow as tf import yaml from tqdm import tqdm from examples.tacotron2.tacotron_dataset import CharactorMelDataset from tensorflow_tts.configs import Tacotron2Config from tensorflow_tts.models import TFTacotron2 def get_duration_from_alignment(alignment): D = np.array([0 for _ in range(np.shape(alignment)[0])]) for i in range(np.shape(alignment)[1]): max_index = list(alignment[:, i]).index(alignment[:, i].max()) D[max_index] = D[max_index] + 1 return D
null
179,488
import os import shutil from tqdm import tqdm import argparse from scipy.ndimage import zoom from skimage.data import camera import numpy as np from scipy.spatial.distance import cdist from pathlib import Path def safemkdir(dirn): if not os.path.isdir(dirn): os.mkdir(dirn)
null
179,489
import os import shutil from tqdm import tqdm import argparse from scipy.ndimage import zoom from skimage.data import camera import numpy as np from scipy.spatial.distance import cdist from pathlib import Path def duration_to_alignment(in_duration): total_len = np.sum(in_duration) num_chars = len(in_duration) attention = np.zeros(shape=(num_chars, total_len), dtype=np.float32) y_offset = 0 for duration_idx, duration_val in enumerate(in_duration): for y_val in range(0, duration_val): attention[duration_idx][y_offset + y_val] = 1.0 y_offset += duration_val return attention
null
179,490
import os import shutil from tqdm import tqdm import argparse from scipy.ndimage import zoom from skimage.data import camera import numpy as np from scipy.spatial.distance import cdist from pathlib import Path def rescale_alignment(in_alignment, in_targcharlen): current_x = in_alignment.shape[0] x_ratio = in_targcharlen / current_x pivot_points = [] zoomed = zoom(in_alignment, (x_ratio, 1.0), mode="nearest") for x_v in range(0, zoomed.shape[0]): for y_v in range(0, zoomed.shape[1]): val = zoomed[x_v][y_v] if val < 0.5: val = 0.0 else: val = 1.0 pivot_points.append((x_v, y_v)) zoomed[x_v][y_v] = val if zoomed.shape[0] != in_targcharlen: print("Zooming didn't rshape well, explicitly reshaping") zoomed.resize((in_targcharlen, in_alignment.shape[1])) return zoomed, pivot_points
null
179,491
import os import shutil from tqdm import tqdm import argparse from scipy.ndimage import zoom from skimage.data import camera import numpy as np from scipy.spatial.distance import cdist from pathlib import Path def gather_dist(in_mtr, in_points): # initialize with known size for fast full_coords = [(0, 0) for x in range(in_mtr.shape[0] * in_mtr.shape[1])] i = 0 for x in range(0, in_mtr.shape[0]): for y in range(0, in_mtr.shape[1]): full_coords[i] = (x, y) i += 1 return cdist(full_coords, in_points, "euclidean") def create_guided(in_align, in_pvt, looseness): new_att = np.ones(in_align.shape, dtype=np.float32) # It is dramatically faster that we first gather all the points and calculate than do it manually # for each point in for loop dist_arr = gather_dist(in_align, in_pvt) # Scale looseness based on attention size. (addition works better than mul). Also divide by 100 # because having user input 3.35 is nicer real_loose = (looseness / 100) * (new_att.shape[0] + new_att.shape[1]) g_idx = 0 for x in range(0, new_att.shape[0]): for y in range(0, new_att.shape[1]): min_point_idx = dist_arr[g_idx].argmin() closest_pvt = in_pvt[min_point_idx] distance = dist_arr[g_idx][min_point_idx] / real_loose distance = np.power(distance, 2) g_idx += 1 new_att[x, y] = distance return np.clip(new_att, 0.0, 1.0)
null
179,492
import os import shutil from tqdm import tqdm import argparse from scipy.ndimage import zoom from skimage.data import camera import numpy as np from scipy.spatial.distance import cdist from pathlib import Path def get_pivot_points(in_att): ret_points = [] for x in range(0, in_att.shape[0]): for y in range(0, in_att.shape[1]): if in_att[x, y] > 0.8: ret_points.append((x, y)) return ret_points
null
179,494
import tensorflow as tf import sys import argparse import logging import os import numpy as np import soundfile as sf import yaml from tqdm import tqdm import tensorflow_tts import tensorflow_tts.configs.melgan as MELGAN_CONFIG from examples.melgan.audio_mel_dataset import AudioMelDataset from tensorflow_tts.losses import TFMelSpectrogram from tensorflow_tts.models import TFMelGANGenerator, TFMelGANMultiScaleDiscriminator from tensorflow_tts.trainers import GanBasedTrainer from tensorflow_tts.utils import calculate_2d_loss, calculate_3d_loss, return_strategy The provided code snippet includes necessary dependencies for implementing the `collater` function. Write a Python function `def collater( items, batch_max_steps=tf.constant(8192, dtype=tf.int32), hop_size=tf.constant(256, dtype=tf.int32), )` to solve the following problem: Initialize collater (mapping function) for Tensorflow Audio-Mel Dataset. Args: batch_max_steps (int): The maximum length of input signal in batch. hop_size (int): Hop size of auxiliary features. Here is the function: def collater( items, batch_max_steps=tf.constant(8192, dtype=tf.int32), hop_size=tf.constant(256, dtype=tf.int32), ): """Initialize collater (mapping function) for Tensorflow Audio-Mel Dataset. Args: batch_max_steps (int): The maximum length of input signal in batch. hop_size (int): Hop size of auxiliary features. """ audio, mel = items["audios"], items["mels"] if batch_max_steps is None: batch_max_steps = (tf.shape(audio)[0] // hop_size) * hop_size batch_max_frames = batch_max_steps // hop_size if len(audio) < len(mel) * hop_size: audio = tf.pad(audio, [[0, len(mel) * hop_size - len(audio)]]) if len(mel) > batch_max_frames: # randomly pickup with the batch_max_steps length of the part interval_start = 0 interval_end = len(mel) - batch_max_frames start_frame = tf.random.uniform( shape=[], minval=interval_start, maxval=interval_end, dtype=tf.int32 ) start_step = start_frame * hop_size audio = audio[start_step : start_step + batch_max_steps] mel = mel[start_frame : start_frame + batch_max_frames, :] else: audio = tf.pad(audio, [[0, batch_max_steps - len(audio)]]) mel = tf.pad(mel, [[0, batch_max_frames - len(mel)], [0, 0]]) items = { "utt_ids": items["utt_ids"], "audios": audio, "mels": mel, "mel_lengths": len(mel), "audio_lengths": len(audio), } return items
Initialize collater (mapping function) for Tensorflow Audio-Mel Dataset. Args: batch_max_steps (int): The maximum length of input signal in batch. hop_size (int): Hop size of auxiliary features.
179,495
import os import numpy as np import tensorflow as tf from tensorflow_tts.datasets.abstract_dataset import AbstractDataset from tensorflow_tts.utils import find_files def average_by_duration(x, durs): mel_len = durs.sum() durs_cum = np.cumsum(np.pad(durs, (1, 0))) # calculate charactor f0/energy x_char = np.zeros((durs.shape[0],), dtype=np.float32) for idx, start, end in zip(range(mel_len), durs_cum[:-1], durs_cum[1:]): values = x[start:end][np.where(x[start:end] != 0.0)[0]] x_char[idx] = np.mean(values) if len(values) > 0 else 0.0 # np.mean([]) = nan. return x_char.astype(np.float32) def tf_average_by_duration(x, durs): outs = tf.numpy_function(average_by_duration, [x, durs], tf.float32) return outs
null
179,496
from subprocess import call from pathlib import Path import click import os def run_mfa( mfa_path: str, corpus_directory: str, lexicon: str, acoustic_model_path: str, output_directory: str, jobs: str, ): Path(output_directory).mkdir(parents=True, exist_ok=True) call( [ f".{os.path.sep}{mfa_path}", corpus_directory, lexicon, acoustic_model_path, output_directory, f"-j {jobs}" ] )
null
179,497
import numpy as np import os from tqdm import tqdm import click import logging import sys logging.basicConfig( level=logging.DEBUG, stream=sys.stdout, format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s", ) def fix(base_path: str, dur_path: str, trimmed_dur_path: str, use_norm: str): for t in ["train", "valid"]: mfa_longer = [] mfa_shorter = [] big_diff = [] not_fixed = [] pre_path = os.path.join(base_path, t) os.makedirs(os.path.join(pre_path, "fix_dur"), exist_ok=True) logging.info(f"FIXING {t} set ...\n") for i in tqdm(os.listdir(os.path.join(pre_path, "ids"))): if use_norm == "t": mel = np.load( os.path.join( pre_path, "norm-feats", f"{i.split('-')[0]}-norm-feats.npy" ) ) else: mel = np.load( os.path.join( pre_path, "raw-feats", f"{i.split('-')[0]}-raw-feats.npy" ) ) try: dur = np.load( os.path.join(trimmed_dur_path, f"{i.split('-')[0]}-durations.npy") ) except: dur = np.load( os.path.join(dur_path, f"{i.split('-')[0]}-durations.npy") ) l_mel = len(mel) dur_s = np.sum(dur) cloned = np.array(dur, copy=True) diff = abs(l_mel - dur_s) if abs(l_mel - dur_s) > 30: # more then 300 ms big_diff.append([i, abs(l_mel - dur_s)]) if dur_s > l_mel: for j in range(1, len(dur) - 1): if diff == 0: break dur_val = cloned[-j] if dur_val >= diff: cloned[-j] -= diff diff -= dur_val break else: cloned[-j] = 0 diff -= dur_val if j == len(dur) - 2: not_fixed.append(i) mfa_longer.append(abs(l_mel - dur_s)) elif dur_s < l_mel: cloned[-1] += diff mfa_shorter.append(abs(l_mel - dur_s)) np.save( os.path.join(pre_path, "fix_dur", f"{i.split('-')[0]}-durations.npy"), cloned.astype(np.int32), allow_pickle=False, ) logging.info( f"{t} stats: number of mfa with longer duration: {len(mfa_longer)}, total diff: {sum(mfa_longer)}" f", mean diff: {sum(mfa_longer)/len(mfa_longer) if len(mfa_longer) > 0 else 0}" ) logging.info( f"{t} stats: number of mfa with shorter duration: {len(mfa_shorter)}, total diff: {sum(mfa_shorter)}" f", mean diff: {sum(mfa_shorter)/len(mfa_shorter) if len(mfa_shorter) > 0 else 0}" ) logging.info( f"{t} stats: number of files with a ''big'' duration diff: {len(big_diff)} if number>1 you should check it" ) logging.info(f"{t} stats: not fixed len: {len(not_fixed)}\n")
null
179,498
import os import re from typing import Dict, List, Union, Tuple, Any import librosa import numpy as np import soundfile as sf from dataclasses import dataclass, field from pypinyin import Style from pypinyin.contrib.neutral_tone import NeutralToneWith5Mixin from pypinyin.converter import DefaultConverter from pypinyin.core import Pinyin from tensorflow_tts.processor import BaseProcessor from tensorflow_tts.utils.utils import PROCESSOR_FILE_NAME zh_pattern = re.compile("[\u4e00-\u9fa5]") def is_zh(word): global zh_pattern match = zh_pattern.search(word) return match is not None
null
179,499
import argparse import glob import logging import os import yaml import librosa import numpy as np import pyworld as pw from functools import partial from multiprocessing import Pool from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from tqdm import tqdm from tensorflow_tts.processor import LJSpeechProcessor from tensorflow_tts.processor import BakerProcessor from tensorflow_tts.processor import KSSProcessor from tensorflow_tts.processor import LibriTTSProcessor from tensorflow_tts.processor import ThorstenProcessor from tensorflow_tts.processor import LJSpeechUltimateProcessor from tensorflow_tts.processor import SynpaflexProcessor from tensorflow_tts.processor import JSUTProcessor from tensorflow_tts.processor.ljspeech import LJSPEECH_SYMBOLS from tensorflow_tts.processor.baker import BAKER_SYMBOLS from tensorflow_tts.processor.kss import KSS_SYMBOLS from tensorflow_tts.processor.libritts import LIBRITTS_SYMBOLS from tensorflow_tts.processor.thorsten import THORSTEN_SYMBOLS from tensorflow_tts.processor.ljspeechu import LJSPEECH_U_SYMBOLS from tensorflow_tts.processor.synpaflex import SYNPAFLEX_SYMBOLS from tensorflow_tts.processor.jsut import JSUT_SYMBOLS from tensorflow_tts.utils import remove_outlier os.environ["CUDA_VISIBLE_DEVICES"] = "" def parse_and_config(): """Parse arguments and set configuration parameters.""" parser = argparse.ArgumentParser( description="Preprocess audio and text features " "(See detail in tensorflow_tts/bin/preprocess_dataset.py)." ) parser.add_argument( "--rootdir", default=None, type=str, required=True, help="Directory containing the dataset files.", ) parser.add_argument( "--outdir", default=None, type=str, required=True, help="Output directory where features will be saved.", ) parser.add_argument( "--dataset", type=str, default="ljspeech", choices=["ljspeech", "kss", "libritts", "baker", "thorsten", "ljspeechu", "synpaflex", "jsut"], help="Dataset to preprocess.", ) parser.add_argument( "--config", type=str, required=True, help="YAML format configuration file." ) parser.add_argument( "--n_cpus", type=int, default=4, required=False, help="Number of CPUs to use in parallel.", ) parser.add_argument( "--test_size", type=float, default=0.05, required=False, help="Proportion of files to use as test dataset.", ) parser.add_argument( "--verbose", type=int, default=0, choices=[0, 1, 2], help="Logging level. 0: DEBUG, 1: INFO and WARNING, 2: INFO, WARNING, and ERROR", ) args = parser.parse_args() # set logger FORMAT = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s" log_level = {0: logging.DEBUG, 1: logging.WARNING, 2: logging.ERROR} logging.basicConfig(level=log_level[args.verbose], format=FORMAT) # load config config = yaml.load(open(args.config), Loader=yaml.SafeLoader) config.update(vars(args)) # config checks assert config["format"] == "npy", "'npy' is the only supported format." return config def gen_audio_features(item, config): """Generate audio features and transformations Args: item (Dict): dictionary containing the attributes to encode. config (Dict): configuration dictionary. Returns: (bool): keep this sample or not. mel (ndarray): mel matrix in np.float32. energy (ndarray): energy audio profile. f0 (ndarray): fundamental frequency. item (Dict): dictionary containing the updated attributes. """ # get info from sample. audio = item["audio"] utt_id = item["utt_id"] rate = item["rate"] # check audio properties assert len(audio.shape) == 1, f"{utt_id} seems to be multi-channel signal." assert np.abs(audio).max() <= 1.0, f"{utt_id} is different from 16 bit PCM." # check sample rate if rate != config["sampling_rate"]: audio = librosa.resample(audio, rate, config["sampling_rate"]) logging.info(f"{utt_id} sampling rate is {rate}, not {config['sampling_rate']}, we resample it.") # trim silence if config["trim_silence"]: if "trim_mfa" in config and config["trim_mfa"]: _, item["text_ids"], audio = ph_based_trim( config, utt_id, item["text_ids"], item["raw_text"], audio, config["hop_size"], ) if ( audio.__len__() < 1 ): # very short files can get trimmed fully if mfa didnt extract any tokens LibriTTS maybe take only longer files? logging.warning( f"File have only silence or MFA didnt extract any token {utt_id}" ) return False, None, None, None, item else: audio, _ = librosa.effects.trim( audio, top_db=config["trim_threshold_in_db"], frame_length=config["trim_frame_size"], hop_length=config["trim_hop_size"], ) # resample audio if necessary if "sampling_rate_for_feats" in config: audio = librosa.resample(audio, rate, config["sampling_rate_for_feats"]) sampling_rate = config["sampling_rate_for_feats"] assert ( config["hop_size"] * config["sampling_rate_for_feats"] % rate == 0 ), "'hop_size' must be 'int' value. Please check if 'sampling_rate_for_feats' is correct." hop_size = config["hop_size"] * config["sampling_rate_for_feats"] // rate else: sampling_rate = config["sampling_rate"] hop_size = config["hop_size"] # get spectrogram D = librosa.stft( audio, n_fft=config["fft_size"], hop_length=hop_size, win_length=config["win_length"], window=config["window"], pad_mode="reflect", ) S, _ = librosa.magphase(D) # (#bins, #frames) # get mel basis fmin = 0 if config["fmin"] is None else config["fmin"] fmax = sampling_rate // 2 if config["fmax"] is None else config["fmax"] mel_basis = librosa.filters.mel( sr=sampling_rate, n_fft=config["fft_size"], n_mels=config["num_mels"], fmin=fmin, fmax=fmax, ) mel = np.log10(np.maximum(np.dot(mel_basis, S), 1e-10)).T # (#frames, #bins) # check audio and feature length audio = np.pad(audio, (0, config["fft_size"]), mode="edge") audio = audio[: len(mel) * hop_size] assert len(mel) * hop_size == len(audio) # extract raw pitch _f0, t = pw.dio( audio.astype(np.double), fs=sampling_rate, f0_ceil=fmax, frame_period=1000 * hop_size / sampling_rate, ) f0 = pw.stonemask(audio.astype(np.double), _f0, t, sampling_rate) if len(f0) >= len(mel): f0 = f0[: len(mel)] else: f0 = np.pad(f0, (0, len(mel) - len(f0))) # extract energy energy = np.sqrt(np.sum(S ** 2, axis=0)) assert len(mel) == len(f0) == len(energy) # remove outlier f0/energy f0 = remove_outlier(f0) energy = remove_outlier(energy) # apply global gain if config["global_gain_scale"] > 0.0: audio *= config["global_gain_scale"] if np.abs(audio).max() >= 1.0: logging.warn( f"{utt_id} causes clipping. It is better to reconsider global gain scale value." ) item["audio"] = audio item["mel"] = mel item["f0"] = f0 item["energy"] = energy return True, mel, energy, f0, item def save_statistics_to_file(scaler_list, config): """Save computed statistics to disk. Args: scaler_list (List): List of scalers containing statistics to save. config (Dict): configuration dictionary. """ for scaler, name in scaler_list: stats = np.stack((scaler.mean_, scaler.scale_)) np.save( os.path.join(config["outdir"], f"stats{name}.npy"), stats.astype(np.float32), allow_pickle=False, ) def save_features_to_file(features, subdir, config): """Save transformed dataset features in disk. Args: features (Dict): dictionary containing the attributes to save. subdir (str): data split folder where features will be saved. config (Dict): configuration dictionary. """ utt_id = features["utt_id"] if config["format"] == "npy": save_list = [ (features["audio"], "wavs", "wave", np.float32), (features["mel"], "raw-feats", "raw-feats", np.float32), (features["text_ids"], "ids", "ids", np.int32), (features["f0"], "raw-f0", "raw-f0", np.float32), (features["energy"], "raw-energies", "raw-energy", np.float32), ] for item, name_dir, name_file, fmt in save_list: np.save( os.path.join( config["outdir"], subdir, name_dir, f"{utt_id}-{name_file}.npy" ), item.astype(fmt), allow_pickle=False, ) else: raise ValueError("'npy' is the only supported format.") LJSPEECH_SYMBOLS = ( [_pad] + list(_special) + list(_punctuation) + list(_letters) + _arpabet + [_eos] ) BAKER_SYMBOLS = _pad + _pause + _initials + [i + j for i in _finals for j in _tones] + _eos LIBRITTS_SYMBOLS = _arpabet + list(_punctuation) THORSTEN_SYMBOLS = ( [_pad] + list(_special) + list(_punctuation) + list(_letters) + [_eos] ) LJSPEECH_U_SYMBOLS = [_pad] + list(_special) + list(_punctuation) + _arpabet + [_eos] S = ( [_pad] + list(_punctuation) + list(_letters) + [_eos] ) JSUT_SYMBOLS = ( [_pad] + [_sil] + valid_symbols + [_eos] ) The provided code snippet includes necessary dependencies for implementing the `preprocess` function. Write a Python function `def preprocess()` to solve the following problem: Run preprocessing process and compute statistics for normalizing. Here is the function: def preprocess(): """Run preprocessing process and compute statistics for normalizing.""" config = parse_and_config() dataset_processor = { "ljspeech": LJSpeechProcessor, "kss": KSSProcessor, "libritts": LibriTTSProcessor, "baker": BakerProcessor, "thorsten": ThorstenProcessor, "ljspeechu": LJSpeechUltimateProcessor, "synpaflex": SynpaflexProcessor, "jsut": JSUTProcessor, } dataset_symbol = { "ljspeech": LJSPEECH_SYMBOLS, "kss": KSS_SYMBOLS, "libritts": LIBRITTS_SYMBOLS, "baker": BAKER_SYMBOLS, "thorsten": THORSTEN_SYMBOLS, "ljspeechu": LJSPEECH_U_SYMBOLS, "synpaflex": SYNPAFLEX_SYMBOLS, "jsut": JSUT_SYMBOLS, } dataset_cleaner = { "ljspeech": "english_cleaners", "kss": "korean_cleaners", "libritts": None, "baker": None, "thorsten": "german_cleaners", "ljspeechu": "english_cleaners", "synpaflex": "basic_cleaners", "jsut": None, } logging.info(f"Selected '{config['dataset']}' processor.") processor = dataset_processor[config["dataset"]]( config["rootdir"], symbols=dataset_symbol[config["dataset"]], cleaner_names=dataset_cleaner[config["dataset"]], ) # check output directories build_dir = lambda x: [ os.makedirs(os.path.join(config["outdir"], x, y), exist_ok=True) for y in ["raw-feats", "wavs", "ids", "raw-f0", "raw-energies"] ] build_dir("train") build_dir("valid") # save pretrained-processor to feature dir processor._save_mapper( os.path.join(config["outdir"], f"{config['dataset']}_mapper.json"), extra_attrs_to_save={"pinyin_dict": processor.pinyin_dict} if config["dataset"] == "baker" else {}, ) # build train test split if config["dataset"] == "libritts": train_split, valid_split, _, _ = train_test_split( processor.items, [i[-1] for i in processor.items], test_size=config["test_size"], random_state=42, shuffle=True, ) else: train_split, valid_split = train_test_split( processor.items, test_size=config["test_size"], random_state=42, shuffle=True, ) logging.info(f"Training items: {len(train_split)}") logging.info(f"Validation items: {len(valid_split)}") get_utt_id = lambda x: os.path.split(x[1])[-1].split(".")[0] train_utt_ids = [get_utt_id(x) for x in train_split] valid_utt_ids = [get_utt_id(x) for x in valid_split] # save train and valid utt_ids to track later np.save(os.path.join(config["outdir"], "train_utt_ids.npy"), train_utt_ids) np.save(os.path.join(config["outdir"], "valid_utt_ids.npy"), valid_utt_ids) # define map iterator def iterator_data(items_list): for item in items_list: yield processor.get_one_sample(item) train_iterator_data = iterator_data(train_split) valid_iterator_data = iterator_data(valid_split) p = Pool(config["n_cpus"]) # preprocess train files and get statistics for normalizing partial_fn = partial(gen_audio_features, config=config) train_map = p.imap_unordered( partial_fn, tqdm(train_iterator_data, total=len(train_split), desc="[Preprocessing train]"), chunksize=10, ) # init scaler for multiple features scaler_mel = StandardScaler(copy=False) scaler_energy = StandardScaler(copy=False) scaler_f0 = StandardScaler(copy=False) id_to_remove = [] for result, mel, energy, f0, features in train_map: if not result: id_to_remove.append(features["utt_id"]) continue save_features_to_file(features, "train", config) # partial fitting of scalers if len(energy[energy != 0]) == 0 or len(f0[f0 != 0]) == 0: id_to_remove.append(features["utt_id"]) continue # partial fitting of scalers if len(energy[energy != 0]) == 0 or len(f0[f0 != 0]) == 0: id_to_remove.append(features["utt_id"]) continue scaler_mel.partial_fit(mel) scaler_energy.partial_fit(energy[energy != 0].reshape(-1, 1)) scaler_f0.partial_fit(f0[f0 != 0].reshape(-1, 1)) if len(id_to_remove) > 0: np.save( os.path.join(config["outdir"], "train_utt_ids.npy"), [i for i in train_utt_ids if i not in id_to_remove], ) logging.info( f"removed {len(id_to_remove)} cause of too many outliers or bad mfa extraction" ) # save statistics to file logging.info("Saving computed statistics.") scaler_list = [(scaler_mel, ""), (scaler_energy, "_energy"), (scaler_f0, "_f0")] save_statistics_to_file(scaler_list, config) # preprocess valid files partial_fn = partial(gen_audio_features, config=config) valid_map = p.imap_unordered( partial_fn, tqdm(valid_iterator_data, total=len(valid_split), desc="[Preprocessing valid]"), chunksize=10, ) for *_, features in valid_map: save_features_to_file(features, "valid", config)
Run preprocessing process and compute statistics for normalizing.
179,500
import argparse import glob import logging import os import yaml import librosa import numpy as np import pyworld as pw from functools import partial from multiprocessing import Pool from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from tqdm import tqdm from tensorflow_tts.processor import LJSpeechProcessor from tensorflow_tts.processor import BakerProcessor from tensorflow_tts.processor import KSSProcessor from tensorflow_tts.processor import LibriTTSProcessor from tensorflow_tts.processor import ThorstenProcessor from tensorflow_tts.processor import LJSpeechUltimateProcessor from tensorflow_tts.processor import SynpaflexProcessor from tensorflow_tts.processor import JSUTProcessor from tensorflow_tts.processor.ljspeech import LJSPEECH_SYMBOLS from tensorflow_tts.processor.baker import BAKER_SYMBOLS from tensorflow_tts.processor.kss import KSS_SYMBOLS from tensorflow_tts.processor.libritts import LIBRITTS_SYMBOLS from tensorflow_tts.processor.thorsten import THORSTEN_SYMBOLS from tensorflow_tts.processor.ljspeechu import LJSPEECH_U_SYMBOLS from tensorflow_tts.processor.synpaflex import SYNPAFLEX_SYMBOLS from tensorflow_tts.processor.jsut import JSUT_SYMBOLS from tensorflow_tts.utils import remove_outlier os.environ["CUDA_VISIBLE_DEVICES"] = "" def parse_and_config(): """Parse arguments and set configuration parameters.""" parser = argparse.ArgumentParser( description="Preprocess audio and text features " "(See detail in tensorflow_tts/bin/preprocess_dataset.py)." ) parser.add_argument( "--rootdir", default=None, type=str, required=True, help="Directory containing the dataset files.", ) parser.add_argument( "--outdir", default=None, type=str, required=True, help="Output directory where features will be saved.", ) parser.add_argument( "--dataset", type=str, default="ljspeech", choices=["ljspeech", "kss", "libritts", "baker", "thorsten", "ljspeechu", "synpaflex", "jsut"], help="Dataset to preprocess.", ) parser.add_argument( "--config", type=str, required=True, help="YAML format configuration file." ) parser.add_argument( "--n_cpus", type=int, default=4, required=False, help="Number of CPUs to use in parallel.", ) parser.add_argument( "--test_size", type=float, default=0.05, required=False, help="Proportion of files to use as test dataset.", ) parser.add_argument( "--verbose", type=int, default=0, choices=[0, 1, 2], help="Logging level. 0: DEBUG, 1: INFO and WARNING, 2: INFO, WARNING, and ERROR", ) args = parser.parse_args() # set logger FORMAT = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s" log_level = {0: logging.DEBUG, 1: logging.WARNING, 2: logging.ERROR} logging.basicConfig(level=log_level[args.verbose], format=FORMAT) # load config config = yaml.load(open(args.config), Loader=yaml.SafeLoader) config.update(vars(args)) # config checks assert config["format"] == "npy", "'npy' is the only supported format." return config def gen_normal_mel(mel_path, scaler, config): """Normalize the mel spectrogram and save it to the corresponding path. Args: mel_path (string): path of the mel spectrogram to normalize. scaler (sklearn.base.BaseEstimator): scaling function to use for normalize. config (Dict): configuration dictionary. """ mel = np.load(mel_path) mel_norm = scaler.transform(mel) path, file_name = os.path.split(mel_path) *_, subdir, suffix = path.split(os.sep) utt_id = file_name.split(f"-{suffix}.npy")[0] np.save( os.path.join( config["outdir"], subdir, "norm-feats", f"{utt_id}-norm-feats.npy" ), mel_norm.astype(np.float32), allow_pickle=False, ) The provided code snippet includes necessary dependencies for implementing the `normalize` function. Write a Python function `def normalize()` to solve the following problem: Normalize mel spectrogram with pre-computed statistics. Here is the function: def normalize(): """Normalize mel spectrogram with pre-computed statistics.""" config = parse_and_config() if config["format"] == "npy": # init scaler with saved values scaler = StandardScaler() scaler.mean_, scaler.scale_ = np.load( os.path.join(config["outdir"], "stats.npy") ) scaler.n_features_in_ = config["num_mels"] else: raise ValueError("'npy' is the only supported format.") # find all "raw-feats" files in both train and valid folders glob_path = os.path.join(config["rootdir"], "**", "raw-feats", "*.npy") mel_raw_feats = glob.glob(glob_path, recursive=True) logging.info(f"Files to normalize: {len(mel_raw_feats)}") # check for output directories os.makedirs(os.path.join(config["outdir"], "train", "norm-feats"), exist_ok=True) os.makedirs(os.path.join(config["outdir"], "valid", "norm-feats"), exist_ok=True) p = Pool(config["n_cpus"]) partial_fn = partial(gen_normal_mel, scaler=scaler, config=config) list(p.map(partial_fn, tqdm(mel_raw_feats, desc="[Normalizing]")))
Normalize mel spectrogram with pre-computed statistics.
179,501
import argparse import glob import logging import os import yaml import librosa import numpy as np import pyworld as pw from functools import partial from multiprocessing import Pool from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from tqdm import tqdm from tensorflow_tts.processor import LJSpeechProcessor from tensorflow_tts.processor import BakerProcessor from tensorflow_tts.processor import KSSProcessor from tensorflow_tts.processor import LibriTTSProcessor from tensorflow_tts.processor import ThorstenProcessor from tensorflow_tts.processor import LJSpeechUltimateProcessor from tensorflow_tts.processor import SynpaflexProcessor from tensorflow_tts.processor import JSUTProcessor from tensorflow_tts.processor.ljspeech import LJSPEECH_SYMBOLS from tensorflow_tts.processor.baker import BAKER_SYMBOLS from tensorflow_tts.processor.kss import KSS_SYMBOLS from tensorflow_tts.processor.libritts import LIBRITTS_SYMBOLS from tensorflow_tts.processor.thorsten import THORSTEN_SYMBOLS from tensorflow_tts.processor.ljspeechu import LJSPEECH_U_SYMBOLS from tensorflow_tts.processor.synpaflex import SYNPAFLEX_SYMBOLS from tensorflow_tts.processor.jsut import JSUT_SYMBOLS from tensorflow_tts.utils import remove_outlier os.environ["CUDA_VISIBLE_DEVICES"] = "" def parse_and_config(): """Parse arguments and set configuration parameters.""" parser = argparse.ArgumentParser( description="Preprocess audio and text features " "(See detail in tensorflow_tts/bin/preprocess_dataset.py)." ) parser.add_argument( "--rootdir", default=None, type=str, required=True, help="Directory containing the dataset files.", ) parser.add_argument( "--outdir", default=None, type=str, required=True, help="Output directory where features will be saved.", ) parser.add_argument( "--dataset", type=str, default="ljspeech", choices=["ljspeech", "kss", "libritts", "baker", "thorsten", "ljspeechu", "synpaflex", "jsut"], help="Dataset to preprocess.", ) parser.add_argument( "--config", type=str, required=True, help="YAML format configuration file." ) parser.add_argument( "--n_cpus", type=int, default=4, required=False, help="Number of CPUs to use in parallel.", ) parser.add_argument( "--test_size", type=float, default=0.05, required=False, help="Proportion of files to use as test dataset.", ) parser.add_argument( "--verbose", type=int, default=0, choices=[0, 1, 2], help="Logging level. 0: DEBUG, 1: INFO and WARNING, 2: INFO, WARNING, and ERROR", ) args = parser.parse_args() # set logger FORMAT = "%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s" log_level = {0: logging.DEBUG, 1: logging.WARNING, 2: logging.ERROR} logging.basicConfig(level=log_level[args.verbose], format=FORMAT) # load config config = yaml.load(open(args.config), Loader=yaml.SafeLoader) config.update(vars(args)) # config checks assert config["format"] == "npy", "'npy' is the only supported format." return config def save_statistics_to_file(scaler_list, config): """Save computed statistics to disk. Args: scaler_list (List): List of scalers containing statistics to save. config (Dict): configuration dictionary. """ for scaler, name in scaler_list: stats = np.stack((scaler.mean_, scaler.scale_)) np.save( os.path.join(config["outdir"], f"stats{name}.npy"), stats.astype(np.float32), allow_pickle=False, ) The provided code snippet includes necessary dependencies for implementing the `compute_statistics` function. Write a Python function `def compute_statistics()` to solve the following problem: Compute mean / std statistics of some features for later normalization. Here is the function: def compute_statistics(): """Compute mean / std statistics of some features for later normalization.""" config = parse_and_config() # find features files for the train split glob_fn = lambda x: glob.glob(os.path.join(config["rootdir"], "train", x, "*.npy")) glob_mel = glob_fn("raw-feats") glob_f0 = glob_fn("raw-f0") glob_energy = glob_fn("raw-energies") assert ( len(glob_mel) == len(glob_f0) == len(glob_energy) ), "Features, f0 and energies have different files in training split." logging.info(f"Computing statistics for {len(glob_mel)} files.") # init scaler for multiple features scaler_mel = StandardScaler(copy=False) scaler_energy = StandardScaler(copy=False) scaler_f0 = StandardScaler(copy=False) for mel, f0, energy in tqdm( zip(glob_mel, glob_f0, glob_energy), total=len(glob_mel) ): # remove outliers energy = np.load(energy) f0 = np.load(f0) # partial fitting of scalers scaler_mel.partial_fit(np.load(mel)) scaler_energy.partial_fit(energy[energy != 0].reshape(-1, 1)) scaler_f0.partial_fit(f0[f0 != 0].reshape(-1, 1)) # save statistics to file logging.info("Saving computed statistics.") scaler_list = [(scaler_mel, ""), (scaler_energy, "_energy"), (scaler_f0, "_f0")] save_statistics_to_file(scaler_list, config)
Compute mean / std statistics of some features for later normalization.
179,502
import numpy as np import tensorflow as tf from scipy.signal import kaiser from tensorflow_tts.models import BaseModel from tensorflow_tts.models import TFMelGANGenerator The provided code snippet includes necessary dependencies for implementing the `design_prototype_filter` function. Write a Python function `def design_prototype_filter(taps=62, cutoff_ratio=0.15, beta=9.0)` to solve the following problem: Design prototype filter for PQMF. This method is based on `A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`_. Args: taps (int): The number of filter taps. cutoff_ratio (float): Cut-off frequency ratio. beta (float): Beta coefficient for kaiser window. Returns: ndarray: Impluse response of prototype filter (taps + 1,). .. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`: https://ieeexplore.ieee.org/abstract/document/681427 Here is the function: def design_prototype_filter(taps=62, cutoff_ratio=0.15, beta=9.0): """Design prototype filter for PQMF. This method is based on `A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`_. Args: taps (int): The number of filter taps. cutoff_ratio (float): Cut-off frequency ratio. beta (float): Beta coefficient for kaiser window. Returns: ndarray: Impluse response of prototype filter (taps + 1,). .. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`: https://ieeexplore.ieee.org/abstract/document/681427 """ # check the arguments are valid assert taps % 2 == 0, "The number of taps mush be even number." assert 0.0 < cutoff_ratio < 1.0, "Cutoff ratio must be > 0.0 and < 1.0." # make initial filter omega_c = np.pi * cutoff_ratio with np.errstate(invalid="ignore"): h_i = np.sin(omega_c * (np.arange(taps + 1) - 0.5 * taps)) / ( np.pi * (np.arange(taps + 1) - 0.5 * taps) ) # fix nan due to indeterminate form h_i[taps // 2] = np.cos(0) * cutoff_ratio # apply kaiser window w = kaiser(taps + 1, beta) h = h_i * w return h
Design prototype filter for PQMF. This method is based on `A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`_. Args: taps (int): The number of filter taps. cutoff_ratio (float): Cut-off frequency ratio. beta (float): Beta coefficient for kaiser window. Returns: ndarray: Impluse response of prototype filter (taps + 1,). .. _`A Kaiser window approach for the design of prototype filters of cosine modulated filterbanks`: https://ieeexplore.ieee.org/abstract/document/681427
179,503
import tensorflow as tf from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `get_initializer` function. Write a Python function `def get_initializer(initializer_seed=42)` to solve the following problem: Creates a `tf.initializers.he_normal` with the given seed. Args: initializer_seed: int, initializer seed. Returns: HeNormal initializer with seed = `initializer_seed`. Here is the function: def get_initializer(initializer_seed=42): """Creates a `tf.initializers.he_normal` with the given seed. Args: initializer_seed: int, initializer seed. Returns: HeNormal initializer with seed = `initializer_seed`. """ return tf.keras.initializers.he_normal(seed=initializer_seed)
Creates a `tf.initializers.he_normal` with the given seed. Args: initializer_seed: int, initializer seed. Returns: HeNormal initializer with seed = `initializer_seed`.
179,504
import numpy as np import tensorflow as tf from tensorflow_tts.models import BaseModel from tensorflow_tts.utils import GroupConv1D, WeightNormalization The provided code snippet includes necessary dependencies for implementing the `get_initializer` function. Write a Python function `def get_initializer(initializer_seed=42)` to solve the following problem: Creates a `tf.initializers.glorot_normal` with the given seed. Args: initializer_seed: int, initializer seed. Returns: GlorotNormal initializer with seed = `initializer_seed`. Here is the function: def get_initializer(initializer_seed=42): """Creates a `tf.initializers.glorot_normal` with the given seed. Args: initializer_seed: int, initializer seed. Returns: GlorotNormal initializer with seed = `initializer_seed`. """ return tf.keras.initializers.GlorotNormal(seed=initializer_seed)
Creates a `tf.initializers.glorot_normal` with the given seed. Args: initializer_seed: int, initializer seed. Returns: GlorotNormal initializer with seed = `initializer_seed`.
179,505
import numpy as np import tensorflow as tf from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `get_initializer` function. Write a Python function `def get_initializer(initializer_range=0.02)` to solve the following problem: Creates a `tf.initializers.truncated_normal` with the given range. Args: initializer_range: float, initializer range for stddev. Returns: TruncatedNormal initializer with stddev = `initializer_range`. Here is the function: def get_initializer(initializer_range=0.02): """Creates a `tf.initializers.truncated_normal` with the given range. Args: initializer_range: float, initializer range for stddev. Returns: TruncatedNormal initializer with stddev = `initializer_range`. """ return tf.keras.initializers.TruncatedNormal(stddev=initializer_range)
Creates a `tf.initializers.truncated_normal` with the given range. Args: initializer_range: float, initializer range for stddev. Returns: TruncatedNormal initializer with stddev = `initializer_range`.
179,506
import numpy as np import tensorflow as tf from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `gelu` function. Write a Python function `def gelu(x)` to solve the following problem: Gaussian Error Linear unit. Here is the function: def gelu(x): """Gaussian Error Linear unit.""" cdf = 0.5 * (1.0 + tf.math.erf(x / tf.math.sqrt(2.0))) return x * cdf
Gaussian Error Linear unit.
179,507
import numpy as np import tensorflow as tf from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `gelu_new` function. Write a Python function `def gelu_new(x)` to solve the following problem: Smoother gaussian Error Linear Unit. Here is the function: def gelu_new(x): """Smoother gaussian Error Linear Unit.""" cdf = 0.5 * (1.0 + tf.tanh((np.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3))))) return x * cdf
Smoother gaussian Error Linear Unit.
179,508
import numpy as np import tensorflow as tf from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `swish` function. Write a Python function `def swish(x)` to solve the following problem: Swish activation function. Here is the function: def swish(x): """Swish activation function.""" return tf.nn.swish(x)
Swish activation function.
179,509
import numpy as np import tensorflow as tf from tensorflow_tts.models import BaseModel def mish(x): return x * tf.math.tanh(tf.math.softplus(x))
null
179,510
import collections import numpy as np import tensorflow as tf from tensorflow_addons.seq2seq import BahdanauAttention, Decoder, Sampler from tensorflow_tts.utils import dynamic_decode from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `get_initializer` function. Write a Python function `def get_initializer(initializer_range=0.02)` to solve the following problem: Creates a `tf.initializers.truncated_normal` with the given range. Args: initializer_range: float, initializer range for stddev. Returns: TruncatedNormal initializer with stddev = `initializer_range`. Here is the function: def get_initializer(initializer_range=0.02): """Creates a `tf.initializers.truncated_normal` with the given range. Args: initializer_range: float, initializer range for stddev. Returns: TruncatedNormal initializer with stddev = `initializer_range`. """ return tf.keras.initializers.TruncatedNormal(stddev=initializer_range)
Creates a `tf.initializers.truncated_normal` with the given range. Args: initializer_range: float, initializer range for stddev. Returns: TruncatedNormal initializer with stddev = `initializer_range`.
179,511
import collections import numpy as np import tensorflow as tf from tensorflow_addons.seq2seq import BahdanauAttention, Decoder, Sampler from tensorflow_tts.utils import dynamic_decode from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `gelu` function. Write a Python function `def gelu(x)` to solve the following problem: Gaussian Error Linear unit. Here is the function: def gelu(x): """Gaussian Error Linear unit.""" cdf = 0.5 * (1.0 + tf.math.erf(x / tf.math.sqrt(2.0))) return x * cdf
Gaussian Error Linear unit.
179,512
import collections import numpy as np import tensorflow as tf from tensorflow_addons.seq2seq import BahdanauAttention, Decoder, Sampler from tensorflow_tts.utils import dynamic_decode from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `gelu_new` function. Write a Python function `def gelu_new(x)` to solve the following problem: Smoother gaussian Error Linear Unit. Here is the function: def gelu_new(x): """Smoother gaussian Error Linear Unit.""" cdf = 0.5 * (1.0 + tf.tanh((np.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3))))) return x * cdf
Smoother gaussian Error Linear Unit.
179,513
import collections import numpy as np import tensorflow as tf from tensorflow_addons.seq2seq import BahdanauAttention, Decoder, Sampler from tensorflow_tts.utils import dynamic_decode from tensorflow_tts.models import BaseModel The provided code snippet includes necessary dependencies for implementing the `swish` function. Write a Python function `def swish(x)` to solve the following problem: Swish activation function. Here is the function: def swish(x): """Swish activation function.""" return tf.nn.swish(x)
Swish activation function.
179,514
import collections import numpy as np import tensorflow as tf from tensorflow_addons.seq2seq import BahdanauAttention, Decoder, Sampler from tensorflow_tts.utils import dynamic_decode from tensorflow_tts.models import BaseModel def mish(x): return x * tf.math.tanh(tf.math.softplus(x))
null
179,515
import fnmatch import os import re import tempfile from pathlib import Path import tensorflow as tf The provided code snippet includes necessary dependencies for implementing the `find_files` function. Write a Python function `def find_files(root_dir, query="*.wav", include_root_dir=True)` to solve the following problem: Find files recursively. Args: root_dir (str): Root root_dir to find. query (str): Query to find. include_root_dir (bool): If False, root_dir name is not included. Returns: list: List of found filenames. Here is the function: def find_files(root_dir, query="*.wav", include_root_dir=True): """Find files recursively. Args: root_dir (str): Root root_dir to find. query (str): Query to find. include_root_dir (bool): If False, root_dir name is not included. Returns: list: List of found filenames. """ files = [] for root, _, filenames in os.walk(root_dir, followlinks=True): for filename in fnmatch.filter(filenames, query): files.append(os.path.join(root, filename)) if not include_root_dir: files = [file_.replace(root_dir + "/", "") for file_ in files] return files
Find files recursively. Args: root_dir (str): Root root_dir to find. query (str): Query to find. include_root_dir (bool): If False, root_dir name is not included. Returns: list: List of found filenames.
179,516
import fnmatch import os import re import tempfile from pathlib import Path import tensorflow as tf def _path_requires_gfile(filepath): """Checks if the given path requires use of GFile API. Args: filepath (str): Path to check. Returns: bool: True if the given path needs GFile API to access, such as "s3://some/path" and "gs://some/path". """ # If the filepath contains a protocol (e.g. "gs://"), it should be handled # using TensorFlow GFile API. return bool(re.match(r"^[a-z]+://", filepath)) The provided code snippet includes necessary dependencies for implementing the `save_weights` function. Write a Python function `def save_weights(model, filepath)` to solve the following problem: Save model weights. Same as model.save_weights(filepath), but supports saving to S3 or GCS buckets using TensorFlow GFile API. Args: model (tf.keras.Model): Model to save. filepath (str): Path to save the model weights to. Here is the function: def save_weights(model, filepath): """Save model weights. Same as model.save_weights(filepath), but supports saving to S3 or GCS buckets using TensorFlow GFile API. Args: model (tf.keras.Model): Model to save. filepath (str): Path to save the model weights to. """ if not _path_requires_gfile(filepath): model.save_weights(filepath) return # Save to a local temp file and copy to the desired path using GFile API. _, ext = os.path.splitext(filepath) with tempfile.NamedTemporaryFile(suffix=ext) as temp_file: model.save_weights(temp_file.name) # To preserve the original semantics, we need to overwrite the target # file. tf.io.gfile.copy(temp_file.name, filepath, overwrite=True)
Save model weights. Same as model.save_weights(filepath), but supports saving to S3 or GCS buckets using TensorFlow GFile API. Args: model (tf.keras.Model): Model to save. filepath (str): Path to save the model weights to.
179,517
import fnmatch import os import re import tempfile from pathlib import Path import tensorflow as tf def _path_requires_gfile(filepath): """Checks if the given path requires use of GFile API. Args: filepath (str): Path to check. Returns: bool: True if the given path needs GFile API to access, such as "s3://some/path" and "gs://some/path". """ # If the filepath contains a protocol (e.g. "gs://"), it should be handled # using TensorFlow GFile API. return bool(re.match(r"^[a-z]+://", filepath)) The provided code snippet includes necessary dependencies for implementing the `load_weights` function. Write a Python function `def load_weights(model, filepath)` to solve the following problem: Load model weights. Same as model.load_weights(filepath), but supports loading from S3 or GCS buckets using TensorFlow GFile API. Args: model (tf.keras.Model): Model to load weights to. filepath (str): Path to the weights file. Here is the function: def load_weights(model, filepath): """Load model weights. Same as model.load_weights(filepath), but supports loading from S3 or GCS buckets using TensorFlow GFile API. Args: model (tf.keras.Model): Model to load weights to. filepath (str): Path to the weights file. """ if not _path_requires_gfile(filepath): model.load_weights(filepath) return # Make a local copy and load it. _, ext = os.path.splitext(filepath) with tempfile.NamedTemporaryFile(suffix=ext) as temp_file: # The target temp_file should be created above, so we need to overwrite. tf.io.gfile.copy(filepath, temp_file.name, overwrite=True) model.load_weights(temp_file.name)
Load model weights. Same as model.load_weights(filepath), but supports loading from S3 or GCS buckets using TensorFlow GFile API. Args: model (tf.keras.Model): Model to load weights to. filepath (str): Path to the weights file.
179,518
import re from tensorflow_tts.utils.korean import tokenize as ko_tokenize from tensorflow_tts.utils.number_norm import normalize_numbers from unidecode import unidecode def lowercase(text): return text.lower() def collapse_whitespace(text): return re.sub(_whitespace_re, " ", text) The provided code snippet includes necessary dependencies for implementing the `basic_cleaners` function. Write a Python function `def basic_cleaners(text)` to solve the following problem: Basic pipeline that lowercases and collapses whitespace without transliteration. Here is the function: def basic_cleaners(text): """Basic pipeline that lowercases and collapses whitespace without transliteration.""" text = lowercase(text) text = collapse_whitespace(text) return text
Basic pipeline that lowercases and collapses whitespace without transliteration.
179,519
import re from tensorflow_tts.utils.korean import tokenize as ko_tokenize from tensorflow_tts.utils.number_norm import normalize_numbers from unidecode import unidecode def lowercase(text): return text.lower() def collapse_whitespace(text): return re.sub(_whitespace_re, " ", text) def convert_to_ascii(text): return unidecode(text) The provided code snippet includes necessary dependencies for implementing the `transliteration_cleaners` function. Write a Python function `def transliteration_cleaners(text)` to solve the following problem: Pipeline for non-English text that transliterates to ASCII. Here is the function: def transliteration_cleaners(text): """Pipeline for non-English text that transliterates to ASCII.""" text = convert_to_ascii(text) text = lowercase(text) text = collapse_whitespace(text) return text
Pipeline for non-English text that transliterates to ASCII.
179,520
import re from tensorflow_tts.utils.korean import tokenize as ko_tokenize from tensorflow_tts.utils.number_norm import normalize_numbers from unidecode import unidecode def expand_abbreviations(text): for regex, replacement in _abbreviations: text = re.sub(regex, replacement, text) return text def expand_numbers(text): return normalize_numbers(text) def lowercase(text): return text.lower() def collapse_whitespace(text): return re.sub(_whitespace_re, " ", text) def convert_to_ascii(text): return unidecode(text) The provided code snippet includes necessary dependencies for implementing the `english_cleaners` function. Write a Python function `def english_cleaners(text)` to solve the following problem: Pipeline for English text, including number and abbreviation expansion. Here is the function: def english_cleaners(text): """Pipeline for English text, including number and abbreviation expansion.""" text = convert_to_ascii(text) text = lowercase(text) text = expand_numbers(text) text = expand_abbreviations(text) text = collapse_whitespace(text) return text
Pipeline for English text, including number and abbreviation expansion.
179,521
import re from tensorflow_tts.utils.korean import tokenize as ko_tokenize from tensorflow_tts.utils.number_norm import normalize_numbers from unidecode import unidecode The provided code snippet includes necessary dependencies for implementing the `korean_cleaners` function. Write a Python function `def korean_cleaners(text)` to solve the following problem: Pipeline for Korean text, including number and abbreviation expansion. Here is the function: def korean_cleaners(text): """Pipeline for Korean text, including number and abbreviation expansion.""" text = ko_tokenize( text ) # '존경하는' --> ['ᄌ', 'ᅩ', 'ᆫ', 'ᄀ', 'ᅧ', 'ᆼ', 'ᄒ', 'ᅡ', 'ᄂ', 'ᅳ', 'ᆫ'] return text
Pipeline for Korean text, including number and abbreviation expansion.
179,522
import re from tensorflow_tts.utils.korean import tokenize as ko_tokenize from tensorflow_tts.utils.number_norm import normalize_numbers from unidecode import unidecode The provided code snippet includes necessary dependencies for implementing the `german_cleaners` function. Write a Python function `def german_cleaners(text)` to solve the following problem: Pipeline for German text, including number and abbreviation expansion. Here is the function: def german_cleaners(text): """Pipeline for German text, including number and abbreviation expansion.""" try: text = GermanTransliterate(replace={';': ',', ':': ' '}, sep_abbreviation=' -- ').transliterate(text) except NameError: raise ModuleNotFoundError("Install german_transliterate package to use german_cleaners") return text
Pipeline for German text, including number and abbreviation expansion.
179,523
import numpy as np def is_outlier(x, p25, p75): """Check if value is an outlier.""" lower = p25 - 1.5 * (p75 - p25) upper = p75 + 1.5 * (p75 - p25) return x <= lower or x >= upper The provided code snippet includes necessary dependencies for implementing the `remove_outlier` function. Write a Python function `def remove_outlier(x, p_bottom: int = 25, p_top: int = 75)` to solve the following problem: Remove outlier from x. Here is the function: def remove_outlier(x, p_bottom: int = 25, p_top: int = 75): """Remove outlier from x.""" p_bottom = np.percentile(x, p_bottom) p_top = np.percentile(x, p_top) indices_of_outliers = [] for ind, value in enumerate(x): if is_outlier(value, p_bottom, p_top): indices_of_outliers.append(ind) x[indices_of_outliers] = 0.0 # replace by mean f0. x[indices_of_outliers] = np.max(x) return x
Remove outlier from x.
179,524
import os import librosa import numpy as np import soundfile as sf import tensorflow as tf from sklearn.preprocessing import StandardScaler The provided code snippet includes necessary dependencies for implementing the `griffin_lim_lb` function. Write a Python function `def griffin_lim_lb( mel_spec, stats_path, dataset_config, n_iter=32, output_dir=None, wav_name="lb" )` to solve the following problem: Generate wave from mel spectrogram with Griffin-Lim algorithm using Librosa. Args: mel_spec (ndarray): array representing the mel spectrogram. stats_path (str): path to the `stats.npy` file containing norm statistics. dataset_config (Dict): dataset configuration parameters. n_iter (int): number of iterations for GL. output_dir (str): output directory where audio file will be saved. wav_name (str): name of the output file. Returns: gl_lb (ndarray): generated wave. Here is the function: def griffin_lim_lb( mel_spec, stats_path, dataset_config, n_iter=32, output_dir=None, wav_name="lb" ): """Generate wave from mel spectrogram with Griffin-Lim algorithm using Librosa. Args: mel_spec (ndarray): array representing the mel spectrogram. stats_path (str): path to the `stats.npy` file containing norm statistics. dataset_config (Dict): dataset configuration parameters. n_iter (int): number of iterations for GL. output_dir (str): output directory where audio file will be saved. wav_name (str): name of the output file. Returns: gl_lb (ndarray): generated wave. """ scaler = StandardScaler() scaler.mean_, scaler.scale_ = np.load(stats_path) mel_spec = np.power(10.0, scaler.inverse_transform(mel_spec)).T mel_basis = librosa.filters.mel( dataset_config["sampling_rate"], n_fft=dataset_config["fft_size"], n_mels=dataset_config["num_mels"], fmin=dataset_config["fmin"], fmax=dataset_config["fmax"], ) mel_to_linear = np.maximum(1e-10, np.dot(np.linalg.pinv(mel_basis), mel_spec)) gl_lb = librosa.griffinlim( mel_to_linear, n_iter=n_iter, hop_length=dataset_config["hop_size"], win_length=dataset_config["win_length"] or dataset_config["fft_size"], ) if output_dir: output_path = os.path.join(output_dir, f"{wav_name}.wav") sf.write(output_path, gl_lb, dataset_config["sampling_rate"], "PCM_16") return gl_lb
Generate wave from mel spectrogram with Griffin-Lim algorithm using Librosa. Args: mel_spec (ndarray): array representing the mel spectrogram. stats_path (str): path to the `stats.npy` file containing norm statistics. dataset_config (Dict): dataset configuration parameters. n_iter (int): number of iterations for GL. output_dir (str): output directory where audio file will be saved. wav_name (str): name of the output file. Returns: gl_lb (ndarray): generated wave.
179,525
import tensorflow as tf def return_strategy(): physical_devices = tf.config.list_physical_devices("GPU") if len(physical_devices) == 0: return tf.distribute.OneDeviceStrategy(device="/cpu:0") elif len(physical_devices) == 1: return tf.distribute.OneDeviceStrategy(device="/gpu:0") else: return tf.distribute.MirroredStrategy()
null
179,526
import tensorflow as tf The provided code snippet includes necessary dependencies for implementing the `calculate_3d_loss` function. Write a Python function `def calculate_3d_loss(y_gt, y_pred, loss_fn)` to solve the following problem: Calculate 3d loss, normally it's mel-spectrogram loss. Here is the function: def calculate_3d_loss(y_gt, y_pred, loss_fn): """Calculate 3d loss, normally it's mel-spectrogram loss.""" y_gt_T = tf.shape(y_gt)[1] y_pred_T = tf.shape(y_pred)[1] # there is a mismath length when training multiple GPU. # we need slice the longer tensor to make sure the loss # calculated correctly. if y_gt_T > y_pred_T: y_gt = tf.slice(y_gt, [0, 0, 0], [-1, y_pred_T, -1]) elif y_pred_T > y_gt_T: y_pred = tf.slice(y_pred, [0, 0, 0], [-1, y_gt_T, -1]) loss = loss_fn(y_gt, y_pred) if isinstance(loss, tuple) is False: loss = tf.reduce_mean(loss, list(range(1, len(loss.shape)))) # shape = [B] else: loss = list(loss) for i in range(len(loss)): loss[i] = tf.reduce_mean( loss[i], list(range(1, len(loss[i].shape))) ) # shape = [B] return loss
Calculate 3d loss, normally it's mel-spectrogram loss.
179,527
import tensorflow as tf The provided code snippet includes necessary dependencies for implementing the `calculate_2d_loss` function. Write a Python function `def calculate_2d_loss(y_gt, y_pred, loss_fn)` to solve the following problem: Calculate 2d loss, normally it's durrations/f0s/energys loss. Here is the function: def calculate_2d_loss(y_gt, y_pred, loss_fn): """Calculate 2d loss, normally it's durrations/f0s/energys loss.""" y_gt_T = tf.shape(y_gt)[1] y_pred_T = tf.shape(y_pred)[1] # there is a mismath length when training multiple GPU. # we need slice the longer tensor to make sure the loss # calculated correctly. if y_gt_T > y_pred_T: y_gt = tf.slice(y_gt, [0, 0], [-1, y_pred_T]) elif y_pred_T > y_gt_T: y_pred = tf.slice(y_pred, [0, 0], [-1, y_gt_T]) loss = loss_fn(y_gt, y_pred) if isinstance(loss, tuple) is False: loss = tf.reduce_mean(loss, list(range(1, len(loss.shape)))) # shape = [B] else: loss = list(loss) for i in range(len(loss)): loss[i] = tf.reduce_mean( loss[i], list(range(1, len(loss[i].shape))) ) # shape = [B] return loss
Calculate 2d loss, normally it's durrations/f0s/energys loss.
179,528
from typing import Any, Optional, Tuple, Union import tensorflow as tf from tensorflow.python.ops import control_flow_util from tensorflow_addons.seq2seq import Decoder from tensorflow_addons.seq2seq.decoder import ( BaseDecoder, _prepend_batch, _transpose_batch_time, ) from tensorflow_addons.utils.types import Number, TensorLike The provided code snippet includes necessary dependencies for implementing the `dynamic_decode` function. Write a Python function `def dynamic_decode( decoder: Union[Decoder, BaseDecoder], output_time_major: bool = False, impute_finished: bool = False, maximum_iterations: Optional[TensorLike] = None, parallel_iterations: int = 32, swap_memory: bool = False, training: Optional[bool] = None, scope: Optional[str] = None, enable_tflite_convertible: bool = False, **kwargs ) -> Tuple[Any, Any, Any]` to solve the following problem: Perform dynamic decoding with `decoder`. Calls initialize() once and step() repeatedly on the Decoder object. Args: decoder: A `Decoder` instance. output_time_major: Python boolean. Default: `False` (batch major). If `True`, outputs are returned as time major tensors (this mode is faster). Otherwise, outputs are returned as batch major tensors (this adds extra time to the computation). impute_finished: Python boolean. If `True`, then states for batch entries which are marked as finished get copied through and the corresponding outputs get zeroed out. This causes some slowdown at each time step, but ensures that the final state and outputs have the correct values and that backprop ignores time steps that were marked as finished. maximum_iterations: A strictly positive `int32` scalar, the maximum allowed number of decoding steps. Default is `None` (decode until the decoder is fully done). parallel_iterations: Argument passed to `tf.while_loop`. swap_memory: Argument passed to `tf.while_loop`. training: Python boolean. Indicates whether the layer should behave in training mode or in inference mode. Only relevant when `dropout` or `recurrent_dropout` is used. scope: Optional name scope to use. enable_tflite_convertible: Python boolean. If `True`, then the variables of `TensorArray` become of 1-D static shape. Also zero pads in the output tensor will be discarded. Default: `False`. **kwargs: dict, other keyword arguments for dynamic_decode. It might contain arguments for `BaseDecoder` to initialize, which takes all tensor inputs during call(). Returns: `(final_outputs, final_state, final_sequence_lengths)`. Raises: ValueError: if `maximum_iterations` is provided but is not a scalar. Here is the function: def dynamic_decode( decoder: Union[Decoder, BaseDecoder], output_time_major: bool = False, impute_finished: bool = False, maximum_iterations: Optional[TensorLike] = None, parallel_iterations: int = 32, swap_memory: bool = False, training: Optional[bool] = None, scope: Optional[str] = None, enable_tflite_convertible: bool = False, **kwargs ) -> Tuple[Any, Any, Any]: """Perform dynamic decoding with `decoder`. Calls initialize() once and step() repeatedly on the Decoder object. Args: decoder: A `Decoder` instance. output_time_major: Python boolean. Default: `False` (batch major). If `True`, outputs are returned as time major tensors (this mode is faster). Otherwise, outputs are returned as batch major tensors (this adds extra time to the computation). impute_finished: Python boolean. If `True`, then states for batch entries which are marked as finished get copied through and the corresponding outputs get zeroed out. This causes some slowdown at each time step, but ensures that the final state and outputs have the correct values and that backprop ignores time steps that were marked as finished. maximum_iterations: A strictly positive `int32` scalar, the maximum allowed number of decoding steps. Default is `None` (decode until the decoder is fully done). parallel_iterations: Argument passed to `tf.while_loop`. swap_memory: Argument passed to `tf.while_loop`. training: Python boolean. Indicates whether the layer should behave in training mode or in inference mode. Only relevant when `dropout` or `recurrent_dropout` is used. scope: Optional name scope to use. enable_tflite_convertible: Python boolean. If `True`, then the variables of `TensorArray` become of 1-D static shape. Also zero pads in the output tensor will be discarded. Default: `False`. **kwargs: dict, other keyword arguments for dynamic_decode. It might contain arguments for `BaseDecoder` to initialize, which takes all tensor inputs during call(). Returns: `(final_outputs, final_state, final_sequence_lengths)`. Raises: ValueError: if `maximum_iterations` is provided but is not a scalar. """ with tf.name_scope(scope or "decoder"): is_xla = not tf.executing_eagerly() and control_flow_util.GraphOrParentsInXlaContext( tf.compat.v1.get_default_graph() ) if maximum_iterations is not None: maximum_iterations = tf.convert_to_tensor( maximum_iterations, dtype=tf.int32, name="maximum_iterations" ) if maximum_iterations.shape.ndims != 0: raise ValueError("maximum_iterations must be a scalar") tf.debugging.assert_greater( maximum_iterations, 0, message="maximum_iterations should be greater than 0", ) elif is_xla: raise ValueError("maximum_iterations is required for XLA compilation.") if isinstance(decoder, Decoder): initial_finished, initial_inputs, initial_state = decoder.initialize() else: # For BaseDecoder that takes tensor inputs during call. decoder_init_input = kwargs.pop("decoder_init_input", None) decoder_init_kwargs = kwargs.pop("decoder_init_kwargs", {}) initial_finished, initial_inputs, initial_state = decoder.initialize( decoder_init_input, **decoder_init_kwargs ) if enable_tflite_convertible: # Assume the batch_size = 1 for inference. # So we can change 2-D TensorArray into 1-D by reshaping it. zero_outputs = tf.nest.map_structure( lambda shape, dtype: tf.reshape( tf.zeros(_prepend_batch(decoder.batch_size, shape), dtype=dtype), [-1], ), decoder.output_size, decoder.output_dtype, ) else: zero_outputs = tf.nest.map_structure( lambda shape, dtype: tf.zeros( _prepend_batch(decoder.batch_size, shape), dtype=dtype ), decoder.output_size, decoder.output_dtype, ) if maximum_iterations is not None: initial_finished = tf.logical_or(initial_finished, 0 >= maximum_iterations) initial_sequence_lengths = tf.zeros_like(initial_finished, dtype=tf.int32) initial_time = tf.constant(0, dtype=tf.int32) def _shape(batch_size, from_shape): if not isinstance(from_shape, tf.TensorShape) or from_shape.ndims == 0: return None else: batch_size = tf.get_static_value( tf.convert_to_tensor(batch_size, name="batch_size") ) if enable_tflite_convertible: # Since we can't use 2-D TensoArray and assume `batch_size` = 1, # we use `from_shape` dimension only. return from_shape return tf.TensorShape([batch_size]).concatenate(from_shape) dynamic_size = maximum_iterations is None or not is_xla # The dynamic shape `TensoArray` is not allowed in TFLite yet. dynamic_size = dynamic_size and (not enable_tflite_convertible) def _create_ta(s, d): return tf.TensorArray( dtype=d, size=0 if dynamic_size else maximum_iterations, dynamic_size=dynamic_size, element_shape=_shape(decoder.batch_size, s), ) initial_outputs_ta = tf.nest.map_structure( _create_ta, decoder.output_size, decoder.output_dtype ) def condition( unused_time, unused_outputs_ta, unused_state, unused_inputs, finished, unused_sequence_lengths, ): return tf.logical_not(tf.reduce_all(finished)) def body(time, outputs_ta, state, inputs, finished, sequence_lengths): """Internal while_loop body. Args: time: scalar int32 tensor. outputs_ta: structure of TensorArray. state: (structure of) state tensors and TensorArrays. inputs: (structure of) input tensors. finished: bool tensor (keeping track of what's finished). sequence_lengths: int32 tensor (keeping track of time of finish). Returns: `(time + 1, outputs_ta, next_state, next_inputs, next_finished, next_sequence_lengths)`. ``` """ (next_outputs, decoder_state, next_inputs, decoder_finished) = decoder.step( time, inputs, state, training ) decoder_state_sequence_lengths = False if decoder.tracks_own_finished: next_finished = decoder_finished lengths = getattr(decoder_state, "lengths", None) if lengths is not None: # sequence lengths are provided by decoder_state.lengths; # overwrite our sequence lengths. decoder_state_sequence_lengths = True sequence_lengths = tf.cast(lengths, tf.int32) else: next_finished = tf.logical_or(decoder_finished, finished) if decoder_state_sequence_lengths: # Just pass something through the loop; at the next iteration # we'll pull the sequence lengths from the decoder_state again. next_sequence_lengths = sequence_lengths else: next_sequence_lengths = tf.where( tf.logical_not(finished), tf.fill(tf.shape(sequence_lengths), time + 1), sequence_lengths, ) tf.nest.assert_same_structure(state, decoder_state) tf.nest.assert_same_structure(outputs_ta, next_outputs) tf.nest.assert_same_structure(inputs, next_inputs) # Zero out output values past finish if impute_finished: def zero_out_finished(out, zero): if finished.shape.rank < zero.shape.rank: broadcast_finished = tf.broadcast_to( tf.expand_dims(finished, axis=-1), zero.shape ) return tf.where(broadcast_finished, zero, out) else: return tf.where(finished, zero, out) emit = tf.nest.map_structure( zero_out_finished, next_outputs, zero_outputs ) else: emit = next_outputs # Copy through states past finish def _maybe_copy_state(new, cur): # TensorArrays and scalar states get passed through. if isinstance(cur, tf.TensorArray): pass_through = True else: new.set_shape(cur.shape) pass_through = new.shape.ndims == 0 if not pass_through: broadcast_finished = tf.broadcast_to( tf.expand_dims(finished, axis=-1), new.shape ) return tf.where(broadcast_finished, cur, new) else: return new if impute_finished: next_state = tf.nest.map_structure( _maybe_copy_state, decoder_state, state ) else: next_state = decoder_state if enable_tflite_convertible: # Reshape to 1-D. emit = tf.nest.map_structure(lambda x: tf.reshape(x, [-1]), emit) outputs_ta = tf.nest.map_structure( lambda ta, out: ta.write(time, out), outputs_ta, emit ) return ( time + 1, outputs_ta, next_state, next_inputs, next_finished, next_sequence_lengths, ) res = tf.while_loop( condition, body, loop_vars=( initial_time, initial_outputs_ta, initial_state, initial_inputs, initial_finished, initial_sequence_lengths, ), parallel_iterations=parallel_iterations, maximum_iterations=maximum_iterations, swap_memory=swap_memory, ) final_outputs_ta = res[1] final_state = res[2] final_sequence_lengths = res[5] final_outputs = tf.nest.map_structure(lambda ta: ta.stack(), final_outputs_ta) try: final_outputs, final_state = decoder.finalize( final_outputs, final_state, final_sequence_lengths ) except NotImplementedError: pass if not output_time_major: if enable_tflite_convertible: # Reshape the output to the original shape. def _restore_batch(x): return tf.expand_dims(x, [1]) final_outputs = tf.nest.map_structure(_restore_batch, final_outputs) final_outputs = tf.nest.map_structure(_transpose_batch_time, final_outputs) return final_outputs, final_state, final_sequence_lengths
Perform dynamic decoding with `decoder`. Calls initialize() once and step() repeatedly on the Decoder object. Args: decoder: A `Decoder` instance. output_time_major: Python boolean. Default: `False` (batch major). If `True`, outputs are returned as time major tensors (this mode is faster). Otherwise, outputs are returned as batch major tensors (this adds extra time to the computation). impute_finished: Python boolean. If `True`, then states for batch entries which are marked as finished get copied through and the corresponding outputs get zeroed out. This causes some slowdown at each time step, but ensures that the final state and outputs have the correct values and that backprop ignores time steps that were marked as finished. maximum_iterations: A strictly positive `int32` scalar, the maximum allowed number of decoding steps. Default is `None` (decode until the decoder is fully done). parallel_iterations: Argument passed to `tf.while_loop`. swap_memory: Argument passed to `tf.while_loop`. training: Python boolean. Indicates whether the layer should behave in training mode or in inference mode. Only relevant when `dropout` or `recurrent_dropout` is used. scope: Optional name scope to use. enable_tflite_convertible: Python boolean. If `True`, then the variables of `TensorArray` become of 1-D static shape. Also zero pads in the output tensor will be discarded. Default: `False`. **kwargs: dict, other keyword arguments for dynamic_decode. It might contain arguments for `BaseDecoder` to initialize, which takes all tensor inputs during call(). Returns: `(final_outputs, final_state, final_sequence_lengths)`. Raises: ValueError: if `maximum_iterations` is provided but is not a scalar.
179,529
# import ast import json import os import re from jamo import h2j, hangul_to_jamo, j2h, jamo_to_hcj get_mode(char): if is_lead(char): return 0 elif is_vowel(char): return 1 elif is_tail(char): return 2 else: return -1 def _get_text_from_candidates(candidates): if len(candidates) == 0: return "" elif len(candidates) == 1: return jamo_to_hcj(candidates[0]) else: return j2h(**dict(zip(["lead", "vowel", "tail"], candidates))) def jamo_to_korean(text): text = h2j(text) idx = 0 new_text = "" candidates = [] while True: if idx >= len(text): new_text += _get_text_from_candidates(candidates) break char = text[idx] mode = get_mode(char) if mode == 0: new_text += _get_text_from_candidates(candidates) candidates = [char] elif mode == -1: new_text += _get_text_from_candidates(candidates) new_text += char candidates = [] else: candidates.append(char) idx += 1 return new_text
null
179,530
# import ast import json import os import re from jamo import h2j, hangul_to_jamo, j2h, jamo_to_hcj def compare_sentence_with_jamo(text1, text2): return h2j(text1) != h2j(text2)
null
179,531
# import ast import json import os import re from jamo import h2j, hangul_to_jamo, j2h, jamo_to_hcj def tokenize(text, as_id=False): # jamo package에 있는 hangul_to_jamo를 이용하여 한글 string을 초성/중성/종성으로 나눈다. text = normalize(text) tokens = list( hangul_to_jamo(text) ) # '존경하는' --> ['ᄌ', 'ᅩ', 'ᆫ', 'ᄀ', 'ᅧ', 'ᆼ', 'ᄒ', 'ᅡ', 'ᄂ', 'ᅳ', 'ᆫ', '~'] if as_id: return [_symbol_to_id[token] for token in tokens] else: return [token for token in tokens] def tokenizer_fn(iterator): return (token for x in iterator for token in tokenize(x, as_id=False))
null
179,532
import copy import random from dataclasses import dataclass, field from typing import Optional, Dict, Sequence import torch import torch.distributed import transformers from transformers import Trainer from datasets import load_dataset class ModelArguments: model_name_or_path: Optional[str] = field(default="deepseek-ai/deepseek-coder-6.7b-instruct") class DataArguments: data_path: str = field(default=None, metadata={"help": "Path to the training data."}) class TrainingArguments(transformers.TrainingArguments): cache_dir: Optional[str] = field(default=None) optim: str = field(default="adamw_torch") model_max_length: int = field( default=512, metadata={"help": "Maximum sequence length. Sequences will be right padded (and possibly truncated)."}, ) def safe_save_model_for_hf_trainer(trainer: transformers.Trainer, output_dir: str): """Collects the state dict and dump to disk.""" state_dict = trainer.model.state_dict() if trainer.args.should_save: cpu_state_dict = {key: value.cpu() for key, value in state_dict.items()} del state_dict trainer._save(output_dir, state_dict=cpu_state_dict) # noqa class DataCollatorForSupervisedDataset(object): """Collate examples for supervised fine-tuning.""" tokenizer: transformers.PreTrainedTokenizer def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]: input_ids, labels = tuple([instance[key] for instance in instances] for key in ("input_ids", "labels")) input_ids = [torch.tensor(x) for x in input_ids] input_ids = torch.nn.utils.rnn.pad_sequence( input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id ) labels = [torch.tensor(x) for x in labels] labels = torch.nn.utils.rnn.pad_sequence(labels, batch_first=True, padding_value=IGNORE_INDEX) return dict( input_ids=input_ids, labels=labels, attention_mask=input_ids.ne(self.tokenizer.pad_token_id), ) def train_tokenize_function(examples, tokenizer): sources = [ build_instruction_prompt(instruction) for instruction in examples['instruction'] ] targets = [f"{output}\n{EOT_TOKEN}" for output in examples['output']] data_dict = preprocess(sources, targets, tokenizer) return data_dict def train(): parser = transformers.HfArgumentParser((ModelArguments, DataArguments, TrainingArguments)) model_args, data_args, training_args = parser.parse_args_into_dataclasses() if training_args.local_rank == 0: print('='*100) print(training_args) tokenizer = transformers.AutoTokenizer.from_pretrained( model_args.model_name_or_path, model_max_length=training_args.model_max_length, padding_side="right", use_fast=True, trust_remote_code=True ) print("PAD Token:", tokenizer.pad_token, tokenizer.pad_token_id) print("BOS Token", tokenizer.bos_token, tokenizer.bos_token_id) print("EOS Token", tokenizer.eos_token, tokenizer.eos_token_id) if training_args.local_rank == 0: print("Load tokenizer from {} over.".format(model_args.model_name_or_path)) model = transformers.AutoModelForCausalLM.from_pretrained( model_args.model_name_or_path, torch_dtype=torch.bfloat16 ) if training_args.local_rank == 0: print("Load model from {} over.".format(model_args.model_name_or_path)) raw_train_datasets = load_dataset( 'json', data_files=data_args.data_path, split="train", cache_dir=training_args.cache_dir ) if training_args.local_rank > 0: torch.distributed.barrier() train_dataset = raw_train_datasets.map( train_tokenize_function, batched=True, batch_size=3000, num_proc=32, remove_columns=raw_train_datasets.column_names, load_from_cache_file=True, # not args.overwrite_cache desc="Running Encoding", fn_kwargs={ "tokenizer": tokenizer } ) if training_args.local_rank == 0: torch.distributed.barrier() if training_args.local_rank == 0: print("Training dataset samples:", len(train_dataset)) for index in random.sample(range(len(train_dataset)), 3): print(f"Sample {index} of the training set: {train_dataset[index]['input_ids']}, {train_dataset[index]['labels']}.") print(f"Sample {index} of the training set: {tokenizer.decode(list(train_dataset[index]['input_ids']))}.") data_collator = DataCollatorForSupervisedDataset(tokenizer=tokenizer) data_module = dict(train_dataset=train_dataset, eval_dataset=None, data_collator=data_collator) trainer = Trainer(model=model, tokenizer=tokenizer, args=training_args, **data_module) trainer.train() trainer.save_state() safe_save_model_for_hf_trainer(trainer=trainer, output_dir=training_args.output_dir)
null
179,533
import os from threading import Thread from typing import Iterator import gradio as gr import spaces import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096")) with gr.Blocks(css="style.css") as demo: gr.Markdown(DESCRIPTION) chat_interface.render() def generate( message: str, chat_history: list, system_prompt: str, max_new_tokens: int = 1024, temperature: float = 0.6, top_p: float = 0.9, top_k: int = 50, repetition_penalty: float = 1, ) -> Iterator[str]: conversation = [] if system_prompt: conversation.append({"role": "system", "content": system_prompt}) for user, assistant in chat_history: conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}]) conversation.append({"role": "user", "content": message}) input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt", add_generation_prompt=True) if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH: input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:] gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.") input_ids = input_ids.to(model.device) streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( {"input_ids": input_ids}, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=False, num_beams=1, repetition_penalty=repetition_penalty, eos_token_id=tokenizer.eos_token_id ) t = Thread(target=model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield "".join(outputs).replace("<|EOT|>","")
null
179,534
import argparse import json import os import torch import re from pathlib import Path from tqdm import tqdm data_abs_dir = Path(__file__).parent / "data" from transformers import AutoTokenizer, AutoModelForCausalLM from human_eval.evaluation import evaluate_functional_correctness def read_test_examples(data_path: str): def format_test_example(q, tests, code: str=None): prompt = ">>> Problem:\n{}\n>>> Test Cases:\n{}\n".format(q.strip(), "\n".join(tests)) if code: code = code.replace("\r", "").replace("\t", " ") prompt += "\n>>> Code:\n```python\n{}\n```".format(code) return prompt examples = [json.loads(x) for x in open(data_path)] print("Read all {} examples from {} over!".format(len(examples), data_path)) # test_cases examples_str = [] for i in range(1, 4): ex = examples[i] q, test, code = ex['text'], ex['test_list'], ex['code'] ex_prompt = format_test_example(q, test, code) example_prompt = '- Example {}:\n{}'.format(i, ex_prompt) examples_str += [example_prompt] for i in range(10, 510): ex = examples[i] q, test, code = ex['text'], ex['test_list'], ex['code'] prompt = format_test_example(q, test, code=None) prompt_with_shots = ''' Please refer the given examples and generate a python function for my problem. Examples are listed as follows: {} Here is my problem: {} '''.strip().format('\n\n'.join(examples_str), prompt) yield { 'task_id': ex['task_id'], 'prompt': prompt_with_shots } def generate_one(example, tokenizer, model): prompt = example['prompt'] inputs = tokenizer.apply_chat_template( [{'role': 'user', 'content': prompt }], return_tensors="pt", add_generation_prompt=True ).to(model.device) stop_id = tokenizer.convert_tokens_to_ids("<|EOT|>") assert isinstance(stop_id, int), "Invalid tokenizer, EOT id not found" outputs = model.generate( inputs, max_new_tokens=512, do_sample=False, # top_p=0.95, # temperature=temperature, pad_token_id=stop_id, eos_token_id=stop_id ) output = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True) # print(output) example['gpt_completion'] = output return convert_for_evaluation(example) def evaluate_functional_correctness( input_file: str = None, tmp_dir: str = "./", n_workers: int = 32, timeout: float = 10.0, problem_file: str = "../data/humaneval_python.jsonl.gz", out_dir: str = None, k: List[int] = [1, 10, 100], test_groundtruth: bool = False, example_test: bool = False, is_mbpp: bool = False, language: str = "python", ): """ Evaluates the functional correctness of a model. """ if example_test: print("Example test...") problems = read_dataset(problem_file, dataset_type="humaneval") sample_jsonl = stream_jsonl_all(input_file) with ThreadPoolExecutor(max_workers=n_workers) as executor: futures = [] completion_id = Counter() n_samples = 0 results = defaultdict(list) if test_groundtruth: print("Testing ground truth...") for sample in tqdm(problems.values()): task_id = sample["task_id"] lang = task_id.split("/")[0].lower() if lang == "javascript": lang = "js" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["generation"] = sample["canonical_solution"] sample["test_code"] = process_humaneval_test(sample, problems, example_test, language) if sample["test_code"] is None: continue args = (task_id, sample, lang, timeout, tmp_dir_, completion_id[task_id]) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 else: print("Reading samples...") for sample in tqdm(sample_jsonl): task_id = sample["task_id"] if not is_mbpp: lang = language if not is_mbpp and lang == "javascript": lang = "js" if is_mbpp: lang = "python" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["task_id"] = task_id sample["test_code"] = process_humaneval_test(sample, problems, example_test, is_mbpp, language) if sample["test_code"] is None: continue if "completion_id" in sample: completion_id_ = sample["completion_id"] else: completion_id_ = completion_id[task_id] args = (task_id, sample, lang, timeout, tmp_dir_, completion_id_) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 if len(completion_id) == len(problems): evaluate_pass_at_k = True else: evaluate_pass_at_k = False print("Running test suites...") for future in tqdm(as_completed(futures), total=len(futures)): result = future.result() results[result["task_id"]].append((result["completion_id"], result)) # Calculate pass@k. total, correct = [], [] for result in results.values(): passed = [r[1]["passed"] for r in result] total.append(len(passed)) correct.append(sum(passed)) total = np.array(total) correct = np.array(correct) if evaluate_pass_at_k: ks = k pass_at_k = {f"pass@{k}": estimate_pass_at_k(total, correct, k).mean() for k in ks if (total >= k).all()} print(pass_at_k) else: print("Total:", np.sum(total)) print("Correct:", np.sum(correct)) return pass_at_k def generate_main(args): model_name_or_path = args.model saved_path = args.output_path temp_dir = args.temp_dir os.makedirs(temp_dir, exist_ok=True) problem_file = os.path.join(data_abs_dir, f"mbpp.jsonl") print("model", model_name_or_path) tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) print("load tokenizer {} from {} over.".format(tokenizer.__class__, model_name_or_path)) model = AutoModelForCausalLM.from_pretrained( model_name_or_path, torch_dtype=torch.bfloat16, device_map="auto", ) model.eval() examples = list(read_test_examples(problem_file)) print("Read {} examples for evaluation over.".format(len(examples))) generated_examples = [] for ex in tqdm(examples, desc='Generating'): gen_example = generate_one(ex, tokenizer, model) generated_examples.append(gen_example) print("Generate {}/{} over...".format(len(generated_examples), len(examples))) print("Generate all over!!!") with open(saved_path, 'w', encoding='utf-8') as fw: for ex in generated_examples: fw.write(json.dumps(ex) + '\n') print("Save {} processed examples into {} over!".format(len(generated_examples), saved_path)) result = evaluate_functional_correctness( input_file=saved_path, tmp_dir=temp_dir, problem_file=os.path.join(data_abs_dir, f"mbpp_test.jsonl"), language='python', is_mbpp=True ) print(result, model_name_or_path) pass
null
179,535
import fire import sys from .data import HUMAN_EVAL from .evaluation import evaluate_functional_correctness def evaluate_functional_correctness( input_file: str = None, tmp_dir: str = "./", n_workers: int = 32, timeout: float = 10.0, problem_file: str = "../data/humaneval_python.jsonl.gz", out_dir: str = None, k: List[int] = [1, 10, 100], test_groundtruth: bool = False, example_test: bool = False, is_mbpp: bool = False, language: str = "python", ): """ Evaluates the functional correctness of a model. """ if example_test: print("Example test...") problems = read_dataset(problem_file, dataset_type="humaneval") sample_jsonl = stream_jsonl_all(input_file) with ThreadPoolExecutor(max_workers=n_workers) as executor: futures = [] completion_id = Counter() n_samples = 0 results = defaultdict(list) if test_groundtruth: print("Testing ground truth...") for sample in tqdm(problems.values()): task_id = sample["task_id"] lang = task_id.split("/")[0].lower() if lang == "javascript": lang = "js" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["generation"] = sample["canonical_solution"] sample["test_code"] = process_humaneval_test(sample, problems, example_test, language) if sample["test_code"] is None: continue args = (task_id, sample, lang, timeout, tmp_dir_, completion_id[task_id]) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 else: print("Reading samples...") for sample in tqdm(sample_jsonl): task_id = sample["task_id"] if not is_mbpp: lang = language if not is_mbpp and lang == "javascript": lang = "js" if is_mbpp: lang = "python" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["task_id"] = task_id sample["test_code"] = process_humaneval_test(sample, problems, example_test, is_mbpp, language) if sample["test_code"] is None: continue if "completion_id" in sample: completion_id_ = sample["completion_id"] else: completion_id_ = completion_id[task_id] args = (task_id, sample, lang, timeout, tmp_dir_, completion_id_) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 if len(completion_id) == len(problems): evaluate_pass_at_k = True else: evaluate_pass_at_k = False print("Running test suites...") for future in tqdm(as_completed(futures), total=len(futures)): result = future.result() results[result["task_id"]].append((result["completion_id"], result)) # Calculate pass@k. total, correct = [], [] for result in results.values(): passed = [r[1]["passed"] for r in result] total.append(len(passed)) correct.append(sum(passed)) total = np.array(total) correct = np.array(correct) if evaluate_pass_at_k: ks = k pass_at_k = {f"pass@{k}": estimate_pass_at_k(total, correct, k).mean() for k in ks if (total >= k).all()} print(pass_at_k) else: print("Total:", np.sum(total)) print("Correct:", np.sum(correct)) return pass_at_k The provided code snippet includes necessary dependencies for implementing the `entry_point` function. Write a Python function `def entry_point( sample_file: str, k: str = "1,10,100", n_workers: int = 4, timeout: float = 3.0, problem_file: str = "", is_mbpp: bool = False, )` to solve the following problem: Evaluates the functional correctness of generated samples, and writes results to f"{sample_file}_results.jsonl.gz" Here is the function: def entry_point( sample_file: str, k: str = "1,10,100", n_workers: int = 4, timeout: float = 3.0, problem_file: str = "", is_mbpp: bool = False, ): """ Evaluates the functional correctness of generated samples, and writes results to f"{sample_file}_results.jsonl.gz" """ k = list(map(int, k.split(","))) results = evaluate_functional_correctness(sample_file, k, n_workers, timeout, problem_file, is_mbpp) print(results)
Evaluates the functional correctness of generated samples, and writes results to f"{sample_file}_results.jsonl.gz"
179,536
from typing import Iterable, Dict import gzip import json import os HUMAN_EVAL = os.path.join(ROOT, "..", "data", "HumanEval.jsonl.gz") def stream_jsonl(filename: str) -> Iterable[Dict]: def read_problems(evalset_file: str = HUMAN_EVAL) -> Dict[str, Dict]: return {task["task_id"]: task for task in stream_jsonl(evalset_file)}
null
179,537
from typing import Iterable, Dict import gzip import json import os The provided code snippet includes necessary dependencies for implementing the `write_jsonl` function. Write a Python function `def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False)` to solve the following problem: Writes an iterable of dictionaries to jsonl Here is the function: def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False): """ Writes an iterable of dictionaries to jsonl """ if append: mode = 'ab' else: mode = 'wb' filename = os.path.expanduser(filename) if filename.endswith(".gz"): with open(filename, mode) as fp: with gzip.GzipFile(fileobj=fp, mode='wb') as gzfp: for x in data: gzfp.write((json.dumps(x) + "\n").encode('utf-8')) else: with open(filename, mode) as fp: for x in data: fp.write((json.dumps(x) + "\n").encode('utf-8'))
Writes an iterable of dictionaries to jsonl
179,538
def _clean_python_code_for_sft(code): code = code.replace("\r", "") if "```python" in code: code_start_idx = code.index("```python") code = code[code_start_idx:].replace("```python", "").strip() end_idx = code.find("```") if "```" in code else len(code) code = code[:end_idx].strip() return code def _truncate_code_at_stopwords(code, stop_words): min_stop_idx = len(code) for stop_word in stop_words: stop_index = code.find(stop_word) if 0 <= stop_index < min_stop_idx: min_stop_idx = stop_index return code[:min_stop_idx] The provided code snippet includes necessary dependencies for implementing the `cleanup_code` function. Write a Python function `def cleanup_code( code: str, language_type: str = None, dataset: str = None, issft: bool = False, stop_words = [] )` to solve the following problem: Cleans up the generated code. Here is the function: def cleanup_code( code: str, language_type: str = None, dataset: str = None, issft: bool = False, stop_words = [] ): """ Cleans up the generated code. """ if language_type.lower() == "python": if issft: code = _clean_python_code_for_sft(code) stop_words = ["\ndef", "\nclass", "\nif", "\n#", "\nprint"] code = _truncate_code_at_stopwords(code, stop_words) elif language_type.lower() == "ts": code = _truncate_code_at_stopwords(code, stop_words + ["\nexport", "\nimport", "\nexport default", "\nimport default", "\nconsole.log"]) else: code = _truncate_code_at_stopwords(code, stop_words) return code
Cleans up the generated code.
179,539
from vllm import LLM, SamplingParams import json from transformers import AutoTokenizer from pathlib import Path def generate_batch(examples, tokenizer, llm, model: str): stop = None if model == 'deepseekcoder-instruct': prompts = [ tokenizer.apply_chat_template([{'role': 'user', 'content': ex['prompt_sft'] }], tokenize=False, add_generation_prompt=True) for ex in examples ] else: raise NotImplementedError() # Create a sampling params object. sampling_params = SamplingParams( temperature=0.0, # top_p=0.95, max_tokens=1024, stop=stop ) print("Sample prompt: {}".format(prompts[0])) outputs = llm.generate(prompts, sampling_params) for i in range(len(examples)): examples[i]['output'] = outputs[i].outputs[0].text return examples def generate_main(data_path: str, model_name_or_path: str, saved_path: str, model_type: str='deepseekcoder-instruct', cot: bool=False): examples = [json.loads(x) for x in open(data_path).readlines()] def _convert_for_sft(ex): ex['prompt_sft'] = ex["prompt_sft"] + "\nYou need first write a step-by-step outline and then write the code." return ex if cot: examples = [_convert_for_sft(x) for x in examples] saved_path = saved_path.replace(".jsonl", ".cot.jsonl") print(model_type) print("Model `{}`, COT = {}:{}".format(model_type, cot, model_name_or_path)) print("Saved path: {}".format(saved_path)) tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True) print("load tokenizer {} from {} over.".format(tokenizer.__class__, model_name_or_path)) # Create an LLM. llm = LLM( model=model_name_or_path, pipeline_parallel_size=1, tensor_parallel_size=8, max_num_seqs=512, max_num_batched_tokens=8192, max_model_len=4096, gpu_memory_utilization=0.85, trust_remote_code=True ) generated_examples = generate_batch(examples, tokenizer, llm, model_type) print("Generate all over!!!") with open(saved_path, 'w', encoding='utf-8') as fw: for ex in generated_examples: fw.write(json.dumps(ex) + '\n') print("Save {} processed examples into {} over!".format(len(generated_examples), saved_path))
null
179,540
import re import json from pathlib import Path from collections import defaultdict from human_eval.evaluation import evaluate_functional_correctness version = "20240121-Jul" DATA_DIR = Path(__file__).parent / "data" def extract_python_code(generation: str): generation = generation.replace("[PYTHON]", '```python').replace("[/PYTHON]", '```') if '```python' in generation: p_code = re.compile(r'```python\n(.*?)\n```', flags=re.DOTALL) code_block = p_code.findall(generation)[0] return code_block else: codelist = re.split("\ndef|\nclass|\nif|\n#|\nprint", generation) return codelist[0] def evaluate_functional_correctness( input_file: str = None, tmp_dir: str = "./", n_workers: int = 32, timeout: float = 10.0, problem_file: str = "../data/humaneval_python.jsonl.gz", out_dir: str = None, k: List[int] = [1, 10, 100], test_groundtruth: bool = False, example_test: bool = False, is_mbpp: bool = False, language: str = "python", ): """ Evaluates the functional correctness of a model. """ if example_test: print("Example test...") problems = read_dataset(problem_file, dataset_type="humaneval") sample_jsonl = stream_jsonl_all(input_file) with ThreadPoolExecutor(max_workers=n_workers) as executor: futures = [] completion_id = Counter() n_samples = 0 results = defaultdict(list) if test_groundtruth: print("Testing ground truth...") for sample in tqdm(problems.values()): task_id = sample["task_id"] lang = task_id.split("/")[0].lower() if lang == "javascript": lang = "js" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["generation"] = sample["canonical_solution"] sample["test_code"] = process_humaneval_test(sample, problems, example_test, language) if sample["test_code"] is None: continue args = (task_id, sample, lang, timeout, tmp_dir_, completion_id[task_id]) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 else: print("Reading samples...") for sample in tqdm(sample_jsonl): task_id = sample["task_id"] if not is_mbpp: lang = language if not is_mbpp and lang == "javascript": lang = "js" if is_mbpp: lang = "python" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["task_id"] = task_id sample["test_code"] = process_humaneval_test(sample, problems, example_test, is_mbpp, language) if sample["test_code"] is None: continue if "completion_id" in sample: completion_id_ = sample["completion_id"] else: completion_id_ = completion_id[task_id] args = (task_id, sample, lang, timeout, tmp_dir_, completion_id_) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 if len(completion_id) == len(problems): evaluate_pass_at_k = True else: evaluate_pass_at_k = False print("Running test suites...") for future in tqdm(as_completed(futures), total=len(futures)): result = future.result() results[result["task_id"]].append((result["completion_id"], result)) # Calculate pass@k. total, correct = [], [] for result in results.values(): passed = [r[1]["passed"] for r in result] total.append(len(passed)) correct.append(sum(passed)) total = np.array(total) correct = np.array(correct) if evaluate_pass_at_k: ks = k pass_at_k = {f"pass@{k}": estimate_pass_at_k(total, correct, k).mean() for k in ks if (total >= k).all()} print(pass_at_k) else: print("Total:", np.sum(total)) print("Correct:", np.sum(correct)) return pass_at_k def evaluate_main(generation_path: str, result_path: str, temp_dir: str): problem_path = (DATA_DIR / f"{version}.jsonl").as_posix() print(problem_path) problems = [json.loads(line) for line in open(problem_path, 'r')] id2problems = { x['task_id']: x for x in problems } results = [json.loads(line) for line in open(generation_path, 'r')] for result in results: if 'task_id' not in result: result['task_id'] = problems[result['index']]['task_id'] if 'generation' not in result: try: if 'output' not in result: result['output'] = result['response'] if result['output'].startswith("\n "): func_code = extract_python_code(result['prompt_sft']).strip() result['generation'] = func_code + '\n' + result['output'] else: result['generation'] = extract_python_code(result['output']) except: result['generation'] = result['output'] with open(result_path, 'w') as fr: for result in results: fr.write(json.dumps(result) + "\n") score = evaluate_functional_correctness( input_file=result_path, tmp_dir=temp_dir, problem_file=problem_path, result_path=result_path ) hardness_results = defaultdict(int) for result in [json.loads(line) for line in open(result_path, 'r')]: problem = id2problems[result['task_id']] hardness = problem['meta']['difficulty'] hardness_results[hardness] += 1 hardness_results[hardness + "_correct"] += result['passed'] print("="*100) print("Evaluate {} over.".format(generation_path)) print("Pass@1: {:.3f}".format(score["pass@1"])) for key in ["Easy", "Medium", "Hard"]: if key.endswith("_correct"): continue acc = hardness_results[key+"_correct"] / hardness_results[key] print("{}: {:.3f}({}/{})".format(key, acc, hardness_results[key+"_correct"], hardness_results[key]))
null
179,541
import contextlib import faulthandler import io import multiprocessing import os import platform import signal import random import subprocess import tempfile import gzip import json from typing import * import traceback java_exec = "" node_exec = "" tsc_exec = "" go_exec = "" php_exec = "" cs_exec = "" def time_limit(seconds: float): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0) def swallow_io(): stream = WriteOnlyStringIO() with contextlib.redirect_stdout(stream): with contextlib.redirect_stderr(stream): with redirect_stdin(stream): yield def create_tempdir(): with tempfile.TemporaryDirectory() as dirname: with chdir(dirname): yield dirname class TimeoutException(Exception): pass def chdir(root): if root == ".": yield return cwd = os.getcwd() os.chdir(root) try: yield except BaseException as exc: raise exc finally: os.chdir(cwd) def reliability_guard(maximum_memory_bytes: Optional[int] = None): """ This disables various destructive functions and prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.) WARNING This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution. """ if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes)) resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes)) if not platform.uname().system == 'Darwin': resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes)) faulthandler.disable() import builtins builtins.exit = None builtins.quit = None import os os.environ['OMP_NUM_THREADS'] = '1' os.kill = None os.system = None os.putenv = None os.remove = None os.removedirs = None os.rmdir = None os.fchdir = None os.setuid = None os.fork = None os.forkpty = None os.killpg = None os.rename = None os.renames = None os.truncate = None os.replace = None os.unlink = None os.fchmod = None os.fchown = None os.chmod = None os.chown = None os.chroot = None os.fchdir = None os.lchflags = None os.lchmod = None os.lchown = None os.getcwd = None os.chdir = None import shutil shutil.rmtree = None shutil.move = None shutil.chown = None import subprocess subprocess.Popen = None # type: ignore __builtins__['help'] = None import sys sys.modules['ipdb'] = None sys.modules['joblib'] = None sys.modules['resource'] = None sys.modules['psutil'] = None sys.modules['tkinter'] = None The provided code snippet includes necessary dependencies for implementing the `check_correctness` function. Write a Python function `def check_correctness( task_id: str, sample: dict, language_type: str, timeout: float = 3.0, tmp_dir: str = None, completion_id: Optional[int] = None, ) -> Dict` to solve the following problem: Evaluates the functional correctness of a completion by running the test suite provided in the problem. Here is the function: def check_correctness( task_id: str, sample: dict, language_type: str, timeout: float = 3.0, tmp_dir: str = None, completion_id: Optional[int] = None, ) -> Dict: """ Evaluates the functional correctness of a completion by running the test suite provided in the problem. """ def unsafe_execute(tmp_dir): random_id = random.randint(1, 100000) if "python" in language_type.lower(): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() try: exec_globals = {} with swallow_io(): with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec(sample["test_code"], exec_globals) result.append("passed") except TimeoutException: result.append("timed out") except AssertionError as e: result.append(f"failed: AssertionError") except BaseException as e: result.append(f"failed: {e}") #print(sample["test_code"]) #print(result) # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir elif "go" in language_type.lower(): assert tmp_dir is not None, "Go should be evaluated in a dir where necessary module files installed." import os import shutil if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) origin_path = os.getcwd() os.chdir(tmp_dir) open(f"main_test.go", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec_result = subprocess.run([f"{go_exec}go", "test", f"-timeout={timeout}s", "main_test.go"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "js" in language_type.lower(): import os import shutil if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) origin_path = os.getcwd() os.chdir(tmp_dir) open(f"test.js", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True) if exec_result.stderr.decode(): err = exec_result.stderr.decode() result.append(f"failed: {err}") elif exec_result.stdout.decode(): err = exec_result.stdout.decode() result.append(f"failed: {err}") else: result.append("passed") except TimeoutException: result.append("timed out") os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "cpp" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"test.cpp", 'w').write(sample["test_code"]) if "162" in task_id: compilation_result = subprocess.run(["/usr/bin/g++", "-std=c++17", "test.cpp", "-lcrypto", "-lssl"], timeout=timeout, capture_output=True) else: compilation_result = subprocess.run(["/usr/bin/g++", "-std=c++17", "test.cpp"], timeout=timeout, capture_output=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result.append(f"failed: compilation error: {err}") else: try: exec_result = None with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec_result = subprocess.run(["./a.out"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") #print(result[-1]) #print(sample["test_code"]) os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "php" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"test.php", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): cmd = f"{php_exec}php -f test.php" exec_result = subprocess.run(cmd, timeout=timeout, capture_output=True, shell=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") print(result[-1]) print(sample["test_code"]) os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "sh" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"test.sh", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): cmd = "/bin/bash test.sh" exec_result = subprocess.run(cmd, timeout=10, capture_output=True, shell=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") #print(result[-1]) #print(sample["test_code"]) os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "ts" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) env = {"PATH": f"{node_exec}:" + subprocess.os.environ["PATH"]} open(f"test.ts", 'w').write(sample["test_code"]) cmd = f"{tsc_exec}tsc test.ts --target ES2015 --lib ES2015,DOM" compilation_result = subprocess.run(cmd, timeout=timeout, capture_output=True, env=env, shell=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result.append(f"failed: compilation error: {err}") else: try: exec_result = None with time_limit(timeout): exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") if result[-1] != "passed": env = {"PATH": f"{node_exec}:" + subprocess.os.environ["PATH"]} cmd = f"{tsc_exec}tsc test.ts" compilation_result = subprocess.run(cmd, timeout=timeout, capture_output=True, env=env, shell=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result[-1] = f"failed: compilation error: {err}" else: try: exec_result = None with time_limit(timeout): exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result[-1] = "passed" else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result[-1] = f"failed: {err}" except TimeoutException: result[-1] = "timed out" os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "cs" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"Program.cs", 'w').write(sample["test_code"]) cmd = f"{cs_exec}mcs -d:DEBUG Program.cs" compilation_result = subprocess.run(cmd, shell=True, capture_output=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result.append(f"failed: compilation error: {err}") else: try: exec_result = None cmd = f"{cs_exec}mono Program.exe" env = dict(MONO_TRACE_LISTENER="Console.Error") with time_limit(timeout): exec_result = subprocess.run(cmd, timeout=timeout, shell=True, capture_output=True, env=env) if "Fail" not in exec_result.stderr.decode(): result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") except Exception as e: result.append(f"failed: {e}") os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "rust" in language_type.lower(): import os WD: str = os.path.dirname(os.path.abspath(__file__)) RUST_DIR: str = os.path.join(WD, "rust") RUST_SRC: str = os.path.join(RUST_DIR, "src") RUST_BIN: str = os.path.join(RUST_SRC, "bin") RUST_TMP_DIR: str = os.path.join(RUST_DIR, "tmp") RUST_LOGS: str = os.path.join(RUST_TMP_DIR, "logs") RUST_EXT: str = ".rs" # Create mandatory tmp directories os.makedirs(RUST_TMP_DIR, exist_ok=True) os.makedirs(RUST_LOGS, exist_ok=True) os.makedirs(RUST_SRC, exist_ok=True) os.makedirs(RUST_BIN, exist_ok=True) with tempfile.NamedTemporaryFile(dir = RUST_BIN, delete=False) as f: #temporal file name file_prefix = sample["task_id"].lower().replace("/", "_") file_name:str = file_prefix +RUST_EXT os.rename(f.name, os.path.join(RUST_BIN, file_name)) # Sample to pure Rust function rust_code: str = sample["test_code"] # dump the rust source code in the target temporal file f.write(rust_code.encode('utf-8')) # Proceed towards Rust binaries compilation. Therefore move to Rust module root dir. os.chdir(RUST_DIR) # Two possible outcomes # Pass OR Fail compilation log_filename: str = file_prefix + ".jsonl" log_path: str = os.path.join(RUST_LOGS, log_filename) cargo_check: str = "cargo check --bin " + file_prefix + " --message-format json >> " + log_path # Compilation build status returned_val_compilation: int # Overwrite file content if os.path.exists(log_path): if(file_size := os.path.getsize(log_path)) >= 0: os.remove(log_path) returned_val_compilation = os.system(cargo_check) else: returned_val_compilation = os.system(cargo_check) # 0 means success if returned_val_compilation == 0: #Execution pipeline cargo_test: str = "cargo test --bin " +file_prefix+ " --message-format json >> " + log_path returned_val_execution = os.system(cargo_test) if returned_val_execution == 0: result.append("passed") else: result.append(f"failed: execution error") else: result.append(f"failed: compilation error") elif "java" in language_type.lower(): assert tmp_dir is not None, "Java should be evaluated in a temporary dir." import os import shutil if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) open(os.path.join(tmp_dir, "Problem.java"), 'w').write(sample["test_code"]) origin_path = os.getcwd() os.system(f"cp ./javatuples-1.2.jar {tmp_dir}/") os.chdir(tmp_dir) res = "failed: unknown error" compile_returncode = -1 for _ in range(5): try: cmd = f"{java_exec}javac -cp javatuples-1.2.jar Problem.java" compilation_result = subprocess.run(cmd, timeout=60, capture_output=True, shell=True) compile_returncode = compilation_result.returncode break except subprocess.TimeoutExpired as e: continue if compile_returncode != 0: res = "failed: compilation error" else: exec_result = None try: # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: cmd = f"{java_exec}java -ea -cp .:javatuples-1.2.jar Problem" exec_result = subprocess.run(cmd, timeout=timeout, capture_output=True, shell=True) if exec_result.returncode == 0: res = "passed" elif exec_result.returncode == 1: if "AssertionError" in exec_result.stderr.decode('unicode-escape'): res = "failed: wrong answer" else: res = f"failed: {exec_result.stderr.decode()}" except subprocess.TimeoutExpired as e: res = "time out" except BaseException as e: res = f"failed: {e}" result.append(res) os.chdir(origin_path) shutil.rmtree(tmp_dir) manager = multiprocessing.Manager() result = manager.list() p = multiprocessing.Process(target=unsafe_execute, args=(tmp_dir,)) p.start() p.join(timeout=timeout + 1) if p.is_alive(): p.kill() if not result: result.append("timed out") return { "task_id" : task_id, "completion_id": completion_id, "result" : result[0], "passed" : result[0] == "passed", "finish" : -1 if "finish" not in sample else sample["finish"], "code" : sample["test_code"] }
Evaluates the functional correctness of a completion by running the test suite provided in the problem.
179,542
from typing import Iterable, Dict import gzip import json import os HUMAN_EVAL = os.path.join(ROOT, "..", "data", "HumanEval.jsonl.gz") def stream_jsonl(filename: str) -> Iterable[Dict]: """ Parses each jsonl line and yields it as a dictionary """ if filename.endswith(".gz"): with open(filename, "rb") as gzfp: with gzip.open(gzfp, 'rt') as fp: for line in fp: if any(not x.isspace() for x in line): yield json.loads(line) else: with open(filename, "r") as fp: for line in fp: if any(not x.isspace() for x in line): yield json.loads(line) def read_problems(evalset_file: str = HUMAN_EVAL) -> Dict[str, Dict]: return {task["task_id"]: task for task in stream_jsonl(evalset_file)}
null
179,544
import os import json import gzip import numpy as np import itertools from typing import * from tqdm.auto import tqdm from collections import defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed from human_eval.data import stream_jsonl from human_eval.execution import check_correctness def read_dataset( data_file: str = None, dataset_type: str = "humaneval", num_shot=None, ) -> Dict: if num_shot is not None: print(f"{num_shot}-shot setting...") if "humaneval" in dataset_type.lower(): if data_file is None: current_path = os.path.dirname(os.path.abspath(__file__)) data_file = os.path.join(current_path, "..", "humaneval-x", "python", "data", "humaneval_python.jsonl.gz") dataset = {task["task_id"]: task for task in stream_jsonl(data_file)} else: raise f"Dataset: {dataset_type} not supported." return dataset def estimate_pass_at_k( num_samples: Union[int, List[int], np.ndarray], num_correct: Union[List[int], np.ndarray], k: int ) -> np.ndarray: """ Estimates pass@k of each problem and returns them in an array. """ def estimator(n: int, c: int, k: int) -> float: """ Calculates 1 - comb(n - c, k) / comb(n, k). """ if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1, n + 1)) if isinstance(num_samples, int): num_samples_it = itertools.repeat(num_samples, len(num_correct)) else: assert len(num_samples) == len(num_correct) num_samples_it = iter(num_samples) return np.array([estimator(int(n), int(c), k) for n, c in zip(num_samples_it, num_correct)]) def process_humaneval_test(sample, problems, example_test=False, is_mbpp=False, language="python"): task_id = sample["task_id"] if is_mbpp: return sample["generation"] + "\n" + "\n".join(problems[task_id]["test"]) #language = task_id.split("/")[0].lower() prompt = sample.get("prompt", "") if example_test and "example_test" in problems[task_id] and problems[task_id]["example_test"] != "": test = problems[task_id]["example_test"] else: test = problems[task_id]["test"] code = sample["generation"] # Pre-process for different languages if language == "python": '''code_ = [] for line in code.split("\n"): if (len(line.strip()) > 0 and line[0] != ' ' and line[0] != '\t'): break code_.append(line) code = "\n".join(code_)''' test_setup = "\n".join(IMPORT_HELPER["python"]) + "\n" test_string = test_setup + code + "\n" + test + "\n" elif language == "cpp": test_set_up = "" for s in IMPORT_HELPER["cpp"]: if s not in prompt: test_set_up += s + "\n" test_string = test_set_up + "\n" + code + "\n" + test elif language == "java": test_string = code + "\n" + test elif language in ["js", "javascript", "ts", "cs", "sh"]: test_string = code + "\n" + test elif language == "go": import_string = problems[task_id]["import"] prompt = prompt.replace(import_string, "") if example_test and "example_test" in problems[task_id]: test = problems[task_id]["example_test"] else: test = problems[task_id]["test"] test_setup = problems[task_id]["test_setup"] other_pkgs = [] for pkg in IMPORT_HELPER["go"]: if pkg not in test_setup: p = pkg.split("/")[-1] if p + "." in code: other_pkgs.append(f"\"{pkg}\"") if other_pkgs: import_other_pkgs = "import (\n" + " ".join([p + "\n" for p in other_pkgs]) + ")" test_string = test_setup + "\n" + import_other_pkgs + "\n" + prompt + code + "\n" + test else: test_string = test_setup + "\n" + prompt + code + "\n" + test elif language == "rust": main = "\nfn main(){ \n } \n" declaration = problems[task_id]["declaration"] test_string = main + declaration + prompt + code + test elif language == "php": test_string = code + "\n" + test + "?>" return test_string def stream_jsonl_all(filename: str) -> Iterable[Dict]: results = [] if filename.endswith(".gz"): fp = gzip.open(open(filename, "rb"), "rt") else: fp = open(filename, "r") for line in fp: if any(not x.isspace() for x in line): results.append(json.loads(line)) fp.close() return results def check_correctness( task_id: str, sample: dict, language_type: str, timeout: float = 3.0, tmp_dir: str = None, completion_id: Optional[int] = None, ) -> Dict: """ Evaluates the functional correctness of a completion by running the test suite provided in the problem. """ def unsafe_execute(tmp_dir): random_id = random.randint(1, 100000) if "python" in language_type.lower(): with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() try: exec_globals = {} with swallow_io(): with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec(sample["test_code"], exec_globals) result.append("passed") except TimeoutException: result.append("timed out") except AssertionError as e: result.append(f"failed: AssertionError") except BaseException as e: result.append(f"failed: {e}") #print(sample["test_code"]) #print(result) # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir elif "go" in language_type.lower(): assert tmp_dir is not None, "Go should be evaluated in a dir where necessary module files installed." import os import shutil if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) origin_path = os.getcwd() os.chdir(tmp_dir) open(f"main_test.go", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec_result = subprocess.run([f"{go_exec}go", "test", f"-timeout={timeout}s", "main_test.go"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "js" in language_type.lower(): import os import shutil if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) origin_path = os.getcwd() os.chdir(tmp_dir) open(f"test.js", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True) if exec_result.stderr.decode(): err = exec_result.stderr.decode() result.append(f"failed: {err}") elif exec_result.stdout.decode(): err = exec_result.stdout.decode() result.append(f"failed: {err}") else: result.append("passed") except TimeoutException: result.append("timed out") os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "cpp" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"test.cpp", 'w').write(sample["test_code"]) if "162" in task_id: compilation_result = subprocess.run(["/usr/bin/g++", "-std=c++17", "test.cpp", "-lcrypto", "-lssl"], timeout=timeout, capture_output=True) else: compilation_result = subprocess.run(["/usr/bin/g++", "-std=c++17", "test.cpp"], timeout=timeout, capture_output=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result.append(f"failed: compilation error: {err}") else: try: exec_result = None with time_limit(timeout): # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: exec_result = subprocess.run(["./a.out"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") #print(result[-1]) #print(sample["test_code"]) os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "php" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"test.php", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): cmd = f"{php_exec}php -f test.php" exec_result = subprocess.run(cmd, timeout=timeout, capture_output=True, shell=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") print(result[-1]) print(sample["test_code"]) os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "sh" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"test.sh", 'w').write(sample["test_code"]) try: exec_result = None with time_limit(timeout): cmd = "/bin/bash test.sh" exec_result = subprocess.run(cmd, timeout=10, capture_output=True, shell=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") #print(result[-1]) #print(sample["test_code"]) os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "ts" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) env = {"PATH": f"{node_exec}:" + subprocess.os.environ["PATH"]} open(f"test.ts", 'w').write(sample["test_code"]) cmd = f"{tsc_exec}tsc test.ts --target ES2015 --lib ES2015,DOM" compilation_result = subprocess.run(cmd, timeout=timeout, capture_output=True, env=env, shell=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result.append(f"failed: compilation error: {err}") else: try: exec_result = None with time_limit(timeout): exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") if result[-1] != "passed": env = {"PATH": f"{node_exec}:" + subprocess.os.environ["PATH"]} cmd = f"{tsc_exec}tsc test.ts" compilation_result = subprocess.run(cmd, timeout=timeout, capture_output=True, env=env, shell=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result[-1] = f"failed: compilation error: {err}" else: try: exec_result = None with time_limit(timeout): exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True) if exec_result.returncode == 0: result[-1] = "passed" else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result[-1] = f"failed: {err}" except TimeoutException: result[-1] = "timed out" os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "cs" in language_type.lower(): import os import shutil origin_path = os.getcwd() if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) os.chdir(tmp_dir) open(f"Program.cs", 'w').write(sample["test_code"]) cmd = f"{cs_exec}mcs -d:DEBUG Program.cs" compilation_result = subprocess.run(cmd, shell=True, capture_output=True) if compilation_result.returncode != 0: if compilation_result.stderr: err = compilation_result.stderr.decode() else: err = compilation_result.stdout.decode() result.append(f"failed: compilation error: {err}") else: try: exec_result = None cmd = f"{cs_exec}mono Program.exe" env = dict(MONO_TRACE_LISTENER="Console.Error") with time_limit(timeout): exec_result = subprocess.run(cmd, timeout=timeout, shell=True, capture_output=True, env=env) if "Fail" not in exec_result.stderr.decode(): result.append("passed") else: if exec_result.stderr: try: err = exec_result.stderr.decode() except: err = exec_result.stderr else: try: err = exec_result.stdout.decode() except: err = exec_result.stdout result.append(f"failed: {err}") except TimeoutException: result.append("timed out") except Exception as e: result.append(f"failed: {e}") os.chdir(origin_path) shutil.rmtree(tmp_dir) elif "rust" in language_type.lower(): import os WD: str = os.path.dirname(os.path.abspath(__file__)) RUST_DIR: str = os.path.join(WD, "rust") RUST_SRC: str = os.path.join(RUST_DIR, "src") RUST_BIN: str = os.path.join(RUST_SRC, "bin") RUST_TMP_DIR: str = os.path.join(RUST_DIR, "tmp") RUST_LOGS: str = os.path.join(RUST_TMP_DIR, "logs") RUST_EXT: str = ".rs" # Create mandatory tmp directories os.makedirs(RUST_TMP_DIR, exist_ok=True) os.makedirs(RUST_LOGS, exist_ok=True) os.makedirs(RUST_SRC, exist_ok=True) os.makedirs(RUST_BIN, exist_ok=True) with tempfile.NamedTemporaryFile(dir = RUST_BIN, delete=False) as f: #temporal file name file_prefix = sample["task_id"].lower().replace("/", "_") file_name:str = file_prefix +RUST_EXT os.rename(f.name, os.path.join(RUST_BIN, file_name)) # Sample to pure Rust function rust_code: str = sample["test_code"] # dump the rust source code in the target temporal file f.write(rust_code.encode('utf-8')) # Proceed towards Rust binaries compilation. Therefore move to Rust module root dir. os.chdir(RUST_DIR) # Two possible outcomes # Pass OR Fail compilation log_filename: str = file_prefix + ".jsonl" log_path: str = os.path.join(RUST_LOGS, log_filename) cargo_check: str = "cargo check --bin " + file_prefix + " --message-format json >> " + log_path # Compilation build status returned_val_compilation: int # Overwrite file content if os.path.exists(log_path): if(file_size := os.path.getsize(log_path)) >= 0: os.remove(log_path) returned_val_compilation = os.system(cargo_check) else: returned_val_compilation = os.system(cargo_check) # 0 means success if returned_val_compilation == 0: #Execution pipeline cargo_test: str = "cargo test --bin " +file_prefix+ " --message-format json >> " + log_path returned_val_execution = os.system(cargo_test) if returned_val_execution == 0: result.append("passed") else: result.append(f"failed: execution error") else: result.append(f"failed: compilation error") elif "java" in language_type.lower(): assert tmp_dir is not None, "Java should be evaluated in a temporary dir." import os import shutil if "tmp" not in tmp_dir: tmp_dir = os.path.join(tmp_dir, "tmp") tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) open(os.path.join(tmp_dir, "Problem.java"), 'w').write(sample["test_code"]) origin_path = os.getcwd() os.system(f"cp ./javatuples-1.2.jar {tmp_dir}/") os.chdir(tmp_dir) res = "failed: unknown error" compile_returncode = -1 for _ in range(5): try: cmd = f"{java_exec}javac -cp javatuples-1.2.jar Problem.java" compilation_result = subprocess.run(cmd, timeout=60, capture_output=True, shell=True) compile_returncode = compilation_result.returncode break except subprocess.TimeoutExpired as e: continue if compile_returncode != 0: res = "failed: compilation error" else: exec_result = None try: # WARNING # This program exists to execute untrusted model-generated code. Although # it is highly unlikely that model-generated code will do something overtly # malicious in response to this test suite, model-generated code may act # destructively due to a lack of model capability or alignment. # Users are strongly encouraged to sandbox this evaluation suite so that it # does not perform destructive actions on their host or network. # Once you have read this disclaimer and taken appropriate precautions, # uncomment the following line and proceed at your own risk: cmd = f"{java_exec}java -ea -cp .:javatuples-1.2.jar Problem" exec_result = subprocess.run(cmd, timeout=timeout, capture_output=True, shell=True) if exec_result.returncode == 0: res = "passed" elif exec_result.returncode == 1: if "AssertionError" in exec_result.stderr.decode('unicode-escape'): res = "failed: wrong answer" else: res = f"failed: {exec_result.stderr.decode()}" except subprocess.TimeoutExpired as e: res = "time out" except BaseException as e: res = f"failed: {e}" result.append(res) os.chdir(origin_path) shutil.rmtree(tmp_dir) manager = multiprocessing.Manager() result = manager.list() p = multiprocessing.Process(target=unsafe_execute, args=(tmp_dir,)) p.start() p.join(timeout=timeout + 1) if p.is_alive(): p.kill() if not result: result.append("timed out") return { "task_id" : task_id, "completion_id": completion_id, "result" : result[0], "passed" : result[0] == "passed", "finish" : -1 if "finish" not in sample else sample["finish"], "code" : sample["test_code"] } def evaluate_functional_correctness( input_file: str = None, tmp_dir: str = "./", n_workers: int = 32, timeout: float = 10.0, problem_file: str = "../data/humaneval_python.jsonl.gz", result_path: str = None, k: List[int] = [1, 10, 100], test_groundtruth: bool = False, example_test: bool = False, is_mbpp: bool = False, language: str = "python", ): if example_test: print("Example test...") problems = read_dataset(problem_file, dataset_type="humaneval") sample_jsonl = stream_jsonl_all(input_file) with ThreadPoolExecutor(max_workers=n_workers) as executor: futures = [] completion_id = Counter() n_samples = 0 results = defaultdict(list) if test_groundtruth: print("Testing ground truth...") for sample in tqdm(problems.values()): task_id = sample["task_id"] lang = task_id.split("/")[0].lower() if lang == "javascript": lang = "js" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["generation"] = sample["canonical_solution"] sample["test_code"] = process_humaneval_test(sample, problems, example_test, language) if sample["test_code"] is None: continue args = (task_id, sample, lang, timeout, tmp_dir_, completion_id[task_id]) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 else: print("Reading Samples...") id2samples = {} for sample in tqdm(sample_jsonl): task_id = sample["task_id"] if not is_mbpp: lang = language if not is_mbpp and lang == "javascript": lang = "js" if is_mbpp: lang = "python" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["task_id"] = task_id sample["test_code"] = process_humaneval_test(sample, problems, example_test, is_mbpp, language) if sample["test_code"] is None: continue if "completion_id" in sample: completion_id_ = sample["completion_id"] else: completion_id_ = completion_id[task_id] args = (task_id, sample, lang, timeout, tmp_dir_, completion_id_) id2samples[(task_id, completion_id_)] = sample future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 if len(completion_id) == len(problems): evaluate_pass_at_k = True else: evaluate_pass_at_k = False print("Running test suites...") sample_with_results = [] for future in tqdm(as_completed(futures), total=len(futures)): result = future.result() results[result["task_id"]].append((result["completion_id"], result)) sample = id2samples[(result["task_id"], result["completion_id"])] sample_with_results.append({ 'task_id': result['task_id'], 'completion_id': result["completion_id"], 'passed': result['passed'], 'generation': sample['generation'] }) for key in sample: if key not in sample_with_results[-1]: sample_with_results[-1][key] = sample[key] # Calculate pass@k. total, correct = [], [] for result in results.values(): passed = [r[1]["passed"] for r in result] total.append(len(passed)) correct.append(sum(passed)) total = np.array(total) correct = np.array(correct) if evaluate_pass_at_k: ks = k pass_at_k = { f"pass@{k}": estimate_pass_at_k(total, correct, k).mean() for k in ks if (total >= k).all() } print(pass_at_k) else: print("Total:", np.sum(total)) print("Correct:", np.sum(correct)) if result_path is not None: with open(result_path, 'w', encoding='utf-8') as fw: for sample_with_result in sample_with_results: fw.write(json.dumps(sample_with_result) + '\n') print("Save evaluation results to\n{}".format(result_path)) return pass_at_k
null
179,545
import argparse import json import os import torch from pathlib import Path from tqdm import tqdm data_abs_dir = Path(__file__).parent / "data" from utils.utils import extract_generation_code, languge_settings from transformers import AutoTokenizer, AutoModelForCausalLM from human_eval.evaluation import evaluate_functional_correctness def generate_one(example, lang, tokenizer, model): def evaluate_functional_correctness( input_file: str = None, tmp_dir: str = "./", n_workers: int = 32, timeout: float = 10.0, problem_file: str = "../data/humaneval_python.jsonl.gz", out_dir: str = None, k: List[int] = [1, 10, 100], test_groundtruth: bool = False, example_test: bool = False, is_mbpp: bool = False, language: str = "python", ): def generate_main(args): model_name_or_path = args.model lang = args.language saved_path = args.output_path temp_dir = args.temp_dir os.makedirs(temp_dir, exist_ok=True) problem_file = os.path.join(data_abs_dir, f"humaneval-{lang}.jsonl") print("model", model_name_or_path) tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) print("load tokenizer {} from {} over.".format(tokenizer.__class__, model_name_or_path)) model = AutoModelForCausalLM.from_pretrained( model_name_or_path, torch_dtype=torch.bfloat16, device_map="auto", #use_flash_attention_2=True ) model.eval() examples = [json.loads(x) for x in open(problem_file) if x.strip()] print("Read {} examples for evaluation over.".format(len(examples))) generated_examples = [] for ex in tqdm(examples, desc='Generating'): gen_example = generate_one(ex, args.language, tokenizer, model) generated_examples.append(gen_example) print("Generate all over!!!") with open(saved_path, 'w', encoding='utf-8') as fw: for ex in generated_examples: fw.write(json.dumps(ex) + '\n') print("Save {} processed examples into {} over!".format(len(generated_examples), saved_path)) result = evaluate_functional_correctness( input_file=saved_path, tmp_dir=temp_dir, n_workers=8, timeout=3.0, problem_file=problem_file, language=lang ) print(lang, result, model_name_or_path) pass
null
179,546
import argparse import json import os import torch from pathlib import Path from tqdm import tqdm data_abs_dir = Path(__file__).parent / "data" from utils.utils import extract_generation_code, languge_settings from transformers import AutoTokenizer, AutoModelForCausalLM from human_eval.evaluation import evaluate_functional_correctness def extract_generation_code(example: str, lang_code: str, verbose: bool=False): task_id = example['task_id'] output = example.get('output', example.get("gpt_completion")) question = example["prompt"].strip() setting = languge_settings[lang_code] lang = setting['full_name'] indent = setting['indent'] try: code_block: str = re.findall(f'```{lang.lower()}\n(.*?)```', output, re.DOTALL | re.IGNORECASE)[0] if verbose: print(">>> Task: {}\n{}".format(task_id, code_block)) # Remove main if setting.get('main', None) and setting['main'] in code_block: main_start = code_block.index(setting['main']) code_block = code_block[:main_start] func_name, func_prefix = get_function_name(question, lang) try: start = code_block.lower().index(func_name.lower()) indent = 0 while start - indent >= 0 and code_block[start - indent-1] == ' ': indent += 1 try: end = code_block.rindex('\n' + ' '*indent + '}') except: end = len(code_block) except: start = 0 try: end = code_block.rindex('\n' + ' '*indent + '}') except: end = len(code_block) body = code_block[start:end] if lang_code.lower() in ['php', 'ts', 'js']: body += '\n' + ' '*indent + '}' generation = func_prefix + '\n' + body + '\n' example['generation'] = generation except Exception as ex: print("Failed to extract code block with error `{}`:\n>>> Task: {}\n>>> Output:\n{}".format( ex, task_id, output )) example['generation'] = example['prompt'] + '\n' + output return example def evaluate_functional_correctness( input_file: str = None, tmp_dir: str = "./", n_workers: int = 32, timeout: float = 10.0, problem_file: str = "../data/humaneval_python.jsonl.gz", out_dir: str = None, k: List[int] = [1, 10, 100], test_groundtruth: bool = False, example_test: bool = False, is_mbpp: bool = False, language: str = "python", ): """ Evaluates the functional correctness of a model. """ if example_test: print("Example test...") problems = read_dataset(problem_file, dataset_type="humaneval") sample_jsonl = stream_jsonl_all(input_file) with ThreadPoolExecutor(max_workers=n_workers) as executor: futures = [] completion_id = Counter() n_samples = 0 results = defaultdict(list) if test_groundtruth: print("Testing ground truth...") for sample in tqdm(problems.values()): task_id = sample["task_id"] lang = task_id.split("/")[0].lower() if lang == "javascript": lang = "js" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["generation"] = sample["canonical_solution"] sample["test_code"] = process_humaneval_test(sample, problems, example_test, language) if sample["test_code"] is None: continue args = (task_id, sample, lang, timeout, tmp_dir_, completion_id[task_id]) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 else: print("Reading samples...") for sample in tqdm(sample_jsonl): task_id = sample["task_id"] if not is_mbpp: lang = language if not is_mbpp and lang == "javascript": lang = "js" if is_mbpp: lang = "python" tmp_dir_ = os.path.join(tmp_dir, lang, "evaluation") sample["task_id"] = task_id sample["test_code"] = process_humaneval_test(sample, problems, example_test, is_mbpp, language) if sample["test_code"] is None: continue if "completion_id" in sample: completion_id_ = sample["completion_id"] else: completion_id_ = completion_id[task_id] args = (task_id, sample, lang, timeout, tmp_dir_, completion_id_) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 if len(completion_id) == len(problems): evaluate_pass_at_k = True else: evaluate_pass_at_k = False print("Running test suites...") for future in tqdm(as_completed(futures), total=len(futures)): result = future.result() results[result["task_id"]].append((result["completion_id"], result)) # Calculate pass@k. total, correct = [], [] for result in results.values(): passed = [r[1]["passed"] for r in result] total.append(len(passed)) correct.append(sum(passed)) total = np.array(total) correct = np.array(correct) if evaluate_pass_at_k: ks = k pass_at_k = {f"pass@{k}": estimate_pass_at_k(total, correct, k).mean() for k in ks if (total >= k).all()} print(pass_at_k) else: print("Total:", np.sum(total)) print("Correct:", np.sum(correct)) return pass_at_k def evaluation_only(args): lang = args.language temp_dir = args.temp_dir assert os.path.exists(args.output_path), "Not fond output file: {}".format(args.output_path) os.makedirs(temp_dir, exist_ok=True) output_name = os.path.basename(args.output_path) output_examples = [json.loads(x) for x in open(args.output_path) if x.strip()] processed_examples = [extract_generation_code(ex, lang) for ex in tqdm(output_examples, "Processing")] processed_path = os.path.join(temp_dir, output_name) with open(processed_path, 'w', encoding='utf-8') as fw: for ex in processed_examples: fw.write(json.dumps(ex) + '\n') print("Save {} processed examples into {} over!".format(len(processed_examples), processed_path)) problem_file = os.path.join(data_abs_dir, f"humaneval-{lang}.jsonl") from human_eval.evaluation import evaluate_functional_correctness result = evaluate_functional_correctness( input_file=processed_path, tmp_dir=temp_dir, n_workers=8, timeout=3.0, problem_file=problem_file, language=lang ) print(lang, result)
null
179,548
from typing import Iterable, Dict import gzip import json import os HUMAN_EVAL = os.path.join(ROOT, "..", "data", "HumanEval.jsonl.gz") def stream_jsonl(filename: str) -> Iterable[Dict]: """ Parses each jsonl line and yields it as a dictionary """ if filename.endswith(".gz"): with open(filename, "rb") as gzfp: with gzip.open(gzfp, 'rt') as fp: for line in fp: if any(not x.isspace() for x in line): yield json.loads(line) else: with open(filename, "r", encoding="utf-8") as fp: for line in fp: if any(not x.isspace() for x in line): yield json.loads(line) def read_problems(evalset_file: str = HUMAN_EVAL) -> Dict[str, Dict]: return {task["task_id"]: task for task in stream_jsonl(evalset_file)}
null
179,550
import re def _clean_python_code_for_sft(code): code = code.replace("\r", "") if "```python" in code: code_start_idx = code.index("```python") code = code[code_start_idx:].replace("```python", "").strip() end_idx = code.find("```") if "```" in code else len(code) code = code[:end_idx].strip() return code def _truncate_code_at_stopwords(code, stop_words): min_stop_idx = len(code) for stop_word in stop_words: stop_index = code.find(stop_word) if 0 <= stop_index < min_stop_idx: min_stop_idx = stop_index return code[:min_stop_idx] The provided code snippet includes necessary dependencies for implementing the `cleanup_code` function. Write a Python function `def cleanup_code( code: str, language_type: str = None, dataset: str = None, issft: bool = False, stop_words = [] )` to solve the following problem: Cleans up the generated code. Here is the function: def cleanup_code( code: str, language_type: str = None, dataset: str = None, issft: bool = False, stop_words = [] ): """ Cleans up the generated code. """ if language_type.lower() == "python": if issft: code = _clean_python_code_for_sft(code) stop_words = ["\ndef", "\nclass", "\nif", "\n#", "\nprint"] code = _truncate_code_at_stopwords(code, stop_words) elif language_type.lower() == "ts": code = _truncate_code_at_stopwords(code, stop_words + ["\nexport", "\nimport", "\nexport default", "\nimport default", "\nconsole.log"]) else: code = _truncate_code_at_stopwords(code, stop_words) return code
Cleans up the generated code.
179,551
import os import re import json import argparse import torch import numpy as np from utils.parser import * from utils.grader import * from utils.python_executor import PythonExecutor from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig def extract_python_block_with_solution(text): """ Extract the code block from the text that contains the solution function. :param text: The text to search for the code block. :return: The extracted code block. """ pattern = r'```python\n(.*?)def solution\(\):\n(.*?)```' match = re.search(pattern, text, re.DOTALL) if match: return match.group(1) + 'def solution():\n' + match.group(2) else: return "" def load_data(args): """ Load data from file. :param args: Arguments. :return: A list of examples. """ if args.data_name != "math": prompt = open("prompts/gsm8k.md").read() else: prompt = open("prompts/math.md").read() examples = [] with open(f"datasets/{args.data_name}/test.json", "r") as f: for line in f: js = json.loads(line) examples.append(js) # parse data samples = [] for example in examples: idx = example['idx'] example['question'] = parse_question(example, args.data_name) gt_cot, gt_ans = parse_ground_truth(example, args.data_name) example["input"] = f"{prompt}\n\nQuestion: {example['question']}\n" example = {'idx': idx, 'question': example['question'], 'gt_cot': gt_cot, 'gt': gt_ans, 'prompt': example["input"]} samples.append(example) return samples class PythonExecutor: def __init__( self, runtime: Optional[Any] = None, get_answer_symbol: Optional[str] = None, get_answer_expr: Optional[str] = None, get_answer_from_stdout: bool = False, timeout_length: int = 5, ) -> None: self.runtime = runtime if runtime else GenericRuntime() self.answer_symbol = get_answer_symbol self.answer_expr = get_answer_expr self.get_answer_from_stdout = get_answer_from_stdout self.timeout_length = timeout_length def process_generation_to_code(self, gens: str): return [g.split('\n') for g in gens] def execute( code, get_answer_from_stdout = None, runtime = None, answer_symbol = None, answer_expr = None, timeout_length = 10, ): try: if get_answer_from_stdout: program_io = io.StringIO() with redirect_stdout(program_io): timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) program_io.seek(0) result = program_io.readlines()[-1] elif answer_symbol: timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) result = runtime._global_vars[answer_symbol] elif answer_expr: timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) result = timeout(timeout_length)(runtime.eval_code)(answer_expr) else: timeout(timeout_length)(runtime.exec_code)('\n'.join(code[:-1])) result = timeout(timeout_length)(runtime.eval_code)(code[-1]) exec_info = "Done" str(result) pickle.dumps(result) # serialization check except: result = '' exec_info = traceback.format_exc().split('\n')[-2] return result, exec_info def apply(self, code): return self.batch_apply([code])[0] def batch_apply(self, batch_code): all_code_snippets = self.process_generation_to_code(batch_code) timeout_cnt = 0 all_exec_results = [] with ProcessPool(max_workers=min(len(all_code_snippets), multiprocessing.cpu_count())) as pool: executor = partial( self.execute, get_answer_from_stdout=self.get_answer_from_stdout, runtime=self.runtime, answer_symbol=self.answer_symbol, answer_expr=self.answer_expr, timeout_length=self.timeout_length, # this timeout not work ) future = pool.map(executor, all_code_snippets, timeout=self.timeout_length) iterator = future.result() if len(all_code_snippets) > 100: progress_bar = tqdm(total=len(all_code_snippets), desc="Execute") else: progress_bar = None while True: try: result = next(iterator) all_exec_results.append(result) except StopIteration: break except TimeoutError as error: print(error) all_exec_results.append(("", "Timeout Error")) timeout_cnt += 1 except Exception as error: print(error) exit() if progress_bar is not None: progress_bar.update(1) if progress_bar is not None: progress_bar.close() batch_results = [] for code, (result, exec_info) in zip(all_code_snippets, all_exec_results): batch_results.append((result, exec_info)) return batch_results The provided code snippet includes necessary dependencies for implementing the `inference` function. Write a Python function `def inference(args)` to solve the following problem: Inference on the dataset. :param args: Arguments. :return: None Here is the function: def inference(args): """ Inference on the dataset. :param args: Arguments. :return: None """ # load data samples = load_data(args) samples = [sample for i,sample in enumerate(samples) if i%args.world_size==args.rank] # create directory for saving results os.makedirs(f'outputs/{args.model_name}/{args.data_name}', exist_ok=True) # init python executor executor = PythonExecutor(get_answer_expr='solution()') # load model torch.set_default_tensor_type(torch.cuda.HalfTensor) tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, trust_remote_code=True,padding_side="left") try: tokenizer.pad_token_id = 0 except: # Deal with CodeGeex-2 pass llm = AutoModelForCausalLM.from_pretrained(args.model_name_or_path, torch_dtype=torch.float16, device_map="auto",trust_remote_code=True) #samples = samples[:32] print("dataset:", args.data_name, "samples:", len(samples)) if len(samples) > 0: print("=" * 50) print("sample:", samples[0]['prompt']) print("=" * 50) stop_ids = [] stop_words = ["Question","----------------"] for x in stop_words: ids = tokenizer.encode(x) if tokenizer.decode(ids[-1:]) == x: stop_ids.append(ids[-1]) print("stop ids:", stop_ids) outputs = [] generation_config = GenerationConfig(num_beams=1,) for i in range(0, len(samples), args.batch_size): chunk = [x["prompt"] for x in samples[i:i+args.batch_size]] if "llama" in args.model_name_or_path.lower() and args.rank==3 and (i==164 or i==328): for x in chunk: outputs.append(x) continue inputs = tokenizer(chunk, return_tensors="pt",padding=True) input_ids = inputs["input_ids"].cuda()[:,-args.max_context_length:] attention_mask = inputs["attention_mask"].cuda()[:,-args.max_context_length:] with torch.no_grad(): generation_output = llm.generate( input_ids=input_ids, attention_mask=attention_mask, generation_config=generation_config, return_dict_in_generate=True, output_scores=True, do_sample=False, max_new_tokens=args.max_output_length, eos_token_id=stop_ids, pad_token_id=0 ) answers = [] for i, a in enumerate(generation_output.sequences): a = a.tolist() a = a[input_ids.shape[-1]:] a = tokenizer.decode(a) for x in stop_words: if x in a: a = a[:a.index(x)] ans = extract_python_block_with_solution(a) answers.append(ans) if i == 0: print("="*80) print("Response:\n") print(a) print("Program:\n") print(ans) print("="*80) outputs.extend(answers) print("Rank",args.rank,"Processed Number:",len(outputs),flush=True) assert len(outputs) == len(samples) results = [x[0] for x in executor.batch_apply(outputs)] for result,code,sample in zip(results, outputs, samples): sample["code"] = code sample["pred"] = strip_string(result) # save results out_file = f"world_size_{args.world_size}_rank_{args.rank}.json" with open(f"outputs/{args.model_name}/{args.data_name}/{out_file}", "w") as f: json.dump(samples,f,indent=4)
Inference on the dataset. :param args: Arguments. :return: None
179,552
import os import re import json import argparse import torch import numpy as np from utils.parser import * from utils.grader import * from utils.python_executor import PythonExecutor from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig The provided code snippet includes necessary dependencies for implementing the `eval` function. Write a Python function `def eval(args)` to solve the following problem: Evaluate the results. :param args: Arguments. :return: None Here is the function: def eval(args): """ Evaluate the results. :param args: Arguments. :return: None """ # load data samples = [] for rank in range(args.world_size): out_file = f"outputs/{args.model_name}/{args.data_name}/world_size_{args.world_size}_rank_{rank}.json" if not os.path.exists(out_file): raise FileNotFoundError(f"File {out_file} does not exist.") samples.extend(json.load(open(out_file,"r"))) print("Dataset:",args.data_name) print("Model:",args.model_name) print("Loaded Examples:",len(samples)) scores = [] for x in samples: scores.append(math_equal(x["gt"],x["pred"])) print("Mean Score",np.mean(scores))
Evaluate the results. :param args: Arguments. :return: None
179,553
import multiprocessing from math import isclose from typing import Union from sympy import simplify, N from sympy.parsing.sympy_parser import parse_expr from sympy.parsing.latex import parse_latex def math_equal(prediction: Union[bool, float, str], reference: Union[float, str], include_percentage: bool = True, is_close: bool = True, timeout: bool = False, ) -> bool: """ Exact match of math if and only if: 1. numerical equal: both can convert to float and are equal 2. symbolic equal: both can convert to sympy expression and are equal """ try: # 1. numerical equal if is_digit(prediction) and is_digit(reference): prediction = float(str(prediction).replace(",", "")) reference = float(str(reference).replace(",", "")) # number questions if include_percentage: gt_result = [reference / 100, reference, reference * 100] else: gt_result = [reference] for item in gt_result: try: if is_close: if isclose(item, prediction, rel_tol=1e-4): return True else: if item == prediction: return True except Exception: continue return False except: pass if not prediction and prediction not in [0, False]: return False # 2. symbolic equal reference = str(reference).strip() prediction = str(prediction).strip() ## deal with [], (), {} pred_str, ref_str = prediction, reference if (prediction.startswith("[") and prediction.endswith("]") and not reference.startswith("(")) or \ (prediction.startswith("(") and prediction.endswith(")") and not reference.startswith("[")): pred_str = pred_str.strip("[]()") ref_str = ref_str.strip("[]()") for s in ['{', "}", "(", ")"]: ref_str = ref_str.replace(s, "") pred_str = pred_str.replace(s, "") if pred_str == ref_str: return True ## [a, b] vs. [c, d], return a==c and b==d if (prediction.startswith("[") and prediction.endswith("]")) and (reference.startswith("[") and reference.endswith("]")) or \ (prediction.startswith("(") and prediction.endswith(")")) and (reference.startswith("(") and reference.endswith(")")): pred_parts = prediction[1:-1].split(",") ref_parts = reference[1:-1].split(",") if len(pred_parts) == len(ref_parts): if all([math_equal(pred_parts[i], ref_parts[i], include_percentage, is_close) for i in range(len(pred_parts))]): return True # symbolic equal with sympy if timeout: if call_with_timeout(symbolic_equal_process, prediction, reference): return True else: if symbolic_equal(prediction, reference): return True return False def math_equal_process(param): print(param[-2], param[-1],math_equal(param[-2], param[-1])) return math_equal(param[-2], param[-1])
null
179,554
import multiprocessing from math import isclose from typing import Union from sympy import simplify, N from sympy.parsing.sympy_parser import parse_expr from sympy.parsing.latex import parse_latex def math_equal(prediction: Union[bool, float, str], reference: Union[float, str], include_percentage: bool = True, is_close: bool = True, timeout: bool = False, ) -> bool: """ Exact match of math if and only if: 1. numerical equal: both can convert to float and are equal 2. symbolic equal: both can convert to sympy expression and are equal """ try: # 1. numerical equal if is_digit(prediction) and is_digit(reference): prediction = float(str(prediction).replace(",", "")) reference = float(str(reference).replace(",", "")) # number questions if include_percentage: gt_result = [reference / 100, reference, reference * 100] else: gt_result = [reference] for item in gt_result: try: if is_close: if isclose(item, prediction, rel_tol=1e-4): return True else: if item == prediction: return True except Exception: continue return False except: pass if not prediction and prediction not in [0, False]: return False # 2. symbolic equal reference = str(reference).strip() prediction = str(prediction).strip() ## deal with [], (), {} pred_str, ref_str = prediction, reference if (prediction.startswith("[") and prediction.endswith("]") and not reference.startswith("(")) or \ (prediction.startswith("(") and prediction.endswith(")") and not reference.startswith("[")): pred_str = pred_str.strip("[]()") ref_str = ref_str.strip("[]()") for s in ['{', "}", "(", ")"]: ref_str = ref_str.replace(s, "") pred_str = pred_str.replace(s, "") if pred_str == ref_str: return True ## [a, b] vs. [c, d], return a==c and b==d if (prediction.startswith("[") and prediction.endswith("]")) and (reference.startswith("[") and reference.endswith("]")) or \ (prediction.startswith("(") and prediction.endswith(")")) and (reference.startswith("(") and reference.endswith(")")): pred_parts = prediction[1:-1].split(",") ref_parts = reference[1:-1].split(",") if len(pred_parts) == len(ref_parts): if all([math_equal(pred_parts[i], ref_parts[i], include_percentage, is_close) for i in range(len(pred_parts))]): return True # symbolic equal with sympy if timeout: if call_with_timeout(symbolic_equal_process, prediction, reference): return True else: if symbolic_equal(prediction, reference): return True return False def _test_math_equal(): # print(math_equal("0.0833333333333333", "\\frac{1}{12}")) # print(math_equal("(1,4.5)", "(1,\\frac{9}{2})")) print(math_equal("\\frac{x}{7}+\\frac{2}{7}", "\\frac{x+2}{7}", timeout=True))
null
179,555
import re from typing import Any, Dict def strip_string(string): string = str(string).strip() # linebreaks string = string.replace("\n", "") # right "." string = string.rstrip(".") # remove inverse spaces string = string.replace("\\!", "") string = string.replace("\\ ", "") # replace \\ with \ string = string.replace("\\\\", "\\") string = string.replace("\\\\", "\\") # replace tfrac and dfrac with frac string = string.replace("tfrac", "frac") string = string.replace("dfrac", "frac") # remove \left and \right string = string.replace("\\left", "") string = string.replace("\\right", "") # Remove unit: miles, dollars if after is not none _string = re.sub(r"\\text{.*?}$", "", string).strip() if _string != "" and _string != string: # print("Warning: unit not removed: '{}' -> '{}'".format(string, _string)) string = _string # Remove circ (degrees) string = string.replace("^{\\circ}", "") string = string.replace("^\\circ", "") # remove dollar signs string = string.replace("\\$", "") string = string.replace("$", "") string = string.replace("\\text", "") string = string.replace("x\\in", "") # remove percentage string = string.replace("\\%", "") string = string.replace("\%", "") string = string.replace("%", "") # " 0." equivalent to " ." and "{0." equivalent to "{." Alternatively, add "0" if "." is the start of the string string = string.replace(" .", " 0.") string = string.replace("{.", "{0.") # cdot string = string.replace("\\cdot", "") # inf string = string.replace("infinity", "\\infty") if "\\infty" not in string: string = string.replace("inf", "\\infty") string = string.replace("+\\inity", "\\infty") # and string = string.replace("and", "") string = string.replace("\\mathbf", "") # use regex to remove \mbox{...} string = re.sub(r"\\mbox{.*?}", "", string) # quote string.replace("'", "") string.replace("\"", "") # i, j if "j" in string and "i" not in string: string = string.replace("j", "i") # replace a.000b where b is not number or b is end, with ab, use regex string = re.sub(r"(\d+)\.0+([^\d])", r"\1\2", string) string = re.sub(r"(\d+)\.0+$", r"\1", string) # if empty, return empty string if len(string) == 0: return string if string[0] == ".": string = "0" + string # to consider: get rid of e.g. "k = " or "q = " at beginning if len(string.split("=")) == 2: if len(string.split("=")[0]) <= 2: string = string.split("=")[1] string = _fix_sqrt(string) string = string.replace(" ", "") # \frac1b or \frac12 --> \frac{1}{b} and \frac{1}{2}, etc. Even works with \frac1{72} (but not \frac{72}1). Also does a/b --> \\frac{a}{b} string = _fix_fracs(string) # NOTE: X/Y changed to \frac{X}{Y} in dataset, but in simple cases fix in case the model output is X/Y string = _fix_a_slash_b(string) return string def extract_answer(pred_str): if 'boxed' in pred_str: ans = pred_str.split('boxed')[-1] if len(ans) == 0: return "" elif (ans[0] == '{'): stack = 1 a = '' for c in ans[1:]: if (c == '{'): stack += 1 a += c elif (c == '}'): stack -= 1 if (stack == 0): break a += c else: a += c else: a = ans.split('$')[0].strip() pred=a elif ('he answer is' in pred_str): pred = pred_str.split('he answer is')[-1].strip() elif extract_program_output(pred_str) != "": # fall back to program pred = extract_program_output(pred_str) else: # use the last number pattern = '-?\d*\.?\d+' pred = re.findall(pattern, pred_str.replace(",", "")) if(len(pred) >= 1): pred = pred[-1] else: pred = '' # multiple line pred = pred.split("\n")[0] if pred != "" and pred[0] == ":": pred = pred[1:] if pred != "" and pred[-1] == ".": pred = pred[:-1] if pred != "" and pred[-1] == "/": pred = pred[:-1] pred = strip_string(pred) return pred def parse_ground_truth(example: Dict[str, Any], data_name): if 'gt_cot' in example: return example['gt_cot'], strip_string(example['gt']) # parse ground truth if data_name in ["math", 'ocw']: gt_cot = example['solution'] gt_ans = extract_answer(gt_cot) elif data_name == "gsm8k": gt_cot, gt_ans = example['answer'].split("####") elif data_name == "gsm-hard": gt_cot, gt_ans = example['code'], example['target'] elif data_name == "svamp": gt_cot, gt_ans = example['Equation'], example['Answer'] elif data_name == "asdiv": gt_cot = example['formula'] gt_ans = re.sub(r"\(.*?\)", "", example['answer']) elif data_name == "mawps": gt_cot, gt_ans = None, example['target'] elif data_name == "tabmwp": gt_cot = example['solution'] gt_ans = example['answer'] if example['ans_type'] in ['integer_number', 'decimal_number']: if '/' in gt_ans: gt_ans = int(gt_ans.split('/')[0]) / int(gt_ans.split('/')[1]) elif ',' in gt_ans: gt_ans = float(gt_ans.replace(',', '')) elif '%' in gt_ans: gt_ans = float(gt_ans.split('%')[0]) / 100 else: gt_ans = float(gt_ans) elif data_name == "bbh": gt_cot, gt_ans = None, example['target'] else: raise NotImplementedError(data_name) # post process gt_cot = str(gt_cot).strip() gt_ans = strip_string(gt_ans) return gt_cot, gt_ans
null
179,556
import re from typing import Any, Dict def parse_question(example, data_name): question = "" if data_name == "asdiv": question = f"{example['body'].strip()} {example['question'].strip()}" elif data_name == "svamp": body = example["Body"].strip() if not body.endswith("."): body = body + "." question = f'{body} {example["Question"].strip()}' elif data_name == "tabmwp": title_str = f'regarding "{example["table_title"]}" ' if example['table_title'] else "" question = f'Read the following table {title_str}and answer a question:\n' question += f'{example["table"]}\n{example["question"]}' if example['choices']: question += f' Please select from the following options: {example["choices"]}' else: for key in ['question', 'problem', 'Question', 'input']: if key in example: question = example[key] break assert question != "" return question.strip()
null
179,557
import re from typing import Any, Dict def strip_string(string): string = str(string).strip() # linebreaks string = string.replace("\n", "") # right "." string = string.rstrip(".") # remove inverse spaces string = string.replace("\\!", "") string = string.replace("\\ ", "") # replace \\ with \ string = string.replace("\\\\", "\\") string = string.replace("\\\\", "\\") # replace tfrac and dfrac with frac string = string.replace("tfrac", "frac") string = string.replace("dfrac", "frac") # remove \left and \right string = string.replace("\\left", "") string = string.replace("\\right", "") # Remove unit: miles, dollars if after is not none _string = re.sub(r"\\text{.*?}$", "", string).strip() if _string != "" and _string != string: # print("Warning: unit not removed: '{}' -> '{}'".format(string, _string)) string = _string # Remove circ (degrees) string = string.replace("^{\\circ}", "") string = string.replace("^\\circ", "") # remove dollar signs string = string.replace("\\$", "") string = string.replace("$", "") string = string.replace("\\text", "") string = string.replace("x\\in", "") # remove percentage string = string.replace("\\%", "") string = string.replace("\%", "") string = string.replace("%", "") # " 0." equivalent to " ." and "{0." equivalent to "{." Alternatively, add "0" if "." is the start of the string string = string.replace(" .", " 0.") string = string.replace("{.", "{0.") # cdot string = string.replace("\\cdot", "") # inf string = string.replace("infinity", "\\infty") if "\\infty" not in string: string = string.replace("inf", "\\infty") string = string.replace("+\\inity", "\\infty") # and string = string.replace("and", "") string = string.replace("\\mathbf", "") # use regex to remove \mbox{...} string = re.sub(r"\\mbox{.*?}", "", string) # quote string.replace("'", "") string.replace("\"", "") # i, j if "j" in string and "i" not in string: string = string.replace("j", "i") # replace a.000b where b is not number or b is end, with ab, use regex string = re.sub(r"(\d+)\.0+([^\d])", r"\1\2", string) string = re.sub(r"(\d+)\.0+$", r"\1", string) # if empty, return empty string if len(string) == 0: return string if string[0] == ".": string = "0" + string # to consider: get rid of e.g. "k = " or "q = " at beginning if len(string.split("=")) == 2: if len(string.split("=")[0]) <= 2: string = string.split("=")[1] string = _fix_sqrt(string) string = string.replace(" ", "") # \frac1b or \frac12 --> \frac{1}{b} and \frac{1}{2}, etc. Even works with \frac1{72} (but not \frac{72}1). Also does a/b --> \\frac{a}{b} string = _fix_fracs(string) # NOTE: X/Y changed to \frac{X}{Y} in dataset, but in simple cases fix in case the model output is X/Y string = _fix_a_slash_b(string) return string def extract_answer(pred_str): if 'boxed' in pred_str: ans = pred_str.split('boxed')[-1] if len(ans) == 0: return "" elif (ans[0] == '{'): stack = 1 a = '' for c in ans[1:]: if (c == '{'): stack += 1 a += c elif (c == '}'): stack -= 1 if (stack == 0): break a += c else: a += c else: a = ans.split('$')[0].strip() pred=a elif ('he answer is' in pred_str): pred = pred_str.split('he answer is')[-1].strip() elif extract_program_output(pred_str) != "": # fall back to program pred = extract_program_output(pred_str) else: # use the last number pattern = '-?\d*\.?\d+' pred = re.findall(pattern, pred_str.replace(",", "")) if(len(pred) >= 1): pred = pred[-1] else: pred = '' # multiple line pred = pred.split("\n")[0] if pred != "" and pred[0] == ":": pred = pred[1:] if pred != "" and pred[-1] == ".": pred = pred[:-1] if pred != "" and pred[-1] == "/": pred = pred[:-1] pred = strip_string(pred) return pred def extract_program(result: str, last_only=True): """ extract the program after "```python", and before "```" """ program = "" start = False for line in result.split("\n"): if line.startswith("```python"): if last_only: program = "" # only extract the last program else: program += "\n# ========\n" start = True elif line.startswith("```"): start = False elif start: program += line + "\n" return program def extract_program_output(pred_str): """ extract output between the last ```output\n...\n``` """ if "```output" not in pred_str: return "" if '```output' in pred_str: pred_str = pred_str.split('```output')[-1] if '```' in pred_str: pred_str = pred_str.split('```')[0] output = pred_str.strip() return output def run_execute(executor, result, prompt_type, execute=False): if not result or result == 'error': return None, None report = None if "program_only" in prompt_type: prediction = extract_program_output(result) elif prompt_type in ["pot", "pal"] and execute: code = extract_program(result) prediction, report = executor.apply(code) else: prediction = extract_answer(result) prediction = strip_string(prediction) return prediction, report
null
179,558
import io import regex import pickle import traceback import copy import datetime import multiprocessing import dateutil.relativedelta import multiprocess from multiprocess import Pool from typing import Any, Dict, Optional from pebble import ProcessPool from tqdm import tqdm from concurrent.futures import TimeoutError from functools import partial from timeout_decorator import timeout from contextlib import redirect_stdout class PythonExecutor: def __init__( self, runtime: Optional[Any] = None, get_answer_symbol: Optional[str] = None, get_answer_expr: Optional[str] = None, get_answer_from_stdout: bool = False, timeout_length: int = 5, ) -> None: self.runtime = runtime if runtime else GenericRuntime() self.answer_symbol = get_answer_symbol self.answer_expr = get_answer_expr self.get_answer_from_stdout = get_answer_from_stdout self.timeout_length = timeout_length def process_generation_to_code(self, gens: str): return [g.split('\n') for g in gens] def execute( code, get_answer_from_stdout = None, runtime = None, answer_symbol = None, answer_expr = None, timeout_length = 10, ): try: if get_answer_from_stdout: program_io = io.StringIO() with redirect_stdout(program_io): timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) program_io.seek(0) result = program_io.readlines()[-1] elif answer_symbol: timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) result = runtime._global_vars[answer_symbol] elif answer_expr: timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) result = timeout(timeout_length)(runtime.eval_code)(answer_expr) else: timeout(timeout_length)(runtime.exec_code)('\n'.join(code[:-1])) result = timeout(timeout_length)(runtime.eval_code)(code[-1]) exec_info = "Done" str(result) pickle.dumps(result) # serialization check except: result = '' exec_info = traceback.format_exc().split('\n')[-2] return result, exec_info def apply(self, code): return self.batch_apply([code])[0] def batch_apply(self, batch_code): all_code_snippets = self.process_generation_to_code(batch_code) timeout_cnt = 0 all_exec_results = [] with ProcessPool(max_workers=min(len(all_code_snippets), multiprocessing.cpu_count())) as pool: executor = partial( self.execute, get_answer_from_stdout=self.get_answer_from_stdout, runtime=self.runtime, answer_symbol=self.answer_symbol, answer_expr=self.answer_expr, timeout_length=self.timeout_length, # this timeout not work ) future = pool.map(executor, all_code_snippets, timeout=self.timeout_length) iterator = future.result() if len(all_code_snippets) > 100: progress_bar = tqdm(total=len(all_code_snippets), desc="Execute") else: progress_bar = None while True: try: result = next(iterator) all_exec_results.append(result) except StopIteration: break except TimeoutError as error: print(error) all_exec_results.append(("", "Timeout Error")) timeout_cnt += 1 except Exception as error: print(error) exit() if progress_bar is not None: progress_bar.update(1) if progress_bar is not None: progress_bar.close() batch_results = [] for code, (result, exec_info) in zip(all_code_snippets, all_exec_results): batch_results.append((result, exec_info)) return batch_results def _test(): batch_code = [ """ print("Hello world!") """ ] executor = PythonExecutor(get_answer_from_stdout=True) predictions = executor.apply(batch_code[0]) print(predictions)
null
179,559
import argparse import os import sys from datetime import datetime import cv2 as cv import numpy as np from stitching import AffineStitcher, Stitcher, __version__ from stitching.blender import Blender from stitching.camera_adjuster import CameraAdjuster from stitching.camera_estimator import CameraEstimator from stitching.camera_wave_corrector import WaveCorrector from stitching.cropper import Cropper from stitching.exposure_error_compensator import ExposureErrorCompensator from stitching.feature_detector import FeatureDetector from stitching.feature_matcher import FeatureMatcher from stitching.images import Images from stitching.seam_finder import SeamFinder from stitching.subsetter import Subsetter from stitching.timelapser import Timelapser from stitching.warper import Warper __version__ = "0.5.3" class Blender: """https://docs.opencv.org/4.x/d6/d4a/classcv_1_1detail_1_1Blender.html""" BLENDER_CHOICES = ( "multiband", "feather", "no", ) DEFAULT_BLENDER = "multiband" DEFAULT_BLEND_STRENGTH = 5 def __init__( self, blender_type=DEFAULT_BLENDER, blend_strength=DEFAULT_BLEND_STRENGTH ): self.blender_type = blender_type self.blend_strength = blend_strength self.blender = None def prepare(self, corners, sizes): dst_sz = cv.detail.resultRoi(corners=corners, sizes=sizes) blend_width = np.sqrt(dst_sz[2] * dst_sz[3]) * self.blend_strength / 100 if self.blender_type == "no" or blend_width < 1: self.blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO) elif self.blender_type == "multiband": self.blender = cv.detail_MultiBandBlender() self.blender.setNumBands(int((np.log(blend_width) / np.log(2.0) - 1.0))) elif self.blender_type == "feather": self.blender = cv.detail_FeatherBlender() self.blender.setSharpness(1.0 / blend_width) self.blender.prepare(dst_sz) def feed(self, img, mask, corner): self.blender.feed(cv.UMat(img.astype(np.int16)), mask, corner) def blend(self): result = None result_mask = None result, result_mask = self.blender.blend(result, result_mask) result = cv.convertScaleAbs(result) return result, result_mask def create_panorama(cls, imgs, masks, corners, sizes): blender = cls("no") blender.prepare(corners, sizes) for img, mask, corner in zip(imgs, masks, corners): blender.feed(img, mask, corner) return blender.blend() class CameraAdjuster: """https://docs.opencv.org/4.x/d5/d56/classcv_1_1detail_1_1BundleAdjusterBase.html""" # noqa: E501 CAMERA_ADJUSTER_CHOICES = OrderedDict() CAMERA_ADJUSTER_CHOICES["ray"] = cv.detail_BundleAdjusterRay CAMERA_ADJUSTER_CHOICES["reproj"] = cv.detail_BundleAdjusterReproj CAMERA_ADJUSTER_CHOICES["affine"] = cv.detail_BundleAdjusterAffinePartial CAMERA_ADJUSTER_CHOICES["no"] = cv.detail_NoBundleAdjuster DEFAULT_CAMERA_ADJUSTER = list(CAMERA_ADJUSTER_CHOICES.keys())[0] DEFAULT_REFINEMENT_MASK = "xxxxx" def __init__( self, adjuster=DEFAULT_CAMERA_ADJUSTER, refinement_mask=DEFAULT_REFINEMENT_MASK, confidence_threshold=1.0, ): self.adjuster = CameraAdjuster.CAMERA_ADJUSTER_CHOICES[adjuster]() self.set_refinement_mask(refinement_mask) self.adjuster.setConfThresh(confidence_threshold) def set_refinement_mask(self, refinement_mask): mask_matrix = np.zeros((3, 3), np.uint8) if refinement_mask[0] == "x": mask_matrix[0, 0] = 1 if refinement_mask[1] == "x": mask_matrix[0, 1] = 1 if refinement_mask[2] == "x": mask_matrix[0, 2] = 1 if refinement_mask[3] == "x": mask_matrix[1, 1] = 1 if refinement_mask[4] == "x": mask_matrix[1, 2] = 1 self.adjuster.setRefinementMask(mask_matrix) def adjust(self, features, pairwise_matches, estimated_cameras): b, cameras = self.adjuster.apply(features, pairwise_matches, estimated_cameras) if not b: raise StitchingError("Camera parameters adjusting failed.") return cameras class CameraEstimator: """https://docs.opencv.org/4.x/df/d15/classcv_1_1detail_1_1Estimator.html""" CAMERA_ESTIMATOR_CHOICES = OrderedDict() CAMERA_ESTIMATOR_CHOICES["homography"] = cv.detail_HomographyBasedEstimator CAMERA_ESTIMATOR_CHOICES["affine"] = cv.detail_AffineBasedEstimator DEFAULT_CAMERA_ESTIMATOR = list(CAMERA_ESTIMATOR_CHOICES.keys())[0] def __init__(self, estimator=DEFAULT_CAMERA_ESTIMATOR, **kwargs): self.estimator = CameraEstimator.CAMERA_ESTIMATOR_CHOICES[estimator](**kwargs) def estimate(self, features, pairwise_matches): b, cameras = self.estimator.apply(features, pairwise_matches, None) if not b: raise StitchingError("Homography estimation failed.") for cam in cameras: cam.R = cam.R.astype(np.float32) return cameras class WaveCorrector: """https://docs.opencv.org/4.x/d7/d74/group__stitching__rotation.html#ga8faf9588aebd5aeb6f8c649c82beb1fb""" # noqa: E501 WAVE_CORRECT_CHOICES = OrderedDict() WAVE_CORRECT_CHOICES["horiz"] = cv.detail.WAVE_CORRECT_HORIZ WAVE_CORRECT_CHOICES["vert"] = cv.detail.WAVE_CORRECT_VERT WAVE_CORRECT_CHOICES["auto"] = cv.detail.WAVE_CORRECT_AUTO WAVE_CORRECT_CHOICES["no"] = None DEFAULT_WAVE_CORRECTION = list(WAVE_CORRECT_CHOICES.keys())[0] def __init__(self, wave_correct_kind=DEFAULT_WAVE_CORRECTION): self.wave_correct_kind = WaveCorrector.WAVE_CORRECT_CHOICES[wave_correct_kind] def correct(self, cameras): if self.wave_correct_kind is not None: rmats = [np.copy(cam.R) for cam in cameras] rmats = cv.detail.waveCorrect(rmats, self.wave_correct_kind) for idx, cam in enumerate(cameras): cam.R = rmats[idx] return cameras return cameras class Cropper: DEFAULT_CROP = True def __init__(self, crop=DEFAULT_CROP): self.do_crop = crop self.overlapping_rectangles = [] self.cropping_rectangles = [] def prepare(self, imgs, masks, corners, sizes): if self.do_crop: mask = self.estimate_panorama_mask(imgs, masks, corners, sizes) lir = self.estimate_largest_interior_rectangle(mask) corners = self.get_zero_center_corners(corners) rectangles = self.get_rectangles(corners, sizes) self.overlapping_rectangles = self.get_overlaps(rectangles, lir) self.intersection_rectangles = self.get_intersections( rectangles, self.overlapping_rectangles ) def crop_images(self, imgs, aspect=1): for idx, img in enumerate(imgs): yield self.crop_img(img, idx, aspect) def crop_img(self, img, idx, aspect=1): if self.do_crop: intersection_rect = self.intersection_rectangles[idx] scaled_intersection_rect = intersection_rect.times(aspect) cropped_img = self.crop_rectangle(img, scaled_intersection_rect) return cropped_img return img def crop_rois(self, corners, sizes, aspect=1): if self.do_crop: scaled_overlaps = [r.times(aspect) for r in self.overlapping_rectangles] cropped_corners = [r.corner for r in scaled_overlaps] cropped_corners = self.get_zero_center_corners(cropped_corners) cropped_sizes = [r.size for r in scaled_overlaps] return cropped_corners, cropped_sizes return corners, sizes def estimate_panorama_mask(imgs, masks, corners, sizes): _, mask = Blender.create_panorama(imgs, masks, corners, sizes) return mask def estimate_largest_interior_rectangle(self, mask): # largestinteriorrectangle is only imported if cropping # is explicitly desired (needs some time to compile at the first run!) import largestinteriorrectangle contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE) if not hierarchy.shape == (1, 1, 4) or not np.all(hierarchy == -1): raise StitchingError( """Invalid Contour. Run with --no-crop (using the stitch interface), crop=false (using the stitcher class) or Cropper(False) (using the cropper class)""" ) contour = contours[0][:, 0, :] lir = largestinteriorrectangle.lir(mask > 0, contour) lir = Rectangle(*lir) return lir def get_zero_center_corners(corners): min_corner_x = min([corner[0] for corner in corners]) min_corner_y = min([corner[1] for corner in corners]) return [(x - min_corner_x, y - min_corner_y) for x, y in corners] def get_rectangles(corners, sizes): rectangles = [] for corner, size in zip(corners, sizes): rectangle = Rectangle(*corner, *size) rectangles.append(rectangle) return rectangles def get_overlaps(rectangles, lir): return [Cropper.get_overlap(r, lir) for r in rectangles] def get_overlap(rectangle1, rectangle2): x1 = max(rectangle1.x, rectangle2.x) y1 = max(rectangle1.y, rectangle2.y) x2 = min(rectangle1.x2, rectangle2.x2) y2 = min(rectangle1.y2, rectangle2.y2) if x2 < x1 or y2 < y1: raise StitchingError("Rectangles do not overlap!") return Rectangle(x1, y1, x2 - x1, y2 - y1) def get_intersections(rectangles, overlapping_rectangles): return [ Cropper.get_intersection(r, overlap_r) for r, overlap_r in zip(rectangles, overlapping_rectangles) ] def get_intersection(rectangle, overlapping_rectangle): x = abs(overlapping_rectangle.x - rectangle.x) y = abs(overlapping_rectangle.y - rectangle.y) width = overlapping_rectangle.width height = overlapping_rectangle.height return Rectangle(x, y, width, height) def crop_rectangle(img, rectangle): return img[rectangle.y : rectangle.y2, rectangle.x : rectangle.x2] class ExposureErrorCompensator: """https://docs.opencv.org/4.x/d2/d37/classcv_1_1detail_1_1ExposureCompensator.html""" # noqa: E501 COMPENSATOR_CHOICES = OrderedDict() COMPENSATOR_CHOICES["gain_blocks"] = cv.detail.ExposureCompensator_GAIN_BLOCKS COMPENSATOR_CHOICES["gain"] = cv.detail.ExposureCompensator_GAIN COMPENSATOR_CHOICES["channel"] = cv.detail.ExposureCompensator_CHANNELS COMPENSATOR_CHOICES["channel_blocks"] = ( cv.detail.ExposureCompensator_CHANNELS_BLOCKS ) COMPENSATOR_CHOICES["no"] = cv.detail.ExposureCompensator_NO DEFAULT_COMPENSATOR = list(COMPENSATOR_CHOICES.keys())[0] DEFAULT_NR_FEEDS = 1 DEFAULT_BLOCK_SIZE = 32 def __init__( self, compensator=DEFAULT_COMPENSATOR, nr_feeds=DEFAULT_NR_FEEDS, block_size=DEFAULT_BLOCK_SIZE, ): if compensator == "channel": self.compensator = cv.detail_ChannelsCompensator(nr_feeds) elif compensator == "channel_blocks": self.compensator = cv.detail_BlocksChannelsCompensator( block_size, block_size, nr_feeds ) else: self.compensator = cv.detail.ExposureCompensator_createDefault( ExposureErrorCompensator.COMPENSATOR_CHOICES[compensator] ) def feed(self, *args): """https://docs.opencv.org/4.x/d2/d37/classcv_1_1detail_1_1ExposureCompensator.html#ae6b0cc69a7bc53818ddea53eddb6bdba""" # noqa self.compensator.feed(*args) def apply(self, *args): """https://docs.opencv.org/4.x/d2/d37/classcv_1_1detail_1_1ExposureCompensator.html#a473eaf1e585804c08d77c91e004f93aa""" # noqa return self.compensator.apply(*args) class FeatureDetector: """https://docs.opencv.org/4.x/d0/d13/classcv_1_1Feature2D.html""" DETECTOR_CHOICES = OrderedDict() DETECTOR_CHOICES["orb"] = cv.ORB.create DETECTOR_CHOICES["sift"] = cv.SIFT_create DETECTOR_CHOICES["brisk"] = cv.BRISK_create DETECTOR_CHOICES["akaze"] = cv.AKAZE_create DEFAULT_DETECTOR = list(DETECTOR_CHOICES.keys())[0] def __init__(self, detector=DEFAULT_DETECTOR, **kwargs): self.detector = FeatureDetector.DETECTOR_CHOICES[detector](**kwargs) def detect_features(self, img, *args, **kwargs): return cv.detail.computeImageFeatures2(self.detector, img, *args, **kwargs) def detect(self, imgs): return [self.detect_features(img) for img in imgs] def detect_with_masks(self, imgs, masks): features = [] for idx, (img, mask) in enumerate(zip(imgs, masks)): assert len(img.shape) == 3 and len(mask.shape) == 2 if not len(imgs) == len(masks): raise StitchingError("image and mask lists must be of same length") if not np.array_equal(img.shape[:2], mask.shape): raise StitchingError( f"Resolution of mask {idx+1} {mask.shape} does not match" f" the resolution of image {idx+1} {img.shape[:2]}." ) features.append(self.detect_features(img, mask=mask)) return features def draw_keypoints(img, features, **kwargs): kwargs.setdefault("color", (0, 255, 0)) keypoints = features.getKeypoints() return cv.drawKeypoints(img, keypoints, None, **kwargs) class FeatureMatcher: """https://docs.opencv.org/4.x/da/d87/classcv_1_1detail_1_1FeaturesMatcher.html""" MATCHER_CHOICES = ("homography", "affine") DEFAULT_MATCHER = "homography" DEFAULT_RANGE_WIDTH = -1 def __init__( self, matcher_type=DEFAULT_MATCHER, range_width=DEFAULT_RANGE_WIDTH, **kwargs ): if matcher_type == "affine": self.matcher = cv.detail_AffineBestOf2NearestMatcher(**kwargs) elif range_width == -1: self.matcher = cv.detail_BestOf2NearestMatcher(**kwargs) else: self.matcher = cv.detail_BestOf2NearestRangeMatcher(range_width, **kwargs) def match_features(self, features, *args, **kwargs): pairwise_matches = self.matcher.apply2(features, *args, **kwargs) self.matcher.collectGarbage() return pairwise_matches def draw_matches_matrix( imgs, features, matches, conf_thresh=1, inliers=False, **kwargs ): matches_matrix = FeatureMatcher.get_matches_matrix(matches) for idx1, idx2 in FeatureMatcher.get_all_img_combinations(len(imgs)): match = matches_matrix[idx1, idx2] if match.confidence < conf_thresh or len(match.matches) == 0: continue if inliers: kwargs["matchesMask"] = match.getInliers() yield idx1, idx2, FeatureMatcher.draw_matches( imgs[idx1], features[idx1], imgs[idx2], features[idx2], match, **kwargs ) def draw_matches(img1, features1, img2, features2, match1to2, **kwargs): kwargs.setdefault("flags", cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) keypoints1 = features1.getKeypoints() keypoints2 = features2.getKeypoints() matches = match1to2.getMatches() return cv.drawMatches( img1, keypoints1, img2, keypoints2, matches, None, **kwargs ) def get_matches_matrix(pairwise_matches): return FeatureMatcher.array_in_square_matrix(pairwise_matches) def get_confidence_matrix(pairwise_matches): matches_matrix = FeatureMatcher.get_matches_matrix(pairwise_matches) match_confs = [[m.confidence for m in row] for row in matches_matrix] match_conf_matrix = np.array(match_confs) return match_conf_matrix def array_in_square_matrix(array): matrix_dimension = int(math.sqrt(len(array))) rows = [] for i in range(0, len(array), matrix_dimension): rows.append(array[i : i + matrix_dimension]) return np.array(rows) def get_all_img_combinations(number_imgs): ii, jj = np.triu_indices(number_imgs, k=1) for i, j in zip(ii, jj): yield i, j def get_match_conf(match_conf, feature_detector_type): if match_conf is None: match_conf = FeatureMatcher.get_default_match_conf(feature_detector_type) return match_conf def get_default_match_conf(feature_detector_type): if feature_detector_type == "orb": return 0.3 return 0.65 class Images(ABC): class Resolution(Enum): MEDIUM = 0.6 LOW = 0.1 FINAL = -1 def of( images, medium_megapix=Resolution.MEDIUM.value, low_megapix=Resolution.LOW.value, final_megapix=Resolution.FINAL.value, ): if not isinstance(images, list): raise StitchingError("images must be a list of images or filenames") if len(images) == 0: raise StitchingError("images must not be an empty list") if Images.check_list_element_types(images, np.ndarray): return _NumpyImages(images, medium_megapix, low_megapix, final_megapix) elif Images.check_list_element_types(images, str): return _FilenameImages(images, medium_megapix, low_megapix, final_megapix) else: raise StitchingError( """invalid images list: must be numpy arrays (loaded images) or filename strings""" ) def __init__(self, images, medium_megapix, low_megapix, final_megapix): if medium_megapix < low_megapix: raise StitchingError( "Medium resolution megapix need to be " "greater or equal than low resolution " "megapix" ) self._scalers = {} self._scalers["MEDIUM"] = MegapixDownscaler(medium_megapix) self._scalers["LOW"] = MegapixDownscaler(low_megapix) self._scalers["FINAL"] = MegapixDownscaler(final_megapix) self._scales_set = False self._sizes_set = False self._names_set = False def sizes(self): assert self._sizes_set return self._sizes def names(self): assert self._names_set return self._names def subset(self, indices): self._sizes = [self._sizes[i] for i in indices] self._names = [self._names[i] for i in indices] def resize(self, resolution, imgs=None): img_iterable = self.__iter__() if imgs is None else imgs for idx, img in enumerate(img_iterable): yield Images.resize_img_by_scaler( self._get_scaler(resolution), self._sizes[idx], img ) def __iter__(self): pass def _set_scales(self, size): if not self._scales_set: for scaler in self._scalers.values(): scaler.set_scale_by_img_size(size) self._scales_set = True def _get_scaler(self, resolution): Images.check_resolution(resolution) return self._scalers[resolution.name] def get_ratio(self, from_resolution, to_resolution): assert self._scales_set Images.check_resolution(from_resolution) Images.check_resolution(to_resolution) return ( self._get_scaler(to_resolution).scale / self._get_scaler(from_resolution).scale # noqa: W503 ) def get_scaled_img_sizes(self, resolution): assert self._scales_set and self._sizes_set Images.check_resolution(resolution) return [ self._get_scaler(resolution).get_scaled_img_size(sz) for sz in self._sizes ] def read_image(img_name): img = cv.imread(img_name) if img is None: raise StitchingError("Cannot read image " + img_name) return img def get_image_size(img): """(width, height)""" return (img.shape[1], img.shape[0]) def resize_img_by_scaler(scaler, size, img): desired_size = scaler.get_scaled_img_size(size) return cv.resize(img, desired_size, interpolation=cv.INTER_LINEAR_EXACT) def check_resolution(resolution): assert isinstance(resolution, Enum) and resolution in Images.Resolution def resolve_wildcards(img_names): if len(img_names) == 1: img_names = [i for i in glob(img_names[0]) if not os.path.isdir(i)] return img_names def check_list_element_types(list_, type_): return all([isinstance(element, type_) for element in list_]) def to_binary(img): if len(img.shape) == 3: img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) _, binary = cv.threshold(img, 0.5, 255.0, cv.THRESH_BINARY) return binary.astype(np.uint8) class SeamFinder: """https://docs.opencv.org/4.x/d7/d09/classcv_1_1detail_1_1SeamFinder.html""" SEAM_FINDER_CHOICES = OrderedDict() SEAM_FINDER_CHOICES["dp_color"] = cv.detail_DpSeamFinder("COLOR") SEAM_FINDER_CHOICES["dp_colorgrad"] = cv.detail_DpSeamFinder("COLOR_GRAD") SEAM_FINDER_CHOICES["gc_color"] = cv.detail_GraphCutSeamFinder("COST_COLOR") SEAM_FINDER_CHOICES["gc_colorgrad"] = cv.detail_GraphCutSeamFinder( "COST_COLOR_GRAD" ) SEAM_FINDER_CHOICES["voronoi"] = cv.detail.SeamFinder_createDefault( cv.detail.SeamFinder_VORONOI_SEAM ) SEAM_FINDER_CHOICES["no"] = cv.detail.SeamFinder_createDefault( cv.detail.SeamFinder_NO ) DEFAULT_SEAM_FINDER = list(SEAM_FINDER_CHOICES.keys())[0] def __init__(self, finder=DEFAULT_SEAM_FINDER): self.finder = SeamFinder.SEAM_FINDER_CHOICES[finder] def find(self, imgs, corners, masks): imgs_float = [img.astype(np.float32) for img in imgs] return self.finder.find(imgs_float, corners, masks) def resize(seam_mask, mask): dilated_mask = cv.dilate(seam_mask, None) resized_seam_mask = cv.resize( dilated_mask, (mask.shape[1], mask.shape[0]), 0, 0, cv.INTER_LINEAR_EXACT ) return cv.bitwise_and(resized_seam_mask, mask) def draw_seam_mask(img, seam_mask, color=(0, 0, 0)): seam_mask = cv.UMat.get(seam_mask) overlaid_img = np.copy(img) overlaid_img[seam_mask == 0] = color return overlaid_img def draw_seam_polygons(panorama, blended_seam_masks, alpha=0.5): return add_weighted_image(panorama, blended_seam_masks, alpha) def draw_seam_lines(panorama, blended_seam_masks, linesize=1, color=(0, 0, 255)): seam_lines = SeamFinder.extract_seam_lines(blended_seam_masks, linesize) panorama_with_seam_lines = panorama.copy() panorama_with_seam_lines[seam_lines == 255] = color return panorama_with_seam_lines def extract_seam_lines(blended_seam_masks, linesize=1): seam_lines = cv.Canny(np.uint8(blended_seam_masks), 100, 200) seam_indices = (seam_lines == 255).nonzero() seam_lines = remove_invalid_line_pixels( seam_indices, seam_lines, blended_seam_masks ) kernelsize = linesize + linesize - 1 kernel = np.ones((kernelsize, kernelsize), np.uint8) return cv.dilate(seam_lines, kernel) def blend_seam_masks( seam_masks, corners, sizes, colors=( (255, 000, 000), # Red (000, 000, 255), # Blue (000, 255, 000), # Green (000, 255, 255), # Yellow (255, 000, 255), # Purple (128, 128, 255), # Pink (128, 128, 128), # Gray (000, 000, 128), # Dark Blue (000, 128, 255), # Light Blue ), ): imgs = colored_img_generator(sizes, colors) blended_seam_masks, _ = Blender.create_panorama( imgs, seam_masks, corners, sizes ) return blended_seam_masks class Subsetter: """https://docs.opencv.org/4.x/d7/d74/group__stitching__rotation.html#ga855d2fccbcfc3b3477b34d415be5e786 and https://docs.opencv.org/4.x/d7/d74/group__stitching__rotation.html#gabaeb9dab170ea8066ae2583bf3a669e9 """ # noqa DEFAULT_CONFIDENCE_THRESHOLD = 1 DEFAULT_MATCHES_GRAPH_DOT_FILE = None def __init__( self, confidence_threshold=DEFAULT_CONFIDENCE_THRESHOLD, matches_graph_dot_file=DEFAULT_MATCHES_GRAPH_DOT_FILE, ): self.confidence_threshold = confidence_threshold self.save_file = matches_graph_dot_file def subset(self, img_names, features, matches): self.save_matches_graph_dot_file(img_names, matches) indices = self.get_indices_to_keep(features, matches) if len(indices) < len(img_names): warnings.warn( """Not all images are included in the final panorama. If this is not intended, use the 'matches_graph_dot_file' parameter to analyze your matches. You might want to lower the 'confidence_threshold' or try another 'detector' to include all your images.""", StitchingWarning, ) return indices def save_matches_graph_dot_file(self, img_names, pairwise_matches): if self.save_file: with open(self.save_file, "w") as filehandler: filehandler.write(self.get_matches_graph(img_names, pairwise_matches)) def get_matches_graph(self, img_names, pairwise_matches): return cv.detail.matchesGraphAsString( img_names, pairwise_matches, ( 0.00001 # see issue #56 if (self.confidence_threshold == 0) else self.confidence_threshold ), ) def get_indices_to_keep(self, features, pairwise_matches): indices = cv.detail.leaveBiggestComponent( features, pairwise_matches, self.confidence_threshold ) # see https://github.com/OpenStitching/stitching/issues/40 indices = indices.flatten() if len(indices) < 2: raise StitchingError( """No match exceeds the given confidence threshold. Do your images have enough overlap and common features? If yes, you might want to lower the 'confidence_threshold' or try another 'detector'.""" ) return indices def subset_list(list_to_subset, indices): return [list_to_subset[i] for i in indices] def subset_matches(pairwise_matches, indices): matches_matrix = FeatureMatcher.get_matches_matrix(pairwise_matches) matches_matrix_subset = matches_matrix[np.ix_(indices, indices)] matches_subset_list = list(chain.from_iterable(matches_matrix_subset.tolist())) return matches_subset_list class Timelapser: """https://docs.opencv.org/4.x/dd/dac/classcv_1_1detail_1_1Timelapser.html""" TIMELAPSE_CHOICES = ( "no", "as_is", "crop", ) DEFAULT_TIMELAPSE = "no" DEFAULT_TIMELAPSE_PREFIX = "fixed_" def __init__( self, timelapse=DEFAULT_TIMELAPSE, timelapse_prefix=DEFAULT_TIMELAPSE_PREFIX ): self.do_timelapse = True self.timelapse_type = None self.timelapser = None self.timelapse_prefix = timelapse_prefix if timelapse == "as_is": self.timelapse_type = cv.detail.Timelapser_AS_IS elif timelapse == "crop": self.timelapse_type = cv.detail.Timelapser_CROP else: self.do_timelapse = False if self.do_timelapse: self.timelapser = cv.detail.Timelapser_createDefault(self.timelapse_type) def initialize(self, *args): self.timelapser.initialize(*args) def process_and_save_frame(self, img_name, img, corner): self.process_frame(img, corner) cv.imwrite(self.get_fixed_filename(img_name), self.get_frame()) def process_frame(self, img, corner): mask = np.ones((img.shape[0], img.shape[1]), np.uint8) img = img.astype(np.int16) self.timelapser.process(img, mask, corner) def get_frame(self): frame = self.timelapser.getDst() frame = np.float32(cv.UMat.get(frame)) frame = cv.convertScaleAbs(frame) return frame def get_fixed_filename(self, img_name): dirname, filename = os.path.split(img_name) return os.path.join(dirname, self.timelapse_prefix + filename) class Warper: """https://docs.opencv.org/4.x/da/db8/classcv_1_1detail_1_1RotationWarper.html""" WARP_TYPE_CHOICES = ( "spherical", "plane", "affine", "cylindrical", "fisheye", "stereographic", "compressedPlaneA2B1", "compressedPlaneA1.5B1", "compressedPlanePortraitA2B1", "compressedPlanePortraitA1.5B1", "paniniA2B1", "paniniA1.5B1", "paniniPortraitA2B1", "paniniPortraitA1.5B1", "mercator", "transverseMercator", ) DEFAULT_WARP_TYPE = "spherical" def __init__(self, warper_type=DEFAULT_WARP_TYPE): self.warper_type = warper_type self.scale = None def set_scale(self, cameras): focals = [cam.focal for cam in cameras] self.scale = median(focals) def warp_images(self, imgs, cameras, aspect=1): for img, camera in zip(imgs, cameras): yield self.warp_image(img, camera, aspect) def warp_image(self, img, camera, aspect=1): warper = cv.PyRotationWarper(self.warper_type, self.scale * aspect) _, warped_image = warper.warp( img, Warper.get_K(camera, aspect), camera.R, cv.INTER_LINEAR, cv.BORDER_REFLECT, ) return warped_image def create_and_warp_masks(self, sizes, cameras, aspect=1): for size, camera in zip(sizes, cameras): yield self.create_and_warp_mask(size, camera, aspect) def create_and_warp_mask(self, size, camera, aspect=1): warper = cv.PyRotationWarper(self.warper_type, self.scale * aspect) mask = 255 * np.ones((size[1], size[0]), np.uint8) _, warped_mask = warper.warp( mask, Warper.get_K(camera, aspect), camera.R, cv.INTER_NEAREST, cv.BORDER_CONSTANT, ) return warped_mask def warp_rois(self, sizes, cameras, aspect=1): roi_corners = [] roi_sizes = [] for size, camera in zip(sizes, cameras): roi = self.warp_roi(size, camera, aspect) roi_corners.append(roi[0:2]) roi_sizes.append(roi[2:4]) return roi_corners, roi_sizes def warp_roi(self, size, camera, aspect=1): warper = cv.PyRotationWarper(self.warper_type, self.scale * aspect) K = Warper.get_K(camera, aspect) return warper.warpRoi(size, K, camera.R) def get_K(camera, aspect=1): K = camera.K().astype(np.float32) """ Modification of intrinsic parameters needed if cameras were obtained on different scale than the scale of the Images which should be warped """ K[0, 0] *= aspect K[0, 2] *= aspect K[1, 1] *= aspect K[1, 2] *= aspect return K def create_parser(): parser = argparse.ArgumentParser(prog="stitch.py") parser.add_argument("--version", action="version", version=__version__) parser.add_argument("images", nargs="+", help="Files to stitch", type=str) parser.add_argument( "-v", "--verbose", action="store_true", help="Creates a directory with verbose results.", ) parser.add_argument( "--verbose_dir", action="store", default=datetime.now().strftime("%Y%m%d_%H%M%S") + "_verbose_results", help="The directory where verbose results should be saved.", ) parser.add_argument( "--affine", action="store_true", help="Overwrites multiple parameters to optimize the stitching for " "scans and images captured by specialized devices.", ) parser.add_argument( "--medium_megapix", action="store", default=Images.Resolution.MEDIUM.value, help="Resolution for image registration step. " "The default is %s Mpx" % Images.Resolution.MEDIUM.value, type=float, ) parser.add_argument( "--detector", action="store", default=FeatureDetector.DEFAULT_DETECTOR, help="Type of features used for images matching. " "The default is '%s'." % FeatureDetector.DEFAULT_DETECTOR, choices=FeatureDetector.DETECTOR_CHOICES.keys(), type=str, ) parser.add_argument( "--nfeatures", action="store", default=500, help="Number of features to be detected per image. " "Only used for the detectors 'orb' and 'sift'. " "The default is 500.", type=int, ) parser.add_argument( "--feature_masks", nargs="*", default=[], help="Masks for selecting where features should be detected.", type=str, ) parser.add_argument( "--matcher_type", action="store", default=FeatureMatcher.DEFAULT_MATCHER, help="Matcher used for pairwise image matching. " "The default is '%s'." % FeatureMatcher.DEFAULT_MATCHER, choices=FeatureMatcher.MATCHER_CHOICES, type=str, ) parser.add_argument( "--range_width", action="store", default=FeatureMatcher.DEFAULT_RANGE_WIDTH, help="uses range_width to limit number of images to match with.", type=int, ) parser.add_argument( "--try_use_gpu", action="store_true", help="Try to use CUDA", ) parser.add_argument( "--match_conf", action="store", help="Confidence for feature matching step. " "The default is 0.3 for ORB and 0.65 for other feature types.", type=float, ) parser.add_argument( "--confidence_threshold", action="store", default=Subsetter.DEFAULT_CONFIDENCE_THRESHOLD, help="Threshold for two images are from the same panorama confidence. " "The default is '%s'." % Subsetter.DEFAULT_CONFIDENCE_THRESHOLD, type=float, ) parser.add_argument( "--matches_graph_dot_file", action="store", default=Subsetter.DEFAULT_MATCHES_GRAPH_DOT_FILE, help="Save matches graph represented in DOT language to <file_name> file.", type=str, ) parser.add_argument( "--estimator", action="store", default=CameraEstimator.DEFAULT_CAMERA_ESTIMATOR, help="Type of estimator used for transformation estimation. " "The default is '%s'." % CameraEstimator.DEFAULT_CAMERA_ESTIMATOR, choices=CameraEstimator.CAMERA_ESTIMATOR_CHOICES.keys(), type=str, ) parser.add_argument( "--adjuster", action="store", default=CameraAdjuster.DEFAULT_CAMERA_ADJUSTER, help="Bundle adjustment cost function. " "The default is '%s'." % CameraAdjuster.DEFAULT_CAMERA_ADJUSTER, choices=CameraAdjuster.CAMERA_ADJUSTER_CHOICES.keys(), type=str, ) parser.add_argument( "--refinement_mask", action="store", default=CameraAdjuster.DEFAULT_REFINEMENT_MASK, help="Set refinement mask for bundle adjustment. It looks like 'x_xxx', " "where 'x' means refine respective parameter and '_' means don't " "refine, and has the following format:<fx><skew><ppx><aspect><ppy>. " "The default mask is '%s'. " "If bundle adjustment doesn't support estimation of selected " "parameter then the respective flag is ignored." "" % CameraAdjuster.DEFAULT_REFINEMENT_MASK, type=str, ) parser.add_argument( "--wave_correct_kind", action="store", default=WaveCorrector.DEFAULT_WAVE_CORRECTION, help="Perform wave effect correction. " "The default is '%s'" % WaveCorrector.DEFAULT_WAVE_CORRECTION, choices=WaveCorrector.WAVE_CORRECT_CHOICES.keys(), type=str, ) parser.add_argument( "--warper_type", action="store", default=Warper.DEFAULT_WARP_TYPE, help="Warp surface type. The default is '%s'." % Warper.DEFAULT_WARP_TYPE, choices=Warper.WARP_TYPE_CHOICES, type=str, ) parser.add_argument( "--low_megapix", action="store", default=Images.Resolution.LOW.value, help="Resolution for seam estimation and exposure estimation step. " "The default is %s Mpx." % Images.Resolution.LOW.value, type=float, ) parser.add_argument( "--crop", action="store_true", help="Crop black borders around images caused by warping using the " "largest interior rectangle. " "Default is '%s'." % Cropper.DEFAULT_CROP, ) parser.add_argument( "--no-crop", action="store_false", help="Don't Crop black borders around images caused by warping using the " "largest interior rectangle. " "Default is '%s'." % (not Cropper.DEFAULT_CROP), dest="crop", ) parser.set_defaults(crop=Cropper.DEFAULT_CROP) parser.add_argument( "--compensator", action="store", default=ExposureErrorCompensator.DEFAULT_COMPENSATOR, help="Exposure compensation method. " "The default is '%s'." % ExposureErrorCompensator.DEFAULT_COMPENSATOR, choices=ExposureErrorCompensator.COMPENSATOR_CHOICES.keys(), type=str, ) parser.add_argument( "--nr_feeds", action="store", default=ExposureErrorCompensator.DEFAULT_NR_FEEDS, help="Number of exposure compensation feed.", type=np.int32, ) parser.add_argument( "--block_size", action="store", default=ExposureErrorCompensator.DEFAULT_BLOCK_SIZE, help="BLock size in pixels used by the exposure compensator. " "The default is '%s'." % ExposureErrorCompensator.DEFAULT_BLOCK_SIZE, type=np.int32, ) parser.add_argument( "--finder", action="store", default=SeamFinder.DEFAULT_SEAM_FINDER, help="Seam estimation method. " "The default is '%s'." % SeamFinder.DEFAULT_SEAM_FINDER, choices=SeamFinder.SEAM_FINDER_CHOICES.keys(), type=str, ) parser.add_argument( "--final_megapix", action="store", default=Images.Resolution.FINAL.value, help="Resolution for compositing step. Use -1 for original resolution. " "The default is %s" % Images.Resolution.FINAL.value, type=float, ) parser.add_argument( "--blender_type", action="store", default=Blender.DEFAULT_BLENDER, help="Blending method. The default is '%s'." % Blender.DEFAULT_BLENDER, choices=Blender.BLENDER_CHOICES, type=str, ) parser.add_argument( "--blend_strength", action="store", default=Blender.DEFAULT_BLEND_STRENGTH, help="Blending strength from [0,100] range. " "The default is '%s'." % Blender.DEFAULT_BLEND_STRENGTH, type=np.int32, ) parser.add_argument( "--timelapse", action="store", default=Timelapser.DEFAULT_TIMELAPSE, help="Output warped images separately as frames of a time lapse movie, " "with 'fixed_' prepended to input file names. " "The default is '%s'." % Timelapser.DEFAULT_TIMELAPSE, choices=Timelapser.TIMELAPSE_CHOICES, type=str, ) parser.add_argument( "--preview", action="store_true", help="Opens a preview window with the stitched result", ) parser.add_argument( "--output", action="store", default="result.jpg", help="The default is 'result.jpg'", type=str, ) return parser
null
179,560
import os import cv2 as cv from .images import Images from .seam_finder import SeamFinder from .timelapser import Timelapser def write_verbose_result(dir_name, img_name, img): cv.imwrite(verbose_output(dir_name, img_name), img) def verbose_output(dir_name, file): return os.path.join(dir_name, file) class Images(ABC): class Resolution(Enum): MEDIUM = 0.6 LOW = 0.1 FINAL = -1 def of( images, medium_megapix=Resolution.MEDIUM.value, low_megapix=Resolution.LOW.value, final_megapix=Resolution.FINAL.value, ): if not isinstance(images, list): raise StitchingError("images must be a list of images or filenames") if len(images) == 0: raise StitchingError("images must not be an empty list") if Images.check_list_element_types(images, np.ndarray): return _NumpyImages(images, medium_megapix, low_megapix, final_megapix) elif Images.check_list_element_types(images, str): return _FilenameImages(images, medium_megapix, low_megapix, final_megapix) else: raise StitchingError( """invalid images list: must be numpy arrays (loaded images) or filename strings""" ) def __init__(self, images, medium_megapix, low_megapix, final_megapix): if medium_megapix < low_megapix: raise StitchingError( "Medium resolution megapix need to be " "greater or equal than low resolution " "megapix" ) self._scalers = {} self._scalers["MEDIUM"] = MegapixDownscaler(medium_megapix) self._scalers["LOW"] = MegapixDownscaler(low_megapix) self._scalers["FINAL"] = MegapixDownscaler(final_megapix) self._scales_set = False self._sizes_set = False self._names_set = False def sizes(self): assert self._sizes_set return self._sizes def names(self): assert self._names_set return self._names def subset(self, indices): self._sizes = [self._sizes[i] for i in indices] self._names = [self._names[i] for i in indices] def resize(self, resolution, imgs=None): img_iterable = self.__iter__() if imgs is None else imgs for idx, img in enumerate(img_iterable): yield Images.resize_img_by_scaler( self._get_scaler(resolution), self._sizes[idx], img ) def __iter__(self): pass def _set_scales(self, size): if not self._scales_set: for scaler in self._scalers.values(): scaler.set_scale_by_img_size(size) self._scales_set = True def _get_scaler(self, resolution): Images.check_resolution(resolution) return self._scalers[resolution.name] def get_ratio(self, from_resolution, to_resolution): assert self._scales_set Images.check_resolution(from_resolution) Images.check_resolution(to_resolution) return ( self._get_scaler(to_resolution).scale / self._get_scaler(from_resolution).scale # noqa: W503 ) def get_scaled_img_sizes(self, resolution): assert self._scales_set and self._sizes_set Images.check_resolution(resolution) return [ self._get_scaler(resolution).get_scaled_img_size(sz) for sz in self._sizes ] def read_image(img_name): img = cv.imread(img_name) if img is None: raise StitchingError("Cannot read image " + img_name) return img def get_image_size(img): """(width, height)""" return (img.shape[1], img.shape[0]) def resize_img_by_scaler(scaler, size, img): desired_size = scaler.get_scaled_img_size(size) return cv.resize(img, desired_size, interpolation=cv.INTER_LINEAR_EXACT) def check_resolution(resolution): assert isinstance(resolution, Enum) and resolution in Images.Resolution def resolve_wildcards(img_names): if len(img_names) == 1: img_names = [i for i in glob(img_names[0]) if not os.path.isdir(i)] return img_names def check_list_element_types(list_, type_): return all([isinstance(element, type_) for element in list_]) def to_binary(img): if len(img.shape) == 3: img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) _, binary = cv.threshold(img, 0.5, 255.0, cv.THRESH_BINARY) return binary.astype(np.uint8) class SeamFinder: """https://docs.opencv.org/4.x/d7/d09/classcv_1_1detail_1_1SeamFinder.html""" SEAM_FINDER_CHOICES = OrderedDict() SEAM_FINDER_CHOICES["dp_color"] = cv.detail_DpSeamFinder("COLOR") SEAM_FINDER_CHOICES["dp_colorgrad"] = cv.detail_DpSeamFinder("COLOR_GRAD") SEAM_FINDER_CHOICES["gc_color"] = cv.detail_GraphCutSeamFinder("COST_COLOR") SEAM_FINDER_CHOICES["gc_colorgrad"] = cv.detail_GraphCutSeamFinder( "COST_COLOR_GRAD" ) SEAM_FINDER_CHOICES["voronoi"] = cv.detail.SeamFinder_createDefault( cv.detail.SeamFinder_VORONOI_SEAM ) SEAM_FINDER_CHOICES["no"] = cv.detail.SeamFinder_createDefault( cv.detail.SeamFinder_NO ) DEFAULT_SEAM_FINDER = list(SEAM_FINDER_CHOICES.keys())[0] def __init__(self, finder=DEFAULT_SEAM_FINDER): self.finder = SeamFinder.SEAM_FINDER_CHOICES[finder] def find(self, imgs, corners, masks): imgs_float = [img.astype(np.float32) for img in imgs] return self.finder.find(imgs_float, corners, masks) def resize(seam_mask, mask): dilated_mask = cv.dilate(seam_mask, None) resized_seam_mask = cv.resize( dilated_mask, (mask.shape[1], mask.shape[0]), 0, 0, cv.INTER_LINEAR_EXACT ) return cv.bitwise_and(resized_seam_mask, mask) def draw_seam_mask(img, seam_mask, color=(0, 0, 0)): seam_mask = cv.UMat.get(seam_mask) overlaid_img = np.copy(img) overlaid_img[seam_mask == 0] = color return overlaid_img def draw_seam_polygons(panorama, blended_seam_masks, alpha=0.5): return add_weighted_image(panorama, blended_seam_masks, alpha) def draw_seam_lines(panorama, blended_seam_masks, linesize=1, color=(0, 0, 255)): seam_lines = SeamFinder.extract_seam_lines(blended_seam_masks, linesize) panorama_with_seam_lines = panorama.copy() panorama_with_seam_lines[seam_lines == 255] = color return panorama_with_seam_lines def extract_seam_lines(blended_seam_masks, linesize=1): seam_lines = cv.Canny(np.uint8(blended_seam_masks), 100, 200) seam_indices = (seam_lines == 255).nonzero() seam_lines = remove_invalid_line_pixels( seam_indices, seam_lines, blended_seam_masks ) kernelsize = linesize + linesize - 1 kernel = np.ones((kernelsize, kernelsize), np.uint8) return cv.dilate(seam_lines, kernel) def blend_seam_masks( seam_masks, corners, sizes, colors=( (255, 000, 000), # Red (000, 000, 255), # Blue (000, 255, 000), # Green (000, 255, 255), # Yellow (255, 000, 255), # Purple (128, 128, 255), # Pink (128, 128, 128), # Gray (000, 000, 128), # Dark Blue (000, 128, 255), # Light Blue ), ): imgs = colored_img_generator(sizes, colors) blended_seam_masks, _ = Blender.create_panorama( imgs, seam_masks, corners, sizes ) return blended_seam_masks class Timelapser: """https://docs.opencv.org/4.x/dd/dac/classcv_1_1detail_1_1Timelapser.html""" TIMELAPSE_CHOICES = ( "no", "as_is", "crop", ) DEFAULT_TIMELAPSE = "no" DEFAULT_TIMELAPSE_PREFIX = "fixed_" def __init__( self, timelapse=DEFAULT_TIMELAPSE, timelapse_prefix=DEFAULT_TIMELAPSE_PREFIX ): self.do_timelapse = True self.timelapse_type = None self.timelapser = None self.timelapse_prefix = timelapse_prefix if timelapse == "as_is": self.timelapse_type = cv.detail.Timelapser_AS_IS elif timelapse == "crop": self.timelapse_type = cv.detail.Timelapser_CROP else: self.do_timelapse = False if self.do_timelapse: self.timelapser = cv.detail.Timelapser_createDefault(self.timelapse_type) def initialize(self, *args): self.timelapser.initialize(*args) def process_and_save_frame(self, img_name, img, corner): self.process_frame(img, corner) cv.imwrite(self.get_fixed_filename(img_name), self.get_frame()) def process_frame(self, img, corner): mask = np.ones((img.shape[0], img.shape[1]), np.uint8) img = img.astype(np.int16) self.timelapser.process(img, mask, corner) def get_frame(self): frame = self.timelapser.getDst() frame = np.float32(cv.UMat.get(frame)) frame = cv.convertScaleAbs(frame) return frame def get_fixed_filename(self, img_name): dirname, filename = os.path.split(img_name) return os.path.join(dirname, self.timelapse_prefix + filename) def verbose_stitching(stitcher, images, feature_masks=[], verbose_dir=None): _dir = "." if verbose_dir is None else verbose_dir images = Images.of( images, stitcher.medium_megapix, stitcher.low_megapix, stitcher.final_megapix ) # Resize Images imgs = list(images.resize(Images.Resolution.MEDIUM)) # Find Features finder = stitcher.detector features = stitcher.find_features(imgs, feature_masks) for idx, img_features in enumerate(features): img_with_features = finder.draw_keypoints(imgs[idx], img_features) write_verbose_result(_dir, f"01_features_img{idx+1}.jpg", img_with_features) # Match Features matcher = stitcher.matcher matches = matcher.match_features(features) # Subset subsetter = stitcher.subsetter all_relevant_matches = list( matcher.draw_matches_matrix( imgs, features, matches, conf_thresh=subsetter.confidence_threshold, inliers=True, matchColor=(0, 255, 0), ) ) for idx1, idx2, img in all_relevant_matches: write_verbose_result(_dir, f"02_matches_img{idx1+1}_to_img{idx2+1}.jpg", img) # Subset subsetter = stitcher.subsetter subsetter.save_file = verbose_output(_dir, "03_matches_graph.txt") subsetter.save_matches_graph_dot_file(images.names, matches) indices = subsetter.get_indices_to_keep(features, matches) imgs = subsetter.subset_list(imgs, indices) features = subsetter.subset_list(features, indices) matches = subsetter.subset_matches(matches, indices) images.subset(indices) # Camera Estimation, Adjustion and Correction camera_estimator = stitcher.camera_estimator camera_adjuster = stitcher.camera_adjuster wave_corrector = stitcher.wave_corrector cameras = camera_estimator.estimate(features, matches) cameras = camera_adjuster.adjust(features, matches, cameras) cameras = wave_corrector.correct(cameras) # Warp Images low_imgs = list(images.resize(Images.Resolution.LOW, imgs)) imgs = None # free memory warper = stitcher.warper warper.set_scale(cameras) low_sizes = images.get_scaled_img_sizes(Images.Resolution.LOW) camera_aspect = images.get_ratio(Images.Resolution.MEDIUM, Images.Resolution.LOW) low_imgs = list(warper.warp_images(low_imgs, cameras, camera_aspect)) low_masks = list(warper.create_and_warp_masks(low_sizes, cameras, camera_aspect)) low_corners, low_sizes = warper.warp_rois(low_sizes, cameras, camera_aspect) final_sizes = images.get_scaled_img_sizes(Images.Resolution.FINAL) camera_aspect = images.get_ratio(Images.Resolution.MEDIUM, Images.Resolution.FINAL) final_imgs = list(images.resize(Images.Resolution.FINAL)) final_imgs = list(warper.warp_images(final_imgs, cameras, camera_aspect)) final_masks = list( warper.create_and_warp_masks(final_sizes, cameras, camera_aspect) ) final_corners, final_sizes = warper.warp_rois(final_sizes, cameras, camera_aspect) for idx, warped_img in enumerate(final_imgs): write_verbose_result(_dir, f"04_warped_img{idx+1}.jpg", warped_img) # Excursion: Timelapser timelapser = Timelapser("as_is") timelapser.initialize(final_corners, final_sizes) for idx, (img, corner) in enumerate(zip(final_imgs, final_corners)): timelapser.process_frame(img, corner) frame = timelapser.get_frame() write_verbose_result(_dir, f"05_timelapse_img{idx+1}.jpg", frame) # Crop cropper = stitcher.cropper if cropper.do_crop: mask = cropper.estimate_panorama_mask( low_imgs, low_masks, low_corners, low_sizes ) write_verbose_result(_dir, "06_estimated_mask_to_crop.jpg", mask) lir = cropper.estimate_largest_interior_rectangle(mask) lir_to_crop = lir.draw_on(mask, size=2) write_verbose_result(_dir, "06_lir.jpg", lir_to_crop) low_corners = cropper.get_zero_center_corners(low_corners) cropper.prepare(low_imgs, low_masks, low_corners, low_sizes) low_masks = list(cropper.crop_images(low_masks)) low_imgs = list(cropper.crop_images(low_imgs)) low_corners, low_sizes = cropper.crop_rois(low_corners, low_sizes) lir_aspect = images.get_ratio(Images.Resolution.LOW, Images.Resolution.FINAL) final_masks = list(cropper.crop_images(final_masks, lir_aspect)) final_imgs = list(cropper.crop_images(final_imgs, lir_aspect)) final_corners, final_sizes = cropper.crop_rois( final_corners, final_sizes, lir_aspect ) timelapser = Timelapser("as_is") timelapser.initialize(final_corners, final_sizes) for idx, (img, corner) in enumerate(zip(final_imgs, final_corners)): timelapser.process_frame(img, corner) frame = timelapser.get_frame() write_verbose_result(_dir, f"07_timelapse_cropped_img{idx+1}.jpg", frame) # Seam Masks seam_finder = stitcher.seam_finder seam_masks = seam_finder.find(low_imgs, low_corners, low_masks) seam_masks = [ seam_finder.resize(seam_mask, mask) for seam_mask, mask in zip(seam_masks, final_masks) ] seam_masks_plots = [ SeamFinder.draw_seam_mask(img, seam_mask) for img, seam_mask in zip(final_imgs, seam_masks) ] for idx, seam_mask in enumerate(seam_masks_plots): write_verbose_result(_dir, f"08_seam_mask{idx+1}.jpg", seam_mask) # Exposure Error Compensation compensator = stitcher.compensator compensator.feed(low_corners, low_imgs, low_masks) compensated_imgs = [ compensator.apply(idx, corner, img, mask) for idx, (img, mask, corner) in enumerate( zip(final_imgs, final_masks, final_corners) ) ] for idx, compensated_img in enumerate(compensated_imgs): write_verbose_result(_dir, f"08_compensated{idx+1}.jpg", compensated_img) # Blending blender = stitcher.blender blender.prepare(final_corners, final_sizes) for img, mask, corner in zip(compensated_imgs, seam_masks, final_corners): blender.feed(img, mask, corner) panorama, _ = blender.blend() write_verbose_result(_dir, "09_result.jpg", panorama) blended_seam_masks = seam_finder.blend_seam_masks( seam_masks, final_corners, final_sizes ) with_seam_lines = seam_finder.draw_seam_lines( panorama, blended_seam_masks, linesize=3 ) with_seam_polygons = seam_finder.draw_seam_polygons(panorama, blended_seam_masks) write_verbose_result(_dir, "09_result_with_seam_lines.jpg", with_seam_lines) write_verbose_result(_dir, "09_result_with_seam_polygons.jpg", with_seam_polygons) return panorama
null
179,561
import warnings from collections import OrderedDict import cv2 as cv import numpy as np from .blender import Blender from .stitching_error import StitchingWarning def create_img_by_size(size, color=(0, 0, 0)): width, height = size img = np.zeros((height, width, 3), np.uint8) img[:] = color return img class StitchingWarning(UserWarning): pass def colored_img_generator(sizes, colors): if len(sizes) + 1 > len(colors): warnings.warn( """Without additional colors, there will be seam masks with identical colors""", StitchingWarning, ) for idx, size in enumerate(sizes): yield create_img_by_size(size, colors[idx % len(colors)])
null
179,562
import warnings from collections import OrderedDict import cv2 as cv import numpy as np from .blender import Blender from .stitching_error import StitchingWarning def add_weighted_image(img1, img2, alpha): return cv.addWeighted(img1, alpha, img2, (1.0 - alpha), 0.0)
null
179,563
import warnings from collections import OrderedDict import cv2 as cv import numpy as np from .blender import Blender from .stitching_error import StitchingWarning def check_if_pixel_or_neighbor_is_black(img, x, y): check = [ is_pixel_black(img, x, y), is_pixel_black(img, x + 1, y), is_pixel_black(img, x - 1, y), is_pixel_black(img, x, y + 1), is_pixel_black(img, x, y - 1), ] return any(check) def remove_invalid_line_pixels(indices, lines, mask): for x, y in zip(*indices): if check_if_pixel_or_neighbor_is_black(mask, x, y): lines[x, y] = 0 return lines
null
179,564
import datetime import logging import logging.handlers import os import sys from torch import nn import numpy as np import requests from videollava.constants import LOGDIR def order_pick_k(lst, k): if len(lst) <= k: return lst rng = np.random.random(len(lst)) index = np.argsort(rng)[:k] index_sort = sorted(index) new_lst = [lst[i] for i in index_sort] print( f"WARNING: total file: {len(lst)}, random pick: {k}." f" (ignored)" ) return new_lst
null
179,565
import datetime import logging import logging.handlers import os import sys from torch import nn import numpy as np import requests from videollava.constants import LOGDIR handler = None class StreamToLogger(object): """ Fake file-like stream object that redirects writes to a logger instance. """ def __init__(self, logger, log_level=logging.INFO): self.terminal = sys.stdout self.logger = logger self.log_level = log_level self.linebuf = '' def __getattr__(self, attr): return getattr(self.terminal, attr) def write(self, buf): temp_linebuf = self.linebuf + buf self.linebuf = '' for line in temp_linebuf.splitlines(True): # From the io.TextIOWrapper docs: # On output, if newline is None, any '\n' characters written # are translated to the system default line separator. # By default sys.stdout.write() expects '\n' newlines and then # translates them so this is still cross platform. if line[-1] == '\n': self.logger.log(self.log_level, line.rstrip()) else: self.linebuf += line def flush(self): if self.linebuf != '': self.logger.log(self.log_level, self.linebuf.rstrip()) self.linebuf = '' LOGDIR = "." def build_logger(logger_name, logger_filename): global handler formatter = logging.Formatter( fmt="%(asctime)s | %(levelname)s | %(name)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) # Set the format of root handlers if not logging.getLogger().handlers: logging.basicConfig(level=logging.INFO) logging.getLogger().handlers[0].setFormatter(formatter) # Redirect stdout and stderr to loggers stdout_logger = logging.getLogger("stdout") stdout_logger.setLevel(logging.INFO) sl = StreamToLogger(stdout_logger, logging.INFO) sys.stdout = sl stderr_logger = logging.getLogger("stderr") stderr_logger.setLevel(logging.ERROR) sl = StreamToLogger(stderr_logger, logging.ERROR) sys.stderr = sl # Get logger logger = logging.getLogger(logger_name) logger.setLevel(logging.INFO) # Add a file handler for all loggers if handler is None: os.makedirs(LOGDIR, exist_ok=True) filename = os.path.join(LOGDIR, logger_filename) handler = logging.handlers.TimedRotatingFileHandler( filename, when='D', utc=True, encoding='UTF-8') handler.setFormatter(formatter) for name, item in logging.root.manager.loggerDict.items(): if isinstance(item, logging.Logger): item.addHandler(handler) return logger
null
179,566
import datetime import logging import logging.handlers import os import sys from torch import nn import numpy as np import requests from videollava.constants import LOGDIR The provided code snippet includes necessary dependencies for implementing the `violates_moderation` function. Write a Python function `def violates_moderation(text)` to solve the following problem: Check whether the text violates OpenAI moderation API. Here is the function: def violates_moderation(text): """ Check whether the text violates OpenAI moderation API. """ url = "https://api.openai.com/v1/moderations" headers = {"Content-Type": "application/json", "Authorization": "Bearer " + os.environ["OPENAI_API_KEY"]} text = text.replace("\n", "") data = "{" + '"input": ' + f'"{text}"' + "}" data = data.encode("utf-8") try: ret = requests.post(url, headers=headers, data=data, timeout=5) flagged = ret.json()["results"][0]["flagged"] except requests.exceptions.RequestException as e: flagged = False except KeyError as e: flagged = False return flagged
Check whether the text violates OpenAI moderation API.
179,567
import datetime import logging import logging.handlers import os import sys from torch import nn import numpy as np import requests from videollava.constants import LOGDIR def pretty_print_semaphore(semaphore): if semaphore is None: return "None" return f"Semaphore(value={semaphore._value}, locked={semaphore.locked()})"
null
179,568
from io import BytesIO import requests from PIL import Image def load_image(image_file): if image_file.startswith('http://') or image_file.startswith('https://'): response = requests.get(image_file) image = Image.open(BytesIO(response.content)).convert('RGB') else: image = Image.open(image_file).convert('RGB') return image
null
179,569
import argparse import asyncio import json import time import threading import uuid from fastapi import FastAPI, Request, BackgroundTasks from fastapi.responses import StreamingResponse import requests import torch import uvicorn from functools import partial from videollava.constants import WORKER_HEART_BEAT_INTERVAL from videollava.utils import (build_logger, server_error_msg, pretty_print_semaphore) from videollava.model.builder import load_pretrained_model from videollava.mm_utils import process_images, load_image_from_base64, tokenizer_image_token, KeywordsStoppingCriteria from videollava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN from transformers import TextIteratorStreamer from threading import Thread WORKER_HEART_BEAT_INTERVAL = 15 def heart_beat_worker(controller): while True: time.sleep(WORKER_HEART_BEAT_INTERVAL) controller.send_heart_beat()
null
179,570
import argparse import asyncio import json import time import threading import uuid from fastapi import FastAPI, Request, BackgroundTasks from fastapi.responses import StreamingResponse import requests import torch import uvicorn from functools import partial from videollava.constants import WORKER_HEART_BEAT_INTERVAL from videollava.utils import (build_logger, server_error_msg, pretty_print_semaphore) from videollava.model.builder import load_pretrained_model from videollava.mm_utils import process_images, load_image_from_base64, tokenizer_image_token, KeywordsStoppingCriteria from videollava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN from transformers import TextIteratorStreamer from threading import Thread global_counter = 0 model_semaphore = None def release_model_semaphore(fn=None): async def generate_stream(request: Request): global model_semaphore, global_counter global_counter += 1 params = await request.json() if model_semaphore is None: model_semaphore = asyncio.Semaphore(args.limit_model_concurrency) await model_semaphore.acquire() worker.send_heart_beat() generator = worker.generate_stream_gate(params) background_tasks = BackgroundTasks() background_tasks.add_task(partial(release_model_semaphore, fn=worker.send_heart_beat)) return StreamingResponse(generator, background=background_tasks)
null
179,571
import argparse import asyncio import json import time import threading import uuid from fastapi import FastAPI, Request, BackgroundTasks from fastapi.responses import StreamingResponse import requests import torch import uvicorn from functools import partial from videollava.constants import WORKER_HEART_BEAT_INTERVAL from videollava.utils import (build_logger, server_error_msg, pretty_print_semaphore) from videollava.model.builder import load_pretrained_model from videollava.mm_utils import process_images, load_image_from_base64, tokenizer_image_token, KeywordsStoppingCriteria from videollava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN from transformers import TextIteratorStreamer from threading import Thread async def get_status(request: Request): return worker.get_status()
null
179,572
import argparse import asyncio import dataclasses from enum import Enum, auto import json import logging import time from typing import List, Union import threading from fastapi import FastAPI, Request from fastapi.responses import StreamingResponse import numpy as np import requests import uvicorn from videollava.constants import CONTROLLER_HEART_BEAT_EXPIRATION from videollava.utils import build_logger, server_error_msg CONTROLLER_HEART_BEAT_EXPIRATION = 30 def heart_beat_controller(controller): while True: time.sleep(CONTROLLER_HEART_BEAT_EXPIRATION) controller.remove_stable_workers_by_expiration()
null
179,573
import argparse import asyncio import dataclasses from enum import Enum, auto import json import logging import time from typing import List, Union import threading from fastapi import FastAPI, Request from fastapi.responses import StreamingResponse import numpy as np import requests import uvicorn from videollava.constants import CONTROLLER_HEART_BEAT_EXPIRATION from videollava.utils import build_logger, server_error_msg async def register_worker(request: Request): data = await request.json() controller.register_worker( data["worker_name"], data["check_heart_beat"], data.get("worker_status", None))
null
179,574
import argparse import asyncio import dataclasses from enum import Enum, auto import json import logging import time from typing import List, Union import threading from fastapi import FastAPI, Request from fastapi.responses import StreamingResponse import numpy as np import requests import uvicorn from videollava.constants import CONTROLLER_HEART_BEAT_EXPIRATION from videollava.utils import build_logger, server_error_msg async def refresh_all_workers(): models = controller.refresh_all_workers()
null
179,575
import argparse import asyncio import dataclasses from enum import Enum, auto import json import logging import time from typing import List, Union import threading from fastapi import FastAPI, Request from fastapi.responses import StreamingResponse import numpy as np import requests import uvicorn from videollava.constants import CONTROLLER_HEART_BEAT_EXPIRATION from videollava.utils import build_logger, server_error_msg async def list_models(): models = controller.list_models() return {"models": models}
null