Readme
cog.yaml
# Configuration for Cog ⚙️
# Reference: https://cog.run/yaml
build:
# set to true if your model requires a GPU
gpu: false
# a list of ubuntu apt packages to install
# system_packages:
# - "libgl1-mesa-glx"
# - "libglib2.0-0"
# python version in the form '3.11' or '3.11.4'
python_version: "3.11"
# path to a Python requirements.txt file
python_requirements: requirements.txt
# enable fast boots
fast: false
# commands run after the environment is setup
# run:
# - "echo env is ready!"
# - "echo another command if needed"
# predict.py defines how predictions are run on your model
predict: "predict.py:Predictor"
# Prediction interface for Cog ⚙️
# https://cog.run/python
from cog import BasePredictor, Input, Path
from PIL import Image
import os
import tempfile
class Predictor(BasePredictor):
def predict(
self,
images: list[Path] = Input(description="A list of images to scale"),
scale: float = Input(
description="Factor to scale image by", ge=0, le=10, default=1.5
),
) -> list[Path]:
"""Run a single prediction on the model"""
scaled_images = []
for image_path in images:
with Image.open(image_path) as img:
original_width, original_height = img.size
new_width = int(original_width * scale)
new_height = int(original_height * scale)
scaled_img = img.resize((new_width, new_height), Image.LANCZOS)
# Create a temporary file for the scaled image
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(str(image_path))[1]) as temp_file:
scaled_img.save(temp_file.name)
scaled_images.append(Path(temp_file.name))
return scaled_images
requirements.txt
# This is a normal Python requirements.txt file.
# You can add dependencies directly from PyPI:
Pillow==10.2.0