File size: 1,880 Bytes
af5e18d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import httpx

class TogetherImageAPI:
    headers = {
        'sec-ch-ua-platform': '"macOS"',
        'Authorization': 'Bearer 869f5ecd80fc6482ccb99f37179cb2c162879925224ae9d91caffc35d9b534b3',
        'Referer': 'https://api.together.ai/playground/image/black-forest-labs/FLUX.1-dev',
        'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
    }

    def __init__(self):
        self.name = "TogetherImageAPI"
        self.url = "https://api.together.ai/inferences"

    def get_model_list(self):
        models = [
            "black-forest-labs/FLUX.1-dev",
        ]
        return models

    async def generate(self, json_data):
        print(json_data)
        model = json_data['modelId']
        prompt = json_data['prompt']
        negative_prompt = json_data.get('negative_prompt', '')
        width = json_data.get('width', 1024)
        height = json_data.get('height', 768)
        steps = json_data.get('steps', 28)

        data = {
            'model': model,
            'prompt': prompt,
            'negative_prompt': negative_prompt,
            'width': width,
            'height': height,
            'steps': steps,
            'n': 1,
            'response_format': 'b64_json',
            'stop': [],
        }

        async with httpx.AsyncClient() as client:
            response = await client.post("https://api.together.ai/inference", json=json_data)
            if response.status_code == 200:
                return response.json()
            else:
                raise Exception(f"Error: {response.status_code} - {response.text}")