Hi is there any guide to run on my own PC?

#1
by teknium - opened

I want to run it from a python script to judge image gens automatically. Possible? Any guide to run these?

One thing you can do is mirror this https://huggingface.co/spaces/cafeai/cafe_aesthetic_demo/tree/main and then use web api from your own python script like this
for files in os.listdir(directory):
with open(directory + '/' + files, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
response = requests.post("http://127.0.0.1:7861/run/predict", json={
"data": [
"data:image/" + pathlib.Path(files).suffix + ";base64," + encoded_string.decode(ENCODING),
]}).json()
print(response)

sample code

from transformers import AutoFeatureExtractor, AutoModelForImageClassification

extractor = AutoFeatureExtractor.from_pretrained("cafeai/cafe_aesthetic")

model = AutoModelForImageClassification.from_pretrained("cafeai/cafe_aesthetic")

files = []
import glob
for ext in ['.png', '.jpg'] :
    files.extend(glob.glob('./*' + ext))

import torch
import einops
import numpy as np
from PIL import Image
with torch.no_grad() :
    for f in files :
        im = Image.open(f).convert('RGB')
        im = einops.rearrange(extractor(im).data['pixel_values'][0], 'c h w->1 c h w')
        score = model(torch.from_numpy(im)).logits.softmax(dim = -1)[0][-1].item()
        print(f, score)

Sign up or log in to comment