forked from Lightning-Universe/DiffusionWithAutoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcold_start_proxy.py
More file actions
44 lines (34 loc) · 1.29 KB
/
cold_start_proxy.py
File metadata and controls
44 lines (34 loc) · 1.29 KB
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
from typing import Any
import aiohttp
from pydantic import BaseModel
from datatypes import Image, Text
proxy_url = "https://ulhcn-01gd3c9epmk5xj2y9a9jrrvgt8.litng-ai-03.litng.ai/api/predict"
class ColdStartProxy:
def __init__(self, proxy_url):
self.proxy_url = proxy_url
self.proxy_timeout = 20
async def handle_request(self, request: BaseModel) -> Any:
# TODO default handler
pass
class CustomColdStartProxy(ColdStartProxy):
def __init__(self):
super().__init__(proxy_url)
async def handle_request(self, request: Text) -> Any:
try:
async with aiohttp.ClientSession() as session:
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
async with session.post(
self.proxy_url,
json={"prompt": request.text},
timeout=self.proxy_timeout,
headers=headers,
) as response:
resp = await response.json()
return Image(image=resp["image"][22:])
except Exception as ex:
# TODO - exception raising
print(f"Error in proxy: {ex}")
return None