File size: 1,217 Bytes
bd0bce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import json
import requests
from PIL import Image
import io
import base64
class PlayGround():
    def __init__(self, model_name, model_type=None):
        self.model_name = model_name
        self.model_type = model_type
        self.api_key = os.environ['PlaygroundAPI']
        if model_name == "PlayGroundV2":
            self._model_name = "Playground_v2"
        elif model_name == "PlayGroundV2.5":
            self._model_name = "Playground_v2.5"

        
    def __call__(self, prompt):
        headers = {
            'Content-Type': 'application/json',
            'Authorization': "Bearer " + self.api_key,
        }

        data = json.dumps({"prompt": prompt, "filter_model": self._model_name, "scheduler": "DPMPP_2M_K", "guidance_scale": 3})

        response = requests.post('https://playground.com/api/models/external/v1', headers=headers, data=data)
        response.raise_for_status() 
        json_obj = response.json()
        image_base64 = json_obj['images'][0]
        img = Image.open(io.BytesIO(base64.decodebytes(bytes(image_base64, "utf-8"))))

        return img
    
def load_playground_model(model_name, model_type="generation"):
    return PlayGround(model_name, model_type)