This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rouge_score.rouge_scorer import RougeScorer | |
from rouge_score.tokenize import SPACES_RE | |
from rouge_score.tokenizers import Tokenizer | |
class NonAlphaNumericSupportTokenizer(Tokenizer): | |
""" | |
>>> NonAlphaNumericSupportTokenizer().tokenize("いぬ ねこ") | |
['いぬ', 'ねこ'] | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
>>> HogeTranslator().foo(1) | |
MyTranslator foo | |
108 | |
>>> HogeTranslator().foo(-1) | |
MyTranslator foo | |
Translator foo | |
42 | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code with bug. ref: https://stackoverflow.com/q/32831150 | |
class A: | |
def __init__(self, v, v2): | |
self.v = v | |
self.v2 = v2 | |
class B(A): | |
def __init__(self, v, v2): | |
print(self.__class__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref: https://github.com/asciidwango/ExpertPython3_Source/blob/887b151bc5f20b6b6e742eff1f18d4ab6e86d872/chapter17/interfaces_abc.py | |
from abc import ABCMeta, abstractmethod | |
class RectangleInterface(metaclass=ABCMeta): | |
@abstractmethod | |
def area(self): | |
"""面積を返す""" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/ftnext/unko-by-rye/blob/500e6a8cb5f9c510c99820a9ed2148e60a77e45f/docker/Dockerfile | |
FROM python:3.12-bookworm AS builder | |
WORKDIR /work | |
COPY requirements.lock requirements.lock | |
RUN <<install_python_packages | |
sed -i '/^-e/d' requirements.lock | |
python -m pip install --no-cache-dir -r requirements.lock | |
install_python_packages | |
COPY . . | |
RUN python -m pip install --no-cache-dir . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref: https://docs.pydantic.dev/2.6/concepts/unions/#discriminated-unions | |
from typing import Literal | |
from pydantic import BaseModel, Field | |
class Cat(BaseModel): | |
pet_type: Literal["cat"] | |
meows: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://gist.github.com/ftnext/b0f4db8dc71333f7b663c4f5da9ec16f | |
import numpy as np | |
from openai import OpenAI | |
client = OpenAI() | |
def get_embedding(text, model): | |
response = client.embeddings.create(input=text, model=model) # dimensions=256 | |
embedding = response.data[0].embedding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://gist.github.com/ftnext/74e19bc4e478fd02bfed72de3552512c | |
import numpy as np | |
from openai import OpenAI | |
client = OpenAI() | |
def get_embedding(text, model): | |
response = client.embeddings.create(input=text, model=model) | |
embedding = response.data[0].embedding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref: https://huggingface.co/blog/how-to-generate#sampling | |
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed | |
tokenizer = AutoTokenizer.from_pretrained("gpt2") | |
model = AutoModelForCausalLM.from_pretrained("gpt2") | |
def generate(prompt): | |
model_inputs = tokenizer(prompt, return_tensors="pt") | |
output = model.generate( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See ref: https://github.com/ftnext/transcendent-book-py/blob/ce7acd2daa4e8ed0770e8739dabe2de9b0046388/chapter8/composite_example.py | |
class Node(metaclass=ABCMeta): | |
def __init__(self, name: str) -> None: | |
self.name = name | |
self.parent: Branch | None = None | |
@abstractmethod | |
def __str__(self) -> str: | |
... |